Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions src/JsonApiDotNetCore/Serialization/Response/LinkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ private bool ShouldIncludeResourceLink(LinkTypes linkType, ResourceType resource

protected virtual string? RenderLinkForAction(string? controllerName, string actionName, IDictionary<string, object?> routeValues)
{
if (controllerName == null)
{
// When passing null to LinkGenerator, it uses the controller for the current endpoint. This is incorrect for
// included resources of a different resource type: it should hide its links when there's no controller for them.
return null;
}

return _options.UseRelativeLinks
? _linkGenerator.GetPathByAction(HttpContext, actionName, controllerName, routeValues)
: _linkGenerator.GetUriByAction(HttpContext, actionName, controllerName, routeValues);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Net;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using TestBuildingBlocks;
using Xunit;

namespace JsonApiDotNetCoreTests.IntegrationTests.Links;

public sealed class LinkInclusionIncludeTests : IClassFixture<IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext>>
{
private readonly IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext> _testContext;
private readonly LinksFakers _fakers = new();

public LinkInclusionIncludeTests(IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext> testContext)
{
_testContext = testContext;

testContext.UseController<PhotoLocationsController>();
}

[Fact]
public async Task Hides_links_for_unregistered_controllers()
{
// Arrange
PhotoLocation location = _fakers.PhotoLocation.Generate();
location.Photo = _fakers.Photo.Generate();
location.Album = _fakers.PhotoAlbum.Generate();

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.PhotoLocations.Add(location);
await dbContext.SaveChangesAsync();
});

string route = $"/photoLocations/{location.StringId}?include=photo,album";

// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);

// Assert
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);

responseDocument.Data.SingleValue.ShouldNotBeNull();

responseDocument.Data.SingleValue.Relationships.ShouldContainKey("photo").With(value =>
{
value.ShouldNotBeNull();
value.Links.ShouldNotBeNull();
});

responseDocument.Included.ShouldHaveCount(2);

responseDocument.Included.Should().ContainSingle(resource => resource.Type == "photos").Subject.With(resource =>
{
resource.Links.Should().BeNull();
resource.Relationships.Should().BeNull();
});

responseDocument.Included.Should().ContainSingle(resource => resource.Type == "photoAlbums").Subject.With(resource =>
{
resource.Links.Should().BeNull();
resource.Relationships.Should().BeNull();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public LinkInclusionTests(IntegrationTestContext<TestableStartup<LinksDbContext>

testContext.UseController<PhotosController>();
testContext.UseController<PhotoLocationsController>();
testContext.UseController<PhotoAlbumsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public CreateResourceWithToOneRelationshipTests(IntegrationTestContext<TestableS
testContext.UseController<WorkItemGroupsController>();
testContext.UseController<WorkItemsController>();
testContext.UseController<RgbColorsController>();
testContext.UseController<UserAccountsController>();

var options = (JsonApiOptions)testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
options.AllowClientGeneratedIds = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public FetchResourceTests(IntegrationTestContext<TestableStartup<ReadWriteDbCont

testContext.UseController<WorkItemsController>();
testContext.UseController<UserAccountsController>();
testContext.UseController<WorkTagsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public ReplaceToManyRelationshipTests(IntegrationTestContext<TestableStartup<Rea
_testContext = testContext;

testContext.UseController<WorkItemsController>();
testContext.UseController<UserAccountsController>();

testContext.ConfigureServicesAfterStartup(services =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public UpdateToOneRelationshipTests(IntegrationTestContext<TestableStartup<ReadW
testContext.UseController<WorkItemsController>();
testContext.UseController<WorkItemGroupsController>();
testContext.UseController<RgbColorsController>();
testContext.UseController<UserAccountsController>();

testContext.ConfigureServicesAfterStartup(services =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace JsonApiDotNetCoreTests.IntegrationTests.ReadWrite;

[UsedImplicitly(ImplicitUseTargetFlags.Members)]
[Resource(ControllerNamespace = "JsonApiDotNetCoreTests.IntegrationTests.ReadWrite")]
public sealed class WorkTag : Identifiable<int>
{
[Attr]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using Humanizer;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Queries;
Expand Down Expand Up @@ -68,7 +69,10 @@ public void Applies_cascading_settings_for_top_level_links(LinkTypes linksInReso
PrimaryId = "1",
IsCollection = true,
Kind = EndpointKind.Relationship,
Relationship = new HasOneAttribute()
Relationship = new HasOneAttribute
{
LeftType = exampleResourceType
}
};

var paginationContext = new PaginationContext
Expand Down Expand Up @@ -386,7 +390,7 @@ public ResourceType GetResourceTypeForController(Type? controllerType)

public string? GetControllerNameForResourceType(ResourceType? resourceType)
{
return null;
return resourceType == null ? null : $"{resourceType.PublicName.Pascalize()}Controller";
}
}

Expand Down