@@ -122,17 +122,24 @@ public virtual async Task<object> GetRelationshipsAsync(TId id, string relations
122
122
123
123
public virtual async Task < object > GetRelationshipAsync ( TId id , string relationshipName )
124
124
{
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
+
126
132
// TODO: it would be better if we could distinguish whether or not the relationship was not found,
127
133
// vs the relationship not being set on the instance of T
128
134
if ( entity == null )
129
135
{
130
- throw new JsonApiException ( 404 , $ "Relationship { relationshipName } not found.") ;
136
+ throw new JsonApiException ( 404 , $ "Relationship ' { navigationPropertyName } ' not found.") ;
131
137
}
132
138
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
+
136
143
return relationship ;
137
144
}
138
145
@@ -242,5 +249,10 @@ private async Task<TResource> GetWithRelationshipsAsync(TId id)
242
249
private bool ShouldIncludeRelationships ( )
243
250
=> ( _jsonApiContext . QuerySet ? . IncludedRelationships != null &&
244
251
_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 ) ;
245
257
}
246
258
}
0 commit comments