2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
4
using System . Reflection ;
5
+ using JsonApiDotNetCore . Extensions ;
5
6
using JsonApiDotNetCore . Internal ;
6
7
using JsonApiDotNetCore . Internal . Generics ;
7
8
using JsonApiDotNetCore . Models ;
@@ -15,14 +16,20 @@ namespace JsonApiDotNetCore.Serialization
15
16
public class JsonApiDeSerializer : IJsonApiDeSerializer
16
17
{
17
18
private readonly IJsonApiContext _jsonApiContext ;
18
- private readonly IGenericProcessorFactory _genericProcessorFactory ;
19
19
20
+ [ Obsolete (
21
+ "The deserializer no longer depends on the IGenericProcessorFactory" ,
22
+ error : false ) ]
20
23
public JsonApiDeSerializer (
21
24
IJsonApiContext jsonApiContext ,
22
25
IGenericProcessorFactory genericProcessorFactory )
23
26
{
24
27
_jsonApiContext = jsonApiContext ;
25
- _genericProcessorFactory = genericProcessorFactory ;
28
+ }
29
+
30
+ public JsonApiDeSerializer ( IJsonApiContext jsonApiContext )
31
+ {
32
+ _jsonApiContext = jsonApiContext ;
26
33
}
27
34
28
35
public object Deserialize ( string requestBody )
@@ -200,20 +207,25 @@ private object SetHasOneRelationship(object entity,
200
207
201
208
var rio = ( ResourceIdentifierObject ) relationshipData . ExposedData ;
202
209
203
- if ( rio == null ) return entity ;
204
-
205
- var newValue = rio . Id ;
206
-
207
210
var foreignKey = attr . IdentifiablePropertyName ;
208
211
var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == foreignKey ) ;
209
- if ( entityProperty == null )
212
+ if ( entityProperty == null && rio != null )
210
213
throw new JsonApiException ( 400 , $ "{ contextEntity . EntityType . Name } does not contain a foreign key property '{ foreignKey } ' for has one relationship '{ attr . InternalRelationshipName } '") ;
211
214
212
- var convertedValue = TypeHelper . ConvertType ( newValue , entityProperty . PropertyType ) ;
215
+ if ( entityProperty != null )
216
+ {
217
+ // e.g. PATCH /articles
218
+ // {... { "relationships":{ "Owner": { "data" :null } } } }
219
+ if ( rio == null && Nullable . GetUnderlyingType ( entityProperty . PropertyType ) == null )
220
+ throw new JsonApiException ( 400 , $ "Cannot set required relationship identifier '{ attr . IdentifiablePropertyName } ' to null.") ;
213
221
214
- _jsonApiContext . RelationshipsToUpdate [ relationshipAttr ] = convertedValue ;
222
+ var newValue = rio ? . Id ?? null ;
223
+ var convertedValue = TypeHelper . ConvertType ( newValue , entityProperty . PropertyType ) ;
215
224
216
- entityProperty . SetValue ( entity , convertedValue ) ;
225
+ _jsonApiContext . RelationshipsToUpdate [ relationshipAttr ] = convertedValue ;
226
+
227
+ entityProperty . SetValue ( entity , convertedValue ) ;
228
+ }
217
229
}
218
230
219
231
return entity ;
@@ -225,11 +237,6 @@ private object SetHasManyRelationship(object entity,
225
237
ContextEntity contextEntity ,
226
238
Dictionary < string , RelationshipData > relationships )
227
239
{
228
- var entityProperty = entityProperties . FirstOrDefault ( p => p . Name == attr . InternalRelationshipName ) ;
229
-
230
- if ( entityProperty == null )
231
- throw new JsonApiException ( 400 , $ "{ contextEntity . EntityType . Name } does not contain an relationsip named { attr . InternalRelationshipName } ") ;
232
-
233
240
var relationshipName = attr . PublicRelationshipName ;
234
241
235
242
if ( relationships . TryGetValue ( relationshipName , out RelationshipData relationshipData ) )
@@ -238,11 +245,18 @@ private object SetHasManyRelationship(object entity,
238
245
239
246
if ( data == null ) return entity ;
240
247
241
- var genericProcessor = _genericProcessorFactory . GetProcessor < IGenericProcessor > ( typeof ( GenericProcessor < > ) , attr . Type ) ;
248
+ var relationshipShells = relationshipData . ManyData . Select ( r =>
249
+ {
250
+ var instance = attr . Type . New < IIdentifiable > ( ) ;
251
+ instance . StringId = r . Id ;
252
+ return instance ;
253
+ } ) ;
254
+
255
+ var convertedCollection = TypeHelper . ConvertCollection ( relationshipShells , attr . Type ) ;
242
256
243
- var ids = relationshipData . ManyData . Select ( r => r . Id ) ;
257
+ attr . SetValue ( entity , convertedCollection ) ;
244
258
245
- genericProcessor . SetRelationships ( entity , attr , ids ) ;
259
+ _jsonApiContext . HasManyRelationshipPointers . Add ( attr . Type , convertedCollection ) ;
246
260
}
247
261
248
262
return entity ;
0 commit comments