Skip to content

Fixed: incorrect meta:total on secondary endpoint with filter #1259

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 2 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 QueryLayerComposer(IEnumerable<IQueryConstraintProvider> constraintProvid

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,21 @@ 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);
Expand All @@ -61,6 +63,36 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
});
}

[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);

// 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_empty_collection()
{
Expand Down