Skip to content

Commit 3914e52

Browse files
committed
check if relationships are null prior to adding them
1 parent 22a7248 commit 3914e52

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

JsonApiDotNetCore/Abstractions/ModelAccessor.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,25 @@ public static object SetValuesOnModelInstance(object model, Dictionary<string, o
3737
});
3838
}
3939

40-
foreach (var relationship in jsonApiRelationships)
41-
{
42-
var relationshipName = relationship.Key;
43-
var relationshipId = ((JObject) relationship.Value)["data"]["id"];
44-
var relationshipPropertyName = $"{relationshipName}Id";
45-
46-
modelProperties.ForEach(pI =>
40+
if(jsonApiRelationships != null) {
41+
foreach (var relationship in jsonApiRelationships)
4742
{
48-
if (pI.Name.ToProperCase() == relationshipPropertyName.ToProperCase())
43+
var relationshipId = ((JObject) relationship.Value)["data"]["id"].ToString();
44+
var relationshipTypeName = ((JObject) relationship.Value)["data"]["type"];
45+
var relationshipPropertyName = $"{relationshipTypeName}Id";
46+
47+
modelProperties.ForEach(pI =>
4948
{
50-
var convertedValue = Convert.ChangeType(relationshipId, pI.PropertyType);
51-
pI.SetValue(model, convertedValue);
52-
}
53-
});
49+
if (pI.Name.ToProperCase() == relationshipPropertyName.ToProperCase())
50+
{
51+
var convertedValue = Convert.ChangeType(relationshipId, pI.PropertyType);
52+
pI.SetValue(model, convertedValue);
53+
}
54+
});
55+
}
5456
}
5557

58+
5659
return model;
5760
}
5861
}

0 commit comments

Comments
 (0)