@@ -204,20 +204,21 @@ private object SetHasOneRelationship(object entity,
204
204
if ( relationships . TryGetValue ( relationshipName , out RelationshipData relationshipData ) == false )
205
205
return entity ;
206
206
207
- var relationshipAttr = _jsonApiContext . RequestEntity . Relationships
208
- . SingleOrDefault ( r => r . PublicRelationshipName == relationshipName ) ;
209
-
210
- if ( relationshipAttr == null )
211
- throw new JsonApiException ( 400 , $ "{ _jsonApiContext . RequestEntity . EntityName } does not contain a relationship '{ relationshipName } '") ;
212
-
213
207
var rio = ( ResourceIdentifierObject ) relationshipData . ExposedData ;
214
208
215
209
var foreignKey = attr . IdentifiablePropertyName ;
216
210
var foreignKeyProperty = entityProperties . FirstOrDefault ( p => p . Name == foreignKey ) ;
217
-
218
211
if ( foreignKeyProperty == null && rio == null )
219
212
return entity ;
220
213
214
+ SetHasOneForeignKeyValue ( entity , attr , foreignKeyProperty , rio ) ;
215
+ SetHasOneNavigationPropertyValue ( entity , attr , rio , included ) ;
216
+
217
+ return entity ;
218
+ }
219
+
220
+ private void SetHasOneForeignKeyValue ( object entity , HasOneAttribute hasOneAttr , PropertyInfo foreignKeyProperty , ResourceIdentifierObject rio )
221
+ {
221
222
var foreignKeyPropertyValue = rio ? . Id ?? null ;
222
223
if ( foreignKeyProperty != null )
223
224
{
@@ -227,30 +228,35 @@ private object SetHasOneRelationship(object entity,
227
228
// e.g. PATCH /articles
228
229
// {... { "relationships":{ "Owner": { "data": null } } } }
229
230
if ( rio == null && Nullable . GetUnderlyingType ( foreignKeyProperty . PropertyType ) == null )
230
- throw new JsonApiException ( 400 , $ "Cannot set required relationship identifier '{ attr . IdentifiablePropertyName } ' to null because it is a non-nullable type.") ;
231
+ throw new JsonApiException ( 400 , $ "Cannot set required relationship identifier '{ hasOneAttr . IdentifiablePropertyName } ' to null because it is a non-nullable type.") ;
231
232
232
233
var convertedValue = TypeHelper . ConvertType ( foreignKeyPropertyValue , foreignKeyProperty . PropertyType ) ;
233
234
foreignKeyProperty . SetValue ( entity , convertedValue ) ;
234
- _jsonApiContext . RelationshipsToUpdate [ relationshipAttr ] = convertedValue ;
235
+ _jsonApiContext . RelationshipsToUpdate [ hasOneAttr ] = convertedValue ;
235
236
}
237
+ }
236
238
237
- if ( rio != null
238
- // if the resource identifier is null, there should be no reason to instantiate an instance
239
- && rio . Id != null )
239
+ /// <summary>
240
+ /// Sets the value of the navigation property for the related resource.
241
+ /// If the resource has been included, all attributes will be set.
242
+ /// If the resource has not been included, only the id will be set.
243
+ /// </summary>
244
+ private void SetHasOneNavigationPropertyValue ( object entity , HasOneAttribute hasOneAttr , ResourceIdentifierObject rio , List < DocumentData > included )
245
+ {
246
+ // if the resource identifier is null, there should be no reason to instantiate an instance
247
+ if ( rio != null && rio . Id != null )
240
248
{
241
249
// we have now set the FK property on the resource, now we need to check to see if the
242
250
// related entity was included in the payload and update its attributes
243
- var includedRelationshipObject = GetIncludedRelationship ( rio , included , relationshipAttr ) ;
251
+ var includedRelationshipObject = GetIncludedRelationship ( rio , included , hasOneAttr ) ;
244
252
if ( includedRelationshipObject != null )
245
- relationshipAttr . SetValue ( entity , includedRelationshipObject ) ;
253
+ hasOneAttr . SetValue ( entity , includedRelationshipObject ) ;
246
254
247
255
// we need to store the fact that this relationship was included in the payload
248
256
// for EF, the repository will use these pointers to make ensure we don't try to
249
257
// create resources if they already exist, we just need to create the relationship
250
- _jsonApiContext . HasOneRelationshipPointers . Add ( attr , includedRelationshipObject ) ;
258
+ _jsonApiContext . HasOneRelationshipPointers . Add ( hasOneAttr , includedRelationshipObject ) ;
251
259
}
252
-
253
- return entity ;
254
260
}
255
261
256
262
private object SetHasManyRelationship ( object entity ,
0 commit comments