77using JsonApiDotNetCore . Models ;
88using JsonApiDotNetCore . Services ;
99using Newtonsoft . Json ;
10+ using Newtonsoft . Json . Linq ;
1011
1112namespace JsonApiDotNetCore . Serialization
1213{
@@ -23,14 +24,17 @@ public static object Deserialize(string requestBody, IJsonApiContext context)
2324
2425 var entity = Activator . CreateInstance ( contextEntity . EntityType ) ;
2526
26- return _setEntityAttributes ( entity , contextEntity , document . Data . Attributes ) ;
27+ entity = _setEntityAttributes ( entity , contextEntity , document . Data . Attributes ) ;
28+ entity = _setRelationships ( entity , contextEntity , document . Data . Relationships ) ;
29+
30+ return entity ;
2731 }
2832
2933 private static object _setEntityAttributes (
3034 object entity , ContextEntity contextEntity , Dictionary < string , object > attributeValues )
3135 {
3236 var entityProperties = entity . GetType ( ) . GetProperties ( ) ;
33-
37+
3438 foreach ( var attr in contextEntity . Attributes )
3539 {
3640 var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == attr . InternalAttributeName ) ;
@@ -39,10 +43,38 @@ private static object _setEntityAttributes(
3943 throw new ArgumentException ( $ "{ contextEntity . EntityType . Name } does not contain an attribute named { attr . InternalAttributeName } ", nameof ( entity ) ) ;
4044
4145 object newValue ;
42- if ( attributeValues . TryGetValue ( attr . PublicAttributeName , out newValue ) )
46+ if ( attributeValues . TryGetValue ( attr . PublicAttributeName . Dasherize ( ) , out newValue ) )
47+ {
48+ var convertedValue = Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
49+ entityProperty . SetValue ( entity , convertedValue ) ;
50+ }
51+ }
52+
53+ return entity ;
54+ }
55+
56+ private static object _setRelationships (
57+ object entity , ContextEntity contextEntity , Dictionary < string , Dictionary < string , object > > relationships )
58+ {
59+ if ( relationships == null )
60+ return entity ;
61+
62+ var entityProperties = entity . GetType ( ) . GetProperties ( ) ;
63+
64+ foreach ( var attr in contextEntity . Relationships )
65+ {
66+ var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == $ "{ attr . RelationshipName } Id") ;
67+
68+ if ( entityProperty == null )
69+ throw new ArgumentException ( $ "{ contextEntity . EntityType . Name } does not contain an relationsip named { attr . RelationshipName } ", nameof ( entity ) ) ;
70+
71+ Dictionary < string , object > relationshipData ;
72+ if ( relationships . TryGetValue ( attr . RelationshipName . Dasherize ( ) , out relationshipData ) )
4373 {
44- Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
45- entityProperty . SetValue ( entity , newValue ) ;
74+ var data = ( ( JObject ) relationshipData [ "data" ] ) . ToObject < Dictionary < string , string > > ( ) ;
75+ var newValue = data [ "id" ] ;
76+ var convertedValue = Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
77+ entityProperty . SetValue ( entity , convertedValue ) ;
4678 }
4779 }
4880
0 commit comments