Skip to content

Commit fdeef8e

Browse files
committed
fix(Deserializer): deserialize indpendent hasone pointers
1 parent 9c1f245 commit fdeef8e

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,21 @@ private object SetHasOneRelationship(object entity,
218218
if (foreignKeyProperty == null && rio == null)
219219
return entity;
220220

221-
if (foreignKeyProperty == null && rio != null)
222-
throw new JsonApiException(400, $"{contextEntity.EntityType.Name} does not contain a foreign key property '{foreignKey}' for has one relationship '{attr.InternalRelationshipName}'");
223-
224-
// e.g. PATCH /articles
225-
// {... { "relationships":{ "Owner": { "data": null } } } }
226-
if (rio == null && Nullable.GetUnderlyingType(foreignKeyProperty.PropertyType) == null)
227-
throw new JsonApiException(400, $"Cannot set required relationship identifier '{attr.IdentifiablePropertyName}' to null because it is a non-nullable type.");
228-
229-
var newValue = rio?.Id ?? null;
230-
var convertedValue = TypeHelper.ConvertType(newValue, foreignKeyProperty.PropertyType);
231-
232-
_jsonApiContext.RelationshipsToUpdate[relationshipAttr] = convertedValue;
221+
var foreignKeyPropertyValue = rio?.Id ?? null;
222+
if (foreignKeyProperty != null)
223+
{
224+
// in the case of the HasOne independent side of the relationship, we should still create the shell entity on the other side
225+
// we should not actually require the resource to have a foreign key (be the dependent side of the relationship)
233226

234-
foreignKeyProperty.SetValue(entity, convertedValue);
227+
// e.g. PATCH /articles
228+
// {... { "relationships":{ "Owner": { "data": null } } } }
229+
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.");
235231

232+
var convertedValue = TypeHelper.ConvertType(foreignKeyPropertyValue, foreignKeyProperty.PropertyType);
233+
foreignKeyProperty.SetValue(entity, convertedValue);
234+
_jsonApiContext.RelationshipsToUpdate[relationshipAttr] = convertedValue;
235+
}
236236

237237
if (rio != null
238238
// if the resource identifier is null, there should be no reason to instantiate an instance

0 commit comments

Comments
 (0)