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 )
@@ -225,11 +232,6 @@ private object SetHasManyRelationship(object entity,
225
232
ContextEntity contextEntity ,
226
233
Dictionary < string , RelationshipData > relationships )
227
234
{
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
235
var relationshipName = attr . PublicRelationshipName ;
234
236
235
237
if ( relationships . TryGetValue ( relationshipName , out RelationshipData relationshipData ) )
@@ -238,11 +240,18 @@ private object SetHasManyRelationship(object entity,
238
240
239
241
if ( data == null ) return entity ;
240
242
241
- var genericProcessor = _genericProcessorFactory . GetProcessor < IGenericProcessor > ( typeof ( GenericProcessor < > ) , attr . Type ) ;
243
+ var relationshipShells = relationshipData . ManyData . Select ( r =>
244
+ {
245
+ var instance = attr . Type . New < IIdentifiable > ( ) ;
246
+ instance . StringId = r . Id ;
247
+ return instance ;
248
+ } ) ;
249
+
250
+ var convertedCollection = TypeHelper . ConvertCollection ( relationshipShells , attr . Type ) ;
242
251
243
- var ids = relationshipData . ManyData . Select ( r => r . Id ) ;
252
+ attr . SetValue ( entity , convertedCollection ) ;
244
253
245
- genericProcessor . SetRelationships ( entity , attr , ids ) ;
254
+ _jsonApiContext . HasManyRelationshipPointers . Add ( attr . Type , convertedCollection ) ;
246
255
}
247
256
248
257
return entity ;
0 commit comments