Skip to content

Commit d92752b

Browse files
committed
fix(#354): transform property name
Should consider creating a new API on the graph that is more intuitive and doesn't require both steps.
1 parent 2cbf4b8 commit d92752b

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/JsonApiDotNetCore/Services/EntityResourceService.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,24 @@ public virtual async Task<object> GetRelationshipsAsync(TId id, string relations
122122

123123
public virtual async Task<object> GetRelationshipAsync(TId id, string relationshipName)
124124
{
125-
var entity = await _entities.GetAndIncludeAsync(id, relationshipName);
125+
// compound-property -> CompoundProperty
126+
var navigationPropertyName = _jsonApiContext.ContextGraph.GetRelationshipName<TResource>(relationshipName);
127+
if (navigationPropertyName == null)
128+
throw new JsonApiException(422, $"Relationship '{relationshipName}' does not exist on resource '{typeof(TResource)}'.");
129+
130+
var entity = await _entities.GetAndIncludeAsync(id, navigationPropertyName);
131+
126132
// TODO: it would be better if we could distinguish whether or not the relationship was not found,
127133
// vs the relationship not being set on the instance of T
128134
if (entity == null)
129135
{
130-
throw new JsonApiException(404, $"Relationship {relationshipName} not found.");
136+
throw new JsonApiException(404, $"Relationship '{navigationPropertyName}' not found.");
131137
}
132138

133-
var resource = (typeof(TResource) == typeof(TEntity)) ? entity as TResource :
134-
_mapper.Map<TResource>(entity);
135-
var relationship = _jsonApiContext.ContextGraph.GetRelationship(resource, relationshipName);
139+
var resource = MapOut(entity);
140+
141+
var relationship = _jsonApiContext.ContextGraph.GetRelationship(resource, navigationPropertyName);
142+
136143
return relationship;
137144
}
138145

@@ -242,5 +249,10 @@ private async Task<TResource> GetWithRelationshipsAsync(TId id)
242249
private bool ShouldIncludeRelationships()
243250
=> (_jsonApiContext.QuerySet?.IncludedRelationships != null &&
244251
_jsonApiContext.QuerySet.IncludedRelationships.Count > 0);
252+
253+
private TResource MapOut(TEntity entity)
254+
=> (typeof(TResource) == typeof(TEntity))
255+
? entity as TResource :
256+
_mapper.Map<TResource>(entity);
245257
}
246258
}

0 commit comments

Comments
 (0)