Skip to content

Commit a622954

Browse files
author
Bart Koelman
committed
Bugfix: hide Self link in included resources when there's no registered controller for it. Before, it would use the controller URL from the current request in the Self link, which is wrong.
1 parent bc5fbe0 commit a622954

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/JsonApiDotNetCore/Serialization/Response/LinkBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ 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+
258266
IDictionary<string, object?> routeValues = GetRouteValues(resource.StringId!, null);
259267

260268
return RenderLinkForAction(controllerName, GetPrimaryControllerActionName, routeValues);

test/JsonApiDotNetCoreTests/IntegrationTests/Links/LinkInclusionTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public LinkInclusionTests(IntegrationTestContext<TestableStartup<LinksDbContext>
1717

1818
testContext.UseController<PhotosController>();
1919
testContext.UseController<PhotoLocationsController>();
20+
testContext.UseController<PhotoAlbumsController>();
2021
}
2122

2223
[Fact]

test/JsonApiDotNetCoreTests/UnitTests/Links/LinkInclusionTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using Humanizer;
23
using JsonApiDotNetCore.Configuration;
34
using JsonApiDotNetCore.Middleware;
45
using JsonApiDotNetCore.Queries;
@@ -386,7 +387,7 @@ public ResourceType GetResourceTypeForController(Type? controllerType)
386387

387388
public string? GetControllerNameForResourceType(ResourceType? resourceType)
388389
{
389-
return null;
390+
return resourceType == null ? null : $"{resourceType.PublicName.Pascalize()}Controller";
390391
}
391392
}
392393

0 commit comments

Comments
 (0)