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
Show file tree
Hide file tree
Changes from 2 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.DisableSelfAndRelatedLinks && attr.DocumentLinks.HasFlag(Link.None) == false)
{
relationshipData.Links = new Links();
if (attr.DocumentLinks.HasFlag(Link.Self))
Expand Down
10 changes: 10 additions & 0 deletions src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be the global default Link. Maybe something like:

public Link DefaultRelationshipLinks { get; set;} = Link.All

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense with Link.All. I thought about this more and got following:
a) we can expose new property on JsonApiOptions "DefaultRelationshipLinks" (instead of DisableSelfAndRelatedLinks )
b) we can use present property ResourceGraphBuilder on JsonApiOptions, set it's DocumentLinks and than check this property while building resource in DocumentBuilder.cs.

Second solution might be breaking change, if you are developer who set it like this before. But I can´t imagine why anyone would do this.
What do you think?

Copy link
Contributor

@jaredcnance jaredcnance Nov 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think option "a" would be better so we can distinguish between the resource level links and the relationship links. That being said, we should probably move the ResourceGraphBuilder.DocumentLinks into JsonApiOptions as well. Ideally, since auto discovery has landed, ResourceGraphBuilder shouldn't be necessary for most applications.


/// <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.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()
{
Expand Down