Skip to content

Feat/#451 #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 29, 2018
Merged
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
@@ -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))
20 changes: 20 additions & 0 deletions src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs
Original file line number Diff line number Diff line change
@@ -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>
24 changes: 24 additions & 0 deletions test/UnitTests/Builders/DocumentBuilder_Tests.cs
Original file line number Diff line number Diff line change
@@ -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()
{