Skip to content

Commit 950d9e6

Browse files
committed
PATCH relationships
1 parent d2ec13d commit 950d9e6

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

JsonApiDotNetCore/Abstractions/ModelAccessor.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,28 @@ public static Type GetTypeFromModelRelationshipName(Type modelType, string relat
2424

2525
public static object SetValuesOnModelInstance(object model, Dictionary<string, object> jsonApiAttributes, Dictionary<string, object> jsonApiRelationships)
2626
{
27-
var modelProperties = model.GetType().GetProperties().ToList();
27+
var patches = GetEntityPatch(model.GetType(), jsonApiAttributes, jsonApiRelationships);
28+
foreach(var patch in patches)
29+
{
30+
patch.Key.SetValue(model, patch.Value);
31+
}
32+
33+
return model;
34+
}
35+
36+
public static Dictionary<PropertyInfo, object> GetEntityPatch(Type modelType, Dictionary<string, object> jsonApiAttributes, Dictionary<string, object> jsonApiRelationships)
37+
{
38+
var patchDefinitions = new Dictionary<PropertyInfo, object>();
39+
40+
var modelProperties = modelType.GetProperties().ToList();
2841
foreach (var attribute in jsonApiAttributes)
2942
{
3043
modelProperties.ForEach(pI =>
3144
{
3245
if (pI.Name.ToProperCase() == attribute.Key.ToProperCase())
3346
{
3447
var convertedValue = Convert.ChangeType(attribute.Value, pI.PropertyType);
35-
pI.SetValue(model, convertedValue);
48+
patchDefinitions.Add(pI, convertedValue);
3649
}
3750
});
3851
}
@@ -49,13 +62,13 @@ public static object SetValuesOnModelInstance(object model, Dictionary<string, o
4962
if (pI.Name.ToProperCase() == relationshipPropertyName.ToProperCase())
5063
{
5164
var convertedValue = Convert.ChangeType(relationshipId, pI.PropertyType);
52-
pI.SetValue(model, convertedValue);
65+
patchDefinitions.Add(pI, convertedValue);
5366
}
5467
});
5568
}
5669
}
5770

58-
return model;
71+
return patchDefinitions;
5972
}
6073
}
6174
}

JsonApiDotNetCore/Services/JsonApiDeserializer.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,9 @@ public Dictionary<PropertyInfo, object> GetEntityPatch()
5353
{
5454
var datum = GetSignularJsonApiDatum();
5555
var attributes = datum.Attributes;
56-
// var relationships = datum.Relationships;
57-
58-
var patchDefinitions = new Dictionary<PropertyInfo, object>();
59-
60-
var modelProperties = _context.GetEntityType().GetProperties().ToList();
61-
62-
foreach (var attribute in attributes)
63-
{
64-
modelProperties.ForEach(pI =>
65-
{
66-
if (pI.Name.ToProperCase() == attribute.Key.ToProperCase())
67-
{
68-
var convertedValue = Convert.ChangeType(attribute.Value, pI.PropertyType);
69-
patchDefinitions.Add(pI, convertedValue);
70-
}
71-
});
72-
}
56+
var relationships = datum.Relationships;
7357

74-
return patchDefinitions;
58+
return ModelAccessor.GetEntityPatch(_context.GetEntityType(), attributes, relationships);
7559
}
7660

7761
}

0 commit comments

Comments
 (0)