Skip to content

Commit 3ade2a7

Browse files
authored
Merge pull request #452 from milosloub/feat/#451
Feat/#451 Global relationship link settings
2 parents e58a48d + 11a3ef7 commit 3ade2a7

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Diff for: src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private RelationshipData GetRelationshipData(RelationshipAttribute attr, Context
168168

169169
var relationshipData = new RelationshipData();
170170

171-
if (attr.DocumentLinks.HasFlag(Link.None) == false)
171+
if (_jsonApiContext.Options.DefaultRelationshipLinks.HasFlag(Link.None) == false && attr.DocumentLinks.HasFlag(Link.None) == false)
172172
{
173173
relationshipData.Links = new Links();
174174
if (attr.DocumentLinks.HasFlag(Link.Self))

Diff for: src/JsonApiDotNetCore/Configuration/JsonApiOptions.cs

+20
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,26 @@ public class JsonApiOptions
102102
/// </example>
103103
public bool RelativeLinks { get; set; }
104104

105+
/// <summary>
106+
/// Which links to include in relationships. Defaults to <see cref="Link.All"/>.
107+
/// </summary>
108+
/// <example>
109+
/// <code>
110+
/// options.DefaultRelationshipLinks = Link.None;
111+
/// </code>
112+
/// <code>
113+
/// {
114+
/// "type": "articles",
115+
/// "id": "4309",
116+
/// "relationships": {
117+
/// "author": {}
118+
/// }
119+
/// }
120+
/// }
121+
/// </code>
122+
/// </example>
123+
public Link DefaultRelationshipLinks { get; set; } = Link.All;
124+
105125
/// <summary>
106126
/// Whether or not to allow all custom query parameters.
107127
/// </summary>

Diff for: test/UnitTests/Builders/DocumentBuilder_Tests.cs

+24
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,30 @@ public void Related_Links_Can_Be_Disabled()
122122
Assert.Null(document.Data.Relationships["related-model"].Links);
123123
}
124124

125+
[Fact]
126+
public void Related_Links_Can_Be_Disabled_Globally()
127+
{
128+
// arrange
129+
_pageManager.PageSize = 1;
130+
_pageManager.TotalRecords = 1;
131+
_pageManager.CurrentPage = 1;
132+
133+
_options.DefaultRelationshipLinks = Link.None;
134+
135+
_jsonApiContextMock
136+
.Setup(m => m.ResourceGraph)
137+
.Returns(_options.ResourceGraph);
138+
139+
var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object);
140+
var entity = new RelatedModel();
141+
142+
// act
143+
var document = documentBuilder.Build(entity);
144+
145+
// assert
146+
Assert.Null(document.Data.Relationships["models"].Links);
147+
}
148+
125149
[Fact]
126150
public void Related_Data_Included_In_Relationships_By_Default()
127151
{

0 commit comments

Comments
 (0)