diff --git a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs index 1287f35a05..82c3d2ec75 100644 --- a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs @@ -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)) diff --git a/src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs b/src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs index 2a37256496..e8e7d83be8 100644 --- a/src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs +++ b/src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs @@ -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> diff --git a/test/UnitTests/Builders/DocumentBuilder_Tests.cs b/test/UnitTests/Builders/DocumentBuilder_Tests.cs index d15a44ca4f..459a8a758d 100644 --- a/test/UnitTests/Builders/DocumentBuilder_Tests.cs +++ b/test/UnitTests/Builders/DocumentBuilder_Tests.cs @@ -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() {