Skip to content

Commit cc46a60

Browse files
authored
Merge pull request #544 from wisepotato/fix/sort-issues
Fix/sort issues
2 parents c6762b3 + dd82d55 commit cc46a60

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/JsonApiDotNetCore/Services/QueryParser.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ protected virtual List<SortQuery> ParseSortParameters(string value)
153153

154154
const char DESCENDING_SORT_OPERATOR = '-';
155155
var sortSegments = value.Split(QueryConstants.COMMA);
156-
156+
if(sortSegments.Where(s => s == string.Empty).Count() >0)
157+
{
158+
throw new JsonApiException(400, "The sort URI segment contained a null value.");
159+
}
157160
foreach (var sortSegment in sortSegments)
158161
{
159162
var propertyName = sortSegment;

test/UnitTests/Services/QueryParser_Tests.cs renamed to test/UnitTests/Services/QueryParserTests.cs

+26-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
namespace UnitTests.Services
1414
{
15-
public class QueryParser_Tests
15+
public class QueryParserTests
1616
{
1717
private readonly Mock<IControllerContext> _controllerContextMock;
1818
private readonly Mock<IQueryCollection> _queryCollectionMock;
1919

20-
public QueryParser_Tests()
20+
public QueryParserTests()
2121
{
2222
_controllerContextMock = new Mock<IControllerContext>();
2323
_queryCollectionMock = new Mock<IQueryCollection>();
@@ -126,11 +126,34 @@ public void Can_Disable_Filters()
126126
// assert
127127
Assert.Empty(querySet.Filters);
128128
}
129+
[Theory]
130+
[InlineData("text,,1")]
131+
[InlineData("text,hello,,5")]
132+
[InlineData(",,2")]
133+
public void Parse_EmptySortSegment_ReceivesJsonApiException(string stringSortQuery)
134+
{
135+
// Arrange
136+
var query = new Dictionary<string, StringValues> {
137+
{ "sort", new StringValues(stringSortQuery) }
138+
};
139+
140+
_queryCollectionMock
141+
.Setup(m => m.GetEnumerator())
142+
.Returns(query.GetEnumerator());
143+
144+
var queryParser = new QueryParser(_controllerContextMock.Object, new JsonApiOptions());
129145

146+
// Act / Assert
147+
var exception = Assert.Throws<JsonApiException>(() =>
148+
{
149+
var querySet = queryParser.Parse(_queryCollectionMock.Object);
150+
});
151+
Assert.Contains("sort", exception.Message);
152+
}
130153
[Fact]
131154
public void Can_Disable_Sort()
132155
{
133-
// arrange
156+
// Arrange
134157
var query = new Dictionary<string, StringValues> {
135158
{ "sort", new StringValues("-key") }
136159
};

test/UnitTests/UnitTests.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
44
<IsPackable>false</IsPackable>
@@ -9,6 +9,7 @@
99
</ItemGroup>
1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3" />
12+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
1213
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitVersion)" />
1314
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
1415
<PackageReference Include="Moq" Version="$(MoqVersion)" />

0 commit comments

Comments
 (0)