@@ -41,16 +41,16 @@ private static object DataToObject(DocumentData data, IJsonApiContext context)
4141
4242 var contextEntity = context . ContextGraph . GetContextEntity ( entityTypeName ) ;
4343 context . RequestEntity = contextEntity ;
44-
44+
4545 var entity = Activator . CreateInstance ( contextEntity . EntityType ) ;
46-
46+
4747 entity = _setEntityAttributes ( entity , contextEntity , data . Attributes ) ;
4848 entity = _setRelationships ( entity , contextEntity , data . Relationships ) ;
4949
5050 var identifiableEntity = ( IIdentifiable ) entity ;
5151
52- if ( data . Id != null )
53- identifiableEntity . Id = Convert . ChangeType ( data . Id , identifiableEntity . Id . GetType ( ) ) ;
52+ if ( data . Id != null )
53+ identifiableEntity . Id = ChangeType ( data . Id , identifiableEntity . Id . GetType ( ) ) ;
5454
5555 return identifiableEntity ;
5656 }
@@ -70,7 +70,7 @@ private static object _setEntityAttributes(
7070 object newValue ;
7171 if ( attributeValues . TryGetValue ( attr . PublicAttributeName . Dasherize ( ) , out newValue ) )
7272 {
73- var convertedValue = Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
73+ var convertedValue = ChangeType ( newValue , entityProperty . PropertyType ) ;
7474 entityProperty . SetValue ( entity , convertedValue ) ;
7575 }
7676 }
@@ -92,22 +92,37 @@ private static object _setRelationships(
9292
9393 if ( entityProperty == null )
9494 throw new JsonApiException ( "400" , $ "{ contextEntity . EntityType . Name } does not contain an relationsip named { attr . RelationshipName } ") ;
95-
95+
9696 var relationshipName = attr . RelationshipName . Dasherize ( ) ;
9797 RelationshipData relationshipData ;
9898 if ( relationships . TryGetValue ( relationshipName , out relationshipData ) )
9999 {
100100 var data = ( Dictionary < string , string > ) relationshipData . ExposedData ;
101-
102- if ( data == null ) continue ;
103-
101+
102+ if ( data == null ) continue ;
103+
104104 var newValue = data [ "id" ] ;
105- var convertedValue = Convert . ChangeType ( newValue , entityProperty . PropertyType ) ;
105+ var convertedValue = ChangeType ( newValue , entityProperty . PropertyType ) ;
106106 entityProperty . SetValue ( entity , convertedValue ) ;
107107 }
108108 }
109109
110110 return entity ;
111111 }
112+
113+ private static object ChangeType ( object value , Type conversion )
114+ {
115+ var t = conversion ;
116+
117+ if ( t . GetTypeInfo ( ) . IsGenericType && t . GetGenericTypeDefinition ( ) . Equals ( typeof ( Nullable < > ) ) )
118+ {
119+ if ( value == null )
120+ return null ;
121+
122+ t = Nullable . GetUnderlyingType ( t ) ;
123+ }
124+
125+ return Convert . ChangeType ( value , t ) ;
126+ }
112127 }
113128}
0 commit comments