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
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Builders/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private RelationshipData GetRelationshipData(RelationshipAttribute attr, Context

var relationshipData = new RelationshipData();

if (attr.DocumentLinks.HasFlag(Link.None) == false)
if (_jsonApiContext.Options.DefaultRelationshipLinks.HasFlag(Link.None) == false && attr.DocumentLinks.HasFlag(Link.None) == false)
{
relationshipData.Links = new Links();
if (attr.DocumentLinks.HasFlag(Link.Self))
Expand Down
20 changes: 20 additions & 0 deletions src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public class JsonApiOptions
/// </example>
public bool RelativeLinks { get; set; }

/// <summary>
/// Which links to include in relationships. Defaults to <see cref="Link.All"/>.
/// </summary>
/// <example>
/// <code>
/// options.DefaultRelationshipLinks = Link.None;
/// </code>
/// <code>
/// {
/// "type": "articles",
/// "id": "4309",
/// "relationships": {
/// "author": {}
/// }
/// }
/// }
/// </code>
/// </example>
public Link DefaultRelationshipLinks { get; set; } = Link.All;

/// <summary>
/// Whether or not to allow all custom query parameters.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions test/UnitTests/Builders/DocumentBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ public void Related_Links_Can_Be_Disabled()
Assert.Null(document.Data.Relationships["related-model"].Links);
}

[Fact]
public void Related_Links_Can_Be_Disabled_Globally()
{
// arrange
_pageManager.PageSize = 1;
_pageManager.TotalRecords = 1;
_pageManager.CurrentPage = 1;

_options.DefaultRelationshipLinks = Link.None;

_jsonApiContextMock
.Setup(m => m.ResourceGraph)
.Returns(_options.ResourceGraph);

var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object);
var entity = new RelatedModel();

// act
var document = documentBuilder.Build(entity);

// assert
Assert.Null(document.Data.Relationships["models"].Links);
}

[Fact]
public void Related_Data_Included_In_Relationships_By_Default()
{
Expand Down