Skip to content

Commit

Permalink
Fixed: full linkage does not allow primary resources to occur in incl…
Browse files Browse the repository at this point in the history
…uded
  • Loading branch information
bkoelman committed Oct 2, 2022
1 parent d71ea37 commit 0ca0c12
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,17 @@ public IList<ResourceObject> GetResponseIncluded()
VisitRelationshipChildrenInSubtree(child, visited);
}

return visited.Select(node => node.ResourceObject).ToArray();
List<ResourceObject> includes = visited.Select(node => node.ResourceObject).ToList();

foreach (ResourceObject primaryResourceObjects in GetDirectChildren().Select(node => node.ResourceObject))
{
if (includes.Contains(primaryResourceObjects))
{
includes.Remove(primaryResourceObjects);
}
}

return includes;
}

private IList<ResourceObjectTreeNode> GetDirectChildren()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,29 +398,25 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
responseDocument.Data.SingleValue.Id.Should().Be(comment.StringId);
responseDocument.Data.SingleValue.Attributes.ShouldContainKey("text").With(value => value.Should().Be(comment.Text));

responseDocument.Included.ShouldHaveCount(5);
responseDocument.Included.ShouldHaveCount(4);

responseDocument.Included[0].Type.Should().Be("blogPosts");
responseDocument.Included[0].Id.Should().Be(comment.Parent.StringId);
responseDocument.Included[0].Attributes.ShouldContainKey("caption").With(value => value.Should().Be(comment.Parent.Caption));

responseDocument.Included[1].Type.Should().Be("comments");
responseDocument.Included[1].Id.Should().Be(comment.StringId);
responseDocument.Included[1].Attributes.ShouldContainKey("text").With(value => value.Should().Be(comment.Text));

responseDocument.Included[2].Type.Should().Be("comments");
responseDocument.Included[2].Id.Should().Be(comment.Parent.Comments.ElementAt(0).StringId);
responseDocument.Included[2].Attributes.ShouldContainKey("text").With(value => value.Should().Be(comment.Parent.Comments.ElementAt(0).Text));
responseDocument.Included[1].Id.Should().Be(comment.Parent.Comments.ElementAt(0).StringId);
responseDocument.Included[1].Attributes.ShouldContainKey("text").With(value => value.Should().Be(comment.Parent.Comments.ElementAt(0).Text));

string userName = comment.Parent.Comments.ElementAt(0).Author!.UserName;

responseDocument.Included[3].Type.Should().Be("webAccounts");
responseDocument.Included[3].Id.Should().Be(comment.Parent.Comments.ElementAt(0).Author!.StringId);
responseDocument.Included[3].Attributes.ShouldContainKey("userName").With(value => value.Should().Be(userName));
responseDocument.Included[2].Type.Should().Be("webAccounts");
responseDocument.Included[2].Id.Should().Be(comment.Parent.Comments.ElementAt(0).Author!.StringId);
responseDocument.Included[2].Attributes.ShouldContainKey("userName").With(value => value.Should().Be(userName));

responseDocument.Included[4].Type.Should().Be("comments");
responseDocument.Included[4].Id.Should().Be(comment.Parent.Comments.ElementAt(1).StringId);
responseDocument.Included[4].Attributes.ShouldContainKey("text").With(value => value.Should().Be(comment.Parent.Comments.ElementAt(1).Text));
responseDocument.Included[3].Type.Should().Be("comments");
responseDocument.Included[3].Id.Should().Be(comment.Parent.Comments.ElementAt(1).StringId);
responseDocument.Included[3].Attributes.ShouldContainKey("text").With(value => value.Should().Be(comment.Parent.Comments.ElementAt(1).Text));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public interface IClientSettingsProvider
{
bool IsIncludePlanetMoonsBlocked { get; }
bool ArePlanetsWithPrivateNameHidden { get; }
bool IsMoonOrbitingPlanetAutoIncluded { get; }
bool IsStarGivingLightToMoonAutoIncluded { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public sealed class Moon : Identifiable<int>

[HasOne]
public Planet OrbitsAround { get; set; } = null!;

[HasOne]
public Star? IsGivenLightBy { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public override IImmutableSet<IncludeElementExpression> OnApplyIncludes(IImmutab
{
base.OnApplyIncludes(existingIncludes);

if (!_clientSettingsProvider.IsMoonOrbitingPlanetAutoIncluded ||
existingIncludes.Any(include => include.Relationship.Property.Name == nameof(Moon.OrbitsAround)))
if (!_clientSettingsProvider.IsStarGivingLightToMoonAutoIncluded ||
existingIncludes.Any(include => include.Relationship.Property.Name == nameof(Moon.IsGivenLightBy)))
{
return existingIncludes;
}

RelationshipAttribute orbitsAroundRelationship = ResourceType.GetRelationshipByPropertyName(nameof(Moon.OrbitsAround));
RelationshipAttribute isGivenLightByRelationship = ResourceType.GetRelationshipByPropertyName(nameof(Moon.IsGivenLightBy));

return existingIncludes.Add(new IncludeElementExpression(orbitsAroundRelationship));
return existingIncludes.Add(new IncludeElementExpression(isGivenLightByRelationship));
}

public override QueryStringParameterHandlers<Moon> OnRegisterQueryableHandlersForQueryStringParameters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ public async Task Include_from_resource_definition_is_added()
var hitCounter = _testContext.Factory.Services.GetRequiredService<ResourceDefinitionHitCounter>();

var settingsProvider = (TestClientSettingsProvider)_testContext.Factory.Services.GetRequiredService<IClientSettingsProvider>();
settingsProvider.AutoIncludeOrbitingPlanetForMoons();
settingsProvider.AutoIncludeStarGivingLightToMoon();

Moon moon = _fakers.Moon.Generate();
moon.OrbitsAround = _fakers.Planet.Generate();
moon.IsGivenLightBy = _fakers.Star.Generate();

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
Expand All @@ -114,18 +115,18 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>

responseDocument.Data.SingleValue.ShouldNotBeNull();

responseDocument.Data.SingleValue.Relationships.ShouldContainKey("orbitsAround").With(value =>
responseDocument.Data.SingleValue.Relationships.ShouldContainKey("isGivenLightBy").With(value =>
{
value.ShouldNotBeNull();
value.Data.SingleValue.ShouldNotBeNull();
value.Data.SingleValue.Type.Should().Be("planets");
value.Data.SingleValue.Id.Should().Be(moon.OrbitsAround.StringId);
value.Data.SingleValue.Type.Should().Be("stars");
value.Data.SingleValue.Id.Should().Be(moon.IsGivenLightBy.StringId);
});

responseDocument.Included.ShouldHaveCount(1);
responseDocument.Included[0].Type.Should().Be("planets");
responseDocument.Included[0].Id.Should().Be(moon.OrbitsAround.StringId);
responseDocument.Included[0].Attributes.ShouldContainKey("publicName").With(value => value.Should().Be(moon.OrbitsAround.PublicName));
responseDocument.Included[0].Type.Should().Be("stars");
responseDocument.Included[0].Id.Should().Be(moon.IsGivenLightBy.StringId);
responseDocument.Included[0].Attributes.ShouldContainKey("name").With(value => value.Should().Be(moon.IsGivenLightBy.Name));

hitCounter.HitExtensibilityPoints.Should().BeEquivalentTo(new[]
{
Expand All @@ -134,12 +135,12 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
(typeof(Moon), ResourceDefinitionExtensibilityPoints.OnApplySort),
(typeof(Moon), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Moon), ResourceDefinitionExtensibilityPoints.OnApplyIncludes),
(typeof(Planet), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Planet), ResourceDefinitionExtensibilityPoints.OnApplyIncludes),
(typeof(Star), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Star), ResourceDefinitionExtensibilityPoints.OnApplyIncludes),
(typeof(Moon), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Moon), ResourceDefinitionExtensibilityPoints.GetMeta),
(typeof(Planet), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Planet), ResourceDefinitionExtensibilityPoints.GetMeta)
(typeof(Star), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Star), ResourceDefinitionExtensibilityPoints.GetMeta)
}, options => options.WithStrictOrdering());
}

Expand All @@ -150,11 +151,11 @@ public async Task Include_from_included_resource_definition_is_added()
var hitCounter = _testContext.Factory.Services.GetRequiredService<ResourceDefinitionHitCounter>();

var settingsProvider = (TestClientSettingsProvider)_testContext.Factory.Services.GetRequiredService<IClientSettingsProvider>();
settingsProvider.AutoIncludeOrbitingPlanetForMoons();
settingsProvider.AutoIncludeStarGivingLightToMoon();

Planet planet = _fakers.Planet.Generate();
planet.Moons = _fakers.Moon.Generate(1).ToHashSet();
planet.Moons.ElementAt(0).OrbitsAround = _fakers.Planet.Generate();
planet.Moons.ElementAt(0).IsGivenLightBy = _fakers.Star.Generate();

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
Expand All @@ -178,11 +179,9 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
responseDocument.Included[0].Id.Should().Be(planet.Moons.ElementAt(0).StringId);
responseDocument.Included[0].Attributes.ShouldContainKey("name").With(value => value.Should().Be(planet.Moons.ElementAt(0).Name));

string moonName = planet.Moons.ElementAt(0).OrbitsAround.PublicName;

responseDocument.Included[1].Type.Should().Be("planets");
responseDocument.Included[1].Id.Should().Be(planet.Moons.ElementAt(0).OrbitsAround.StringId);
responseDocument.Included[1].Attributes.ShouldContainKey("publicName").With(value => value.Should().Be(moonName));
responseDocument.Included[1].Type.Should().Be("stars");
responseDocument.Included[1].Id.Should().Be(planet.Moons.ElementAt(0).IsGivenLightBy!.StringId);
responseDocument.Included[1].Attributes.ShouldContainKey("name").With(value => value.Should().Be(planet.Moons.ElementAt(0).IsGivenLightBy!.Name));

hitCounter.HitExtensibilityPoints.Should().BeEquivalentTo(new[]
{
Expand All @@ -196,11 +195,14 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
(typeof(Moon), ResourceDefinitionExtensibilityPoints.OnApplyPagination),
(typeof(Moon), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Moon), ResourceDefinitionExtensibilityPoints.OnApplyIncludes),
(typeof(Planet), ResourceDefinitionExtensibilityPoints.OnApplyIncludes),
(typeof(Star), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Star), ResourceDefinitionExtensibilityPoints.OnApplyIncludes),
(typeof(Planet), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Planet), ResourceDefinitionExtensibilityPoints.GetMeta),
(typeof(Moon), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Moon), ResourceDefinitionExtensibilityPoints.GetMeta)
(typeof(Moon), ResourceDefinitionExtensibilityPoints.GetMeta),
(typeof(Star), ResourceDefinitionExtensibilityPoints.OnApplySparseFieldSet),
(typeof(Star), ResourceDefinitionExtensibilityPoints.GetMeta)
}, options => options.WithStrictOrdering());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ internal sealed class TestClientSettingsProvider : IClientSettingsProvider
{
public bool IsIncludePlanetMoonsBlocked { get; private set; }
public bool ArePlanetsWithPrivateNameHidden { get; private set; }
public bool IsMoonOrbitingPlanetAutoIncluded { get; private set; }
public bool IsStarGivingLightToMoonAutoIncluded { get; private set; }

public void ResetToDefaults()
{
IsIncludePlanetMoonsBlocked = false;
ArePlanetsWithPrivateNameHidden = false;
IsMoonOrbitingPlanetAutoIncluded = false;
IsStarGivingLightToMoonAutoIncluded = false;
}

public void BlockIncludePlanetMoons()
Expand All @@ -23,8 +23,8 @@ public void HidePlanetsWithPrivateName()
ArePlanetsWithPrivateNameHidden = true;
}

public void AutoIncludeOrbitingPlanetForMoons()
public void AutoIncludeStarGivingLightToMoon()
{
IsMoonOrbitingPlanetAutoIncluded = true;
IsStarGivingLightToMoonAutoIncluded = true;
}
}

0 comments on commit 0ca0c12

Please sign in to comment.