Skip to content

Commit 448aecf

Browse files
MilosMilos
Milos
authored and
Milos
committed
Ability to turn of self and related links glogally
1 parent 0b4a6c3 commit 448aecf

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-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.DisableSelfAndRelatedLinks && 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

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

105+
/// <summary>
106+
/// Whether or not to include self and related links
107+
/// </summary>
108+
/// <example>
109+
/// <code>
110+
/// options.DisableSelfAndRelatedLinks = true;
111+
/// </code>
112+
/// </example>
113+
public bool DisableSelfAndRelatedLinks { get; set; }
114+
105115
/// <summary>
106116
/// Whether or not to allow all custom query parameters.
107117
/// </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.DisableSelfAndRelatedLinks = true;
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)