Skip to content

Commit b5eaabd

Browse files
author
Bart Koelman
committed
Fixed: call ResourceDefinition.OnApplyIncludes for all children, even when empty
1 parent ea88076 commit b5eaabd

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/JsonApiDotNetCore/Queries/Internal/QueryLayerComposer.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,12 @@ private IImmutableSet<IncludeElementExpression> ProcessIncludeSet(IImmutableSet<
185185

186186
parentLayer.Projection.Add(includeElement.Relationship, child);
187187

188-
if (includeElement.Children.Any())
189-
{
190-
IImmutableSet<IncludeElementExpression> updatedChildren =
191-
ProcessIncludeSet(includeElement.Children, child, relationshipChain, constraints);
188+
IImmutableSet<IncludeElementExpression> updatedChildren =
189+
ProcessIncludeSet(includeElement.Children, child, relationshipChain, constraints);
192190

193-
if (!ReferenceEquals(includeElement.Children, updatedChildren))
194-
{
195-
updatesInChildren.Add(includeElement, updatedChildren);
196-
}
191+
if (!ReferenceEquals(includeElement.Children, updatedChildren))
192+
{
193+
updatesInChildren.Add(includeElement, updatedChildren);
197194
}
198195
}
199196
}

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceDefinitions/Reading/ResourceDefinitionReadTests.cs

+50-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,56 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
124124

125125
hitCounter.HitExtensibilityPoints.Should().BeEquivalentTo(new[]
126126
{
127-
(typeof(Moon), ResourceDefinitionHitCounter.ExtensibilityPoint.OnApplyIncludes)
127+
(typeof(Moon), ResourceDefinitionHitCounter.ExtensibilityPoint.OnApplyIncludes),
128+
(typeof(Planet), ResourceDefinitionHitCounter.ExtensibilityPoint.OnApplyIncludes)
129+
}, options => options.WithStrictOrdering());
130+
}
131+
132+
[Fact]
133+
public async Task Include_from_included_resource_definition_is_added()
134+
{
135+
// Arrange
136+
var hitCounter = _testContext.Factory.Services.GetRequiredService<ResourceDefinitionHitCounter>();
137+
138+
var settingsProvider = (TestClientSettingsProvider)_testContext.Factory.Services.GetRequiredService<IClientSettingsProvider>();
139+
settingsProvider.AutoIncludeOrbitingPlanetForMoons();
140+
141+
Planet planet = _fakers.Planet.Generate();
142+
planet.Moons = _fakers.Moon.Generate(1).ToHashSet();
143+
planet.Moons.ElementAt(0).OrbitsAround = _fakers.Planet.Generate();
144+
145+
await _testContext.RunOnDatabaseAsync(async dbContext =>
146+
{
147+
dbContext.Planets.Add(planet);
148+
await dbContext.SaveChangesAsync();
149+
});
150+
151+
string route = $"/planets/{planet.StringId}?include=moons";
152+
153+
// Act
154+
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
155+
156+
// Assert
157+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
158+
159+
responseDocument.Data.SingleValue.Should().NotBeNull();
160+
161+
responseDocument.Included.Should().HaveCount(2);
162+
163+
responseDocument.Included[0].Type.Should().Be("moons");
164+
responseDocument.Included[0].Id.Should().Be(planet.Moons.ElementAt(0).StringId);
165+
responseDocument.Included[0].Attributes["name"].Should().Be(planet.Moons.ElementAt(0).Name);
166+
167+
responseDocument.Included[1].Type.Should().Be("planets");
168+
responseDocument.Included[1].Id.Should().Be(planet.Moons.ElementAt(0).OrbitsAround.StringId);
169+
responseDocument.Included[1].Attributes["publicName"].Should().Be(planet.Moons.ElementAt(0).OrbitsAround.PublicName);
170+
171+
hitCounter.HitExtensibilityPoints.Should().BeEquivalentTo(new[]
172+
{
173+
(typeof(Planet), ResourceDefinitionHitCounter.ExtensibilityPoint.OnApplyFilter),
174+
(typeof(Planet), ResourceDefinitionHitCounter.ExtensibilityPoint.OnApplyIncludes),
175+
(typeof(Moon), ResourceDefinitionHitCounter.ExtensibilityPoint.OnApplyIncludes),
176+
(typeof(Planet), ResourceDefinitionHitCounter.ExtensibilityPoint.OnApplyIncludes)
128177
}, options => options.WithStrictOrdering());
129178
}
130179

0 commit comments

Comments
 (0)