From 448aecf1f3dc8d7f401771f17561af48f5122b4b Mon Sep 17 00:00:00 2001 From: Milos <Milos@DESKTOP-F906DOQ> Date: Thu, 22 Nov 2018 10:13:35 +0100 Subject: [PATCH 1/2] Ability to turn of self and related links glogally --- .../Builders/DocumentBuilder.cs | 2 +- .../Configuration/JsonApiOptions.cs | 10 ++++++++ .../Builders/DocumentBuilder_Tests.cs | 24 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs index 1287f35a05..e8b917c99a 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.DisableSelfAndRelatedLinks && 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..78901242c2 100644 --- a/src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs +++ b/src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs @@ -102,6 +102,16 @@ public class JsonApiOptions /// </example> public bool RelativeLinks { get; set; } + /// <summary> + /// Whether or not to include self and related links + /// </summary> + /// <example> + /// <code> + /// options.DisableSelfAndRelatedLinks = true; + /// </code> + /// </example> + public bool DisableSelfAndRelatedLinks { get; set; } + /// <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..5c51c94658 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.DisableSelfAndRelatedLinks = true; + + _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() { From 11a3ef79b695dddd6fd1f2a402f02cd8f9e406ee Mon Sep 17 00:00:00 2001 From: Milos <Milos@DESKTOP-F906DOQ> Date: Thu, 29 Nov 2018 12:22:29 +0100 Subject: [PATCH 2/2] Option renamed to DefaultRelationshipLinks and changed to Link type --- .../Builders/DocumentBuilder.cs | 2 +- .../Configuration/JsonApiOptions.cs | 16 +++++++++++++--- test/UnitTests/Builders/DocumentBuilder_Tests.cs | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs b/src/JsonApiDotNetCore/Builders/DocumentBuilder.cs index e8b917c99a..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 (!_jsonApiContext.Options.DisableSelfAndRelatedLinks && 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 78901242c2..e8e7d83be8 100644 --- a/src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs +++ b/src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs @@ -103,14 +103,24 @@ public class JsonApiOptions public bool RelativeLinks { get; set; } /// <summary> - /// Whether or not to include self and related links + /// Which links to include in relationships. Defaults to <see cref="Link.All"/>. /// </summary> /// <example> /// <code> - /// options.DisableSelfAndRelatedLinks = true; + /// options.DefaultRelationshipLinks = Link.None; + /// </code> + /// <code> + /// { + /// "type": "articles", + /// "id": "4309", + /// "relationships": { + /// "author": {} + /// } + /// } + /// } /// </code> /// </example> - public bool DisableSelfAndRelatedLinks { get; set; } + public Link DefaultRelationshipLinks { get; set; } = Link.All; /// <summary> /// Whether or not to allow all custom query parameters. diff --git a/test/UnitTests/Builders/DocumentBuilder_Tests.cs b/test/UnitTests/Builders/DocumentBuilder_Tests.cs index 5c51c94658..459a8a758d 100644 --- a/test/UnitTests/Builders/DocumentBuilder_Tests.cs +++ b/test/UnitTests/Builders/DocumentBuilder_Tests.cs @@ -130,7 +130,7 @@ public void Related_Links_Can_Be_Disabled_Globally() _pageManager.TotalRecords = 1; _pageManager.CurrentPage = 1; - _options.DisableSelfAndRelatedLinks = true; + _options.DefaultRelationshipLinks = Link.None; _jsonApiContextMock .Setup(m => m.ResourceGraph)