Skip to content

Commit 2f5d684

Browse files
committed
chore(Deserializer): clean up
1 parent fdeef8e commit 2f5d684

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,21 @@ private object SetHasOneRelationship(object entity,
204204
if (relationships.TryGetValue(relationshipName, out RelationshipData relationshipData) == false)
205205
return entity;
206206

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-
213207
var rio = (ResourceIdentifierObject)relationshipData.ExposedData;
214208

215209
var foreignKey = attr.IdentifiablePropertyName;
216210
var foreignKeyProperty = entityProperties.FirstOrDefault(p => p.Name == foreignKey);
217-
218211
if (foreignKeyProperty == null && rio == null)
219212
return entity;
220213

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+
{
221222
var foreignKeyPropertyValue = rio?.Id ?? null;
222223
if (foreignKeyProperty != null)
223224
{
@@ -227,30 +228,35 @@ private object SetHasOneRelationship(object entity,
227228
// e.g. PATCH /articles
228229
// {... { "relationships":{ "Owner": { "data": null } } } }
229230
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.");
231232

232233
var convertedValue = TypeHelper.ConvertType(foreignKeyPropertyValue, foreignKeyProperty.PropertyType);
233234
foreignKeyProperty.SetValue(entity, convertedValue);
234-
_jsonApiContext.RelationshipsToUpdate[relationshipAttr] = convertedValue;
235+
_jsonApiContext.RelationshipsToUpdate[hasOneAttr] = convertedValue;
235236
}
237+
}
236238

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)
240248
{
241249
// we have now set the FK property on the resource, now we need to check to see if the
242250
// 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);
244252
if (includedRelationshipObject != null)
245-
relationshipAttr.SetValue(entity, includedRelationshipObject);
253+
hasOneAttr.SetValue(entity, includedRelationshipObject);
246254

247255
// we need to store the fact that this relationship was included in the payload
248256
// for EF, the repository will use these pointers to make ensure we don't try to
249257
// 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);
251259
}
252-
253-
return entity;
254260
}
255261

256262
private object SetHasManyRelationship(object entity,

0 commit comments

Comments
 (0)