Skip to content

Commit d7a6556

Browse files
committed
fix(link-builder): do not include entity name in base path
the entity name is used to determine the end of the base path for example /api/v1/my-entities/1 the base path should just be /api/v1
1 parent 494191c commit d7a6556

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
8383
{
8484
Links = new Links
8585
{
86-
Self = linkBuilder.GetSelfRelationLink(entity.Id.ToString(), r.RelationshipName),
87-
Related = linkBuilder.GetRelatedRelationLink(entity.Id.ToString(), r.RelationshipName)
86+
Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName),
87+
Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName)
8888
}
8989
};
9090

src/JsonApiDotNetCore/Builders/LinkBuilder.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,24 @@ private string GetNamespaceFromPath(string path, string entityName)
2626
var segments = path.Split('/');
2727

2828
for(var i = 1; i < segments.Length; i++)
29-
nSpace += $"/{segments[i].Dasherize()}";
29+
{
30+
if(segments[i].ToLower() == entityName.Dasherize())
31+
break;
3032

33+
nSpace += $"/{segments[i].Dasherize()}";
34+
}
35+
3136
return nSpace;
3237
}
3338

34-
public string GetSelfRelationLink(string parentId, string child)
39+
public string GetSelfRelationLink(string parent, string parentId, string child)
3540
{
36-
return $"{_context.BasePath}/{parentId}/relationships/{child.Dasherize()}";
41+
return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/relationships/{child.Dasherize()}";
3742
}
3843

39-
public string GetRelatedRelationLink(string parentId, string child)
44+
public string GetRelatedRelationLink(string parent, string parentId, string child)
4045
{
41-
return $"{_context.BasePath}/{parentId}/{child.Dasherize()}";
46+
return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/{child.Dasherize()}";
4247
}
4348
}
4449
}

0 commit comments

Comments
 (0)