Skip to content

fix(QuerySet): filter was not properly parsing queries with colons #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/JsonApiDotNetCore/Internal/Query/QuerySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ private List<FilterQuery> ParseFilterQuery(string key, string value)
return (string.Empty, value);

// remove prefix from value
if(Enum.TryParse(operation[0], out FilterOperations op) == false)
return (string.Empty, value);

var prefix = operation[0];
value = operation[1];
value = string.Join(":", operation.Skip(1));

return (prefix, value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.1.2</VersionPrefix>
<VersionPrefix>2.1.3</VersionPrefix>
<TargetFrameworks>netstandard1.6</TargetFrameworks>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand Down
54 changes: 54 additions & 0 deletions test/UnitTests/Internal/QuerySet_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,60 @@ public void Can_Build_Filters()
Assert.Equal("value", querySet.Filters.Single(f => f.Key == "Key").Value);
}

[Fact]
public void Filters_Properly_Parses_DateTime_With_Operation()
{
// arrange
const string dt = "2017-08-15T22:43:47.0156350-05:00";
var query = new Dictionary<string, StringValues> {
{ "filter[key]", new StringValues("le:" + dt) }
};

_queryCollectionMock
.Setup(m => m.GetEnumerator())
.Returns(query.GetEnumerator());

_jsonApiContextMock
.Setup(m => m.GetControllerAttribute<DisableQueryAttribute>())
.Returns(new DisableQueryAttribute(QueryParams.None));

// act -- ctor calls BuildQuerySet()
var querySet = new QuerySet(
_jsonApiContextMock.Object,
_queryCollectionMock.Object);

// assert
Assert.Equal(dt, querySet.Filters.Single(f => f.Key == "Key").Value);
Assert.Equal("le", querySet.Filters.Single(f => f.Key == "Key").Operation);
}

[Fact]
public void Filters_Properly_Parses_DateTime_Without_Operation()
{
// arrange
const string dt = "2017-08-15T22:43:47.0156350-05:00";
var query = new Dictionary<string, StringValues> {
{ "filter[key]", new StringValues(dt) }
};

_queryCollectionMock
.Setup(m => m.GetEnumerator())
.Returns(query.GetEnumerator());

_jsonApiContextMock
.Setup(m => m.GetControllerAttribute<DisableQueryAttribute>())
.Returns(new DisableQueryAttribute(QueryParams.None));

// act -- ctor calls BuildQuerySet()
var querySet = new QuerySet(
_jsonApiContextMock.Object,
_queryCollectionMock.Object);

// assert
Assert.Equal(dt, querySet.Filters.Single(f => f.Key == "Key").Value);
Assert.Equal(string.Empty, querySet.Filters.Single(f => f.Key == "Key").Operation);
}

[Fact]
public void Can_Disable_Filters()
{
Expand Down