Skip to content

Commit ab67c86

Browse files
author
Bart Koelman
committed
Fixed: also hide links for missing controllers on deeply nested relationships from includes
1 parent 473d53b commit ab67c86

File tree

8 files changed

+27
-16
lines changed

8 files changed

+27
-16
lines changed

src/JsonApiDotNetCore/Serialization/Response/LinkBuilder.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,6 @@ private bool ShouldIncludeResourceLink(LinkTypes linkType, ResourceType resource
255255
private string? GetLinkForResourceSelf(ResourceType resourceType, IIdentifiable resource)
256256
{
257257
string? controllerName = _controllerResourceMapping.GetControllerNameForResourceType(resourceType);
258-
259-
if (controllerName == null)
260-
{
261-
// When passing null to RenderLinkForAction, it uses the controller for the current endpoint. This is incorrect for
262-
// included resources of a different resource type: it should hide their Self links when there's no controller for them.
263-
return null;
264-
}
265-
266258
IDictionary<string, object?> routeValues = GetRouteValues(resource.StringId!, null);
267259

268260
return RenderLinkForAction(controllerName, GetPrimaryControllerActionName, routeValues);
@@ -320,6 +312,13 @@ private bool ShouldIncludeResourceLink(LinkTypes linkType, ResourceType resource
320312

321313
protected virtual string? RenderLinkForAction(string? controllerName, string actionName, IDictionary<string, object?> routeValues)
322314
{
315+
if (controllerName == null)
316+
{
317+
// When passing null to LinkGenerator, it uses the controller for the current endpoint. This is incorrect for
318+
// included resources of a different resource type: it should hide its links when there's no controller for them.
319+
return null;
320+
}
321+
323322
return _options.UseRelativeLinks
324323
? _linkGenerator.GetPathByAction(HttpContext, actionName, controllerName, routeValues)
325324
: _linkGenerator.GetUriByAction(HttpContext, actionName, controllerName, routeValues);

test/JsonApiDotNetCoreTests/IntegrationTests/Links/LinkInclusionIncludeTests.cs

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public LinkInclusionIncludeTests(IntegrationTestContext<TestableStartup<LinksDbC
1919
}
2020

2121
[Fact]
22-
public async Task Hides_Self_link_in_included_resources_for_unregistered_controllers()
22+
public async Task Hides_links_for_unregistered_controllers()
2323
{
2424
// Arrange
2525
PhotoLocation location = _fakers.PhotoLocation.Generate();
@@ -40,22 +40,26 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
4040
// Assert
4141
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
4242

43+
responseDocument.Data.SingleValue.ShouldNotBeNull();
44+
45+
responseDocument.Data.SingleValue.Relationships.ShouldContainKey("photo").With(value =>
46+
{
47+
value.ShouldNotBeNull();
48+
value.Links.ShouldNotBeNull();
49+
});
50+
4351
responseDocument.Included.ShouldHaveCount(2);
4452

4553
responseDocument.Included.Should().ContainSingle(resource => resource.Type == "photos").Subject.With(resource =>
4654
{
4755
resource.Links.Should().BeNull();
48-
49-
resource.Relationships.ShouldContainKey("location").With(value =>
50-
{
51-
value.ShouldNotBeNull();
52-
value.Links.ShouldNotBeNull();
53-
});
56+
resource.Relationships.Should().BeNull();
5457
});
5558

5659
responseDocument.Included.Should().ContainSingle(resource => resource.Type == "photoAlbums").Subject.With(resource =>
5760
{
5861
resource.Links.Should().BeNull();
62+
resource.Relationships.Should().BeNull();
5963
});
6064
}
6165
}

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceWithToOneRelationshipTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public CreateResourceWithToOneRelationshipTests(IntegrationTestContext<TestableS
2222
testContext.UseController<WorkItemGroupsController>();
2323
testContext.UseController<WorkItemsController>();
2424
testContext.UseController<RgbColorsController>();
25+
testContext.UseController<UserAccountsController>();
2526

2627
var options = (JsonApiOptions)testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
2728
options.AllowClientGeneratedIds = true;

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Fetching/FetchResourceTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public FetchResourceTests(IntegrationTestContext<TestableStartup<ReadWriteDbCont
1717

1818
testContext.UseController<WorkItemsController>();
1919
testContext.UseController<UserAccountsController>();
20+
testContext.UseController<WorkTagsController>();
2021
}
2122

2223
[Fact]

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/ReplaceToManyRelationshipTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public ReplaceToManyRelationshipTests(IntegrationTestContext<TestableStartup<Rea
1919
_testContext = testContext;
2020

2121
testContext.UseController<WorkItemsController>();
22+
testContext.UseController<UserAccountsController>();
2223

2324
testContext.ConfigureServicesAfterStartup(services =>
2425
{

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateToOneRelationshipTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public UpdateToOneRelationshipTests(IntegrationTestContext<TestableStartup<ReadW
2020
testContext.UseController<WorkItemsController>();
2121
testContext.UseController<WorkItemGroupsController>();
2222
testContext.UseController<RgbColorsController>();
23+
testContext.UseController<UserAccountsController>();
2324

2425
testContext.ConfigureServicesAfterStartup(services =>
2526
{

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/WorkTag.cs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace JsonApiDotNetCoreTests.IntegrationTests.ReadWrite;
66

77
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
8+
[Resource(ControllerNamespace = "JsonApiDotNetCoreTests.IntegrationTests.ReadWrite")]
89
public sealed class WorkTag : Identifiable<int>
910
{
1011
[Attr]

test/JsonApiDotNetCoreTests/UnitTests/Links/LinkInclusionTests.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public void Applies_cascading_settings_for_top_level_links(LinkTypes linksInReso
6969
PrimaryId = "1",
7070
IsCollection = true,
7171
Kind = EndpointKind.Relationship,
72-
Relationship = new HasOneAttribute()
72+
Relationship = new HasOneAttribute
73+
{
74+
LeftType = exampleResourceType
75+
}
7376
};
7477

7578
var paginationContext = new PaginationContext

0 commit comments

Comments
 (0)