Skip to content

Commit

Permalink
Merge pull request #1259 from bart-vmware/fix-secondary-reverse-filter
Browse files Browse the repository at this point in the history
Fixed: incorrect meta:total on secondary endpoint with filter
  • Loading branch information
bkoelman committed Feb 16, 2023
2 parents 0adebfd + a2c50e4 commit 56cfe08
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/JsonApiDotNetCore/Queries/Internal/QueryLayerComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,11 @@ public class QueryLayerComposer : IQueryLayerComposer

ExpressionInScope[] constraints = _constraintProviders.SelectMany(provider => provider.GetConstraints()).ToArray();

var secondaryScope = new ResourceFieldChainExpression(hasManyRelationship);

// @formatter:wrap_chained_method_calls chop_always
// @formatter:keep_existing_linebreaks true

FilterExpression[] filtersInSecondaryScope = constraints
.Where(constraint => secondaryScope.Equals(constraint.Scope))
.Where(constraint => constraint.Scope == null)
.Select(constraint => constraint.Expression)
.OfType<FilterExpression>()
.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ public sealed class SupportTicket : Identifiable<int>
{
[Attr]
public string Description { get; set; } = null!;

[HasOne]
public ProductFamily? ProductFamily { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,51 @@ public TopLevelCountTests(IntegrationTestContext<TestableStartup<MetaDbContext>,
}

[Fact]
public async Task Renders_resource_count_for_collection()
public async Task Renders_resource_count_for_primary_resources_endpoint_with_filter()
{
// Arrange
SupportTicket ticket = _fakers.SupportTicket.Generate();
List<SupportTicket> tickets = _fakers.SupportTicket.Generate(2);

tickets[1].Description = "Update firmware version";

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
await dbContext.ClearTableAsync<SupportTicket>();
dbContext.SupportTickets.Add(ticket);
dbContext.SupportTickets.AddRange(tickets);
await dbContext.SaveChangesAsync();
});

const string route = "/supportTickets";
const string route = "/supportTickets?filter=startsWith(description,'Update ')";

// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);

// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);

responseDocument.Meta.ShouldContainKey("total").With(value =>
{
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
element.GetInt32().Should().Be(1);
});
}

[Fact]
public async Task Renders_resource_count_for_secondary_resources_endpoint_with_filter()
{
// Arrange
ProductFamily family = _fakers.ProductFamily.Generate();
family.Tickets = _fakers.SupportTicket.Generate(2);

family.Tickets[1].Description = "Update firmware version";

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.ProductFamilies.Add(family);
await dbContext.SaveChangesAsync();
});

string route = $"/productFamilies/{family.StringId}/tickets?filter=contains(description,'firmware')";

// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
Expand Down

0 comments on commit 56cfe08

Please sign in to comment.