Skip to content

Commit b76629d

Browse files
author
Bart Koelman
committed
Pass the full resource to LinkBuilder, instead of just its ID. This allows for more intelligence in the link builder, such as handling versioning or inheritance.
1 parent 1badb50 commit b76629d

File tree

6 files changed

+20
-19
lines changed

6 files changed

+20
-19
lines changed

benchmarks/Serialization/SerializationBenchmarkBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,15 @@ public TopLevelLinks GetTopLevelLinks()
229229
};
230230
}
231231

232-
public ResourceLinks GetResourceLinks(ResourceType resourceType, string id)
232+
public ResourceLinks GetResourceLinks(ResourceType resourceType, IIdentifiable resource)
233233
{
234234
return new ResourceLinks
235235
{
236236
Self = "Resource:Self"
237237
};
238238
}
239239

240-
public RelationshipLinks GetRelationshipLinks(RelationshipAttribute relationship, string leftId)
240+
public RelationshipLinks GetRelationshipLinks(RelationshipAttribute relationship, IIdentifiable leftResource)
241241
{
242242
return new RelationshipLinks
243243
{

src/JsonApiDotNetCore/Serialization/Response/ILinkBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Resources;
23
using JsonApiDotNetCore.Resources.Annotations;
34
using JsonApiDotNetCore.Serialization.Objects;
45

@@ -17,11 +18,11 @@ public interface ILinkBuilder
1718
/// <summary>
1819
/// Builds the links object for a returned resource (primary or included).
1920
/// </summary>
20-
ResourceLinks? GetResourceLinks(ResourceType resourceType, string id);
21+
ResourceLinks? GetResourceLinks(ResourceType resourceType, IIdentifiable resource);
2122

2223
/// <summary>
2324
/// Builds the links object for a relationship inside a returned resource.
2425
/// </summary>
25-
RelationshipLinks? GetRelationshipLinks(RelationshipAttribute relationship, string leftId);
26+
RelationshipLinks? GetRelationshipLinks(RelationshipAttribute relationship, IIdentifiable leftResource);
2627
}
2728
}

src/JsonApiDotNetCore/Serialization/Response/LinkBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,16 @@ private string GetQueryStringInPaginationLink(int pageOffset, string? pageSizeVa
228228
}
229229

230230
/// <inheritdoc />
231-
public ResourceLinks? GetResourceLinks(ResourceType resourceType, string id)
231+
public ResourceLinks? GetResourceLinks(ResourceType resourceType, IIdentifiable resource)
232232
{
233233
ArgumentGuard.NotNull(resourceType, nameof(resourceType));
234-
ArgumentGuard.NotNullNorEmpty(id, nameof(id));
234+
ArgumentGuard.NotNull(resource, nameof(resource));
235235

236236
var links = new ResourceLinks();
237237

238238
if (ShouldIncludeResourceLink(LinkTypes.Self, resourceType))
239239
{
240-
links.Self = GetLinkForResourceSelf(resourceType, id);
240+
links.Self = GetLinkForResourceSelf(resourceType, resource);
241241
}
242242

243243
return links.HasValue() ? links : null;
@@ -257,30 +257,30 @@ private bool ShouldIncludeResourceLink(LinkTypes linkType, ResourceType resource
257257
return _options.ResourceLinks.HasFlag(linkType);
258258
}
259259

260-
private string? GetLinkForResourceSelf(ResourceType resourceType, string resourceId)
260+
private string? GetLinkForResourceSelf(ResourceType resourceType, IIdentifiable resource)
261261
{
262262
string? controllerName = _controllerResourceMapping.GetControllerNameForResourceType(resourceType);
263-
IDictionary<string, object?> routeValues = GetRouteValues(resourceId, null);
263+
IDictionary<string, object?> routeValues = GetRouteValues(resource.StringId!, null);
264264

265265
return RenderLinkForAction(controllerName, GetPrimaryControllerActionName, routeValues);
266266
}
267267

268268
/// <inheritdoc />
269-
public RelationshipLinks? GetRelationshipLinks(RelationshipAttribute relationship, string leftId)
269+
public RelationshipLinks? GetRelationshipLinks(RelationshipAttribute relationship, IIdentifiable leftResource)
270270
{
271271
ArgumentGuard.NotNull(relationship, nameof(relationship));
272-
ArgumentGuard.NotNullNorEmpty(leftId, nameof(leftId));
272+
ArgumentGuard.NotNull(leftResource, nameof(leftResource));
273273

274274
var links = new RelationshipLinks();
275275

276276
if (ShouldIncludeRelationshipLink(LinkTypes.Self, relationship))
277277
{
278-
links.Self = GetLinkForRelationshipSelf(leftId, relationship);
278+
links.Self = GetLinkForRelationshipSelf(leftResource.StringId!, relationship);
279279
}
280280

281281
if (ShouldIncludeRelationshipLink(LinkTypes.Related, relationship))
282282
{
283-
links.Related = GetLinkForRelationshipRelated(leftId, relationship);
283+
links.Related = GetLinkForRelationshipRelated(leftResource.StringId!, relationship);
284284
}
285285

286286
return links.HasValue() ? links : null;

src/JsonApiDotNetCore/Serialization/Response/ResponseModelAdapter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected virtual ResourceObject ConvertResource(IIdentifiable resource, Resourc
204204
IImmutableSet<ResourceFieldAttribute> fieldSet = _sparseFieldSetCache.GetSparseFieldSetForSerializer(type);
205205

206206
resourceObject.Attributes = ConvertAttributes(resource, type, fieldSet);
207-
resourceObject.Links = _linkBuilder.GetResourceLinks(type, resource.StringId!);
207+
resourceObject.Links = _linkBuilder.GetResourceLinks(type, resource);
208208
resourceObject.Meta = _resourceDefinitionAccessor.GetMeta(type, resource);
209209
}
210210

@@ -292,7 +292,7 @@ private void PopulateRelationshipsInResourceObject(ResourceObjectTreeNode treeNo
292292
private void PopulateRelationshipInResourceObject(ResourceObjectTreeNode treeNode, RelationshipAttribute relationship)
293293
{
294294
SingleOrManyData<ResourceIdentifierObject> data = GetRelationshipData(treeNode, relationship);
295-
RelationshipLinks? links = _linkBuilder.GetRelationshipLinks(relationship, treeNode.Resource.StringId!);
295+
RelationshipLinks? links = _linkBuilder.GetRelationshipLinks(relationship, treeNode.Resource);
296296

297297
if (links != null || data.IsAssigned)
298298
{

test/JsonApiDotNetCoreTests/UnitTests/Links/LinkInclusionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void Applies_cascading_settings_for_resource_links(LinkTypes linksInResou
166166
var linkBuilder = new LinkBuilder(options, request, paginationContext, httpContextAccessor, linkGenerator, controllerResourceMapping);
167167

168168
// Act
169-
ResourceLinks? resourceLinks = linkBuilder.GetResourceLinks(exampleResourceType, "id");
169+
ResourceLinks? resourceLinks = linkBuilder.GetResourceLinks(exampleResourceType, new ExampleResource());
170170

171171
// Assert
172172
if (expected == LinkTypes.Self)
@@ -331,7 +331,7 @@ public void Applies_cascading_settings_for_relationship_links(LinkTypes linksInR
331331
};
332332

333333
// Act
334-
RelationshipLinks? relationshipLinks = linkBuilder.GetRelationshipLinks(relationship, "?");
334+
RelationshipLinks? relationshipLinks = linkBuilder.GetRelationshipLinks(relationship, new ExampleResource());
335335

336336
// Assert
337337
if (expected == LinkTypes.None)

test/JsonApiDotNetCoreTests/UnitTests/Serialization/Response/ResponseModelAdapterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ private sealed class FakeLinkBuilder : ILinkBuilder
595595
return null;
596596
}
597597

598-
public ResourceLinks? GetResourceLinks(ResourceType resourceType, string id)
598+
public ResourceLinks? GetResourceLinks(ResourceType resourceType, IIdentifiable resource)
599599
{
600600
return null;
601601
}
602602

603-
public RelationshipLinks? GetRelationshipLinks(RelationshipAttribute relationship, string leftId)
603+
public RelationshipLinks? GetRelationshipLinks(RelationshipAttribute relationship, IIdentifiable leftResource)
604604
{
605605
return null;
606606
}

0 commit comments

Comments
 (0)