13
13
14
14
namespace ApiPlatform \Core \Serializer ;
15
15
16
+ use ApiPlatform \Core \Api \IriConverterInterface ;
17
+ use ApiPlatform \Core \Api \ResourceClassResolverInterface ;
18
+ use ApiPlatform \Core \DataProvider \ItemDataProviderInterface ;
16
19
use ApiPlatform \Core \Exception \InvalidArgumentException ;
20
+ use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
21
+ use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
22
+ use Psr \Log \LoggerInterface ;
23
+ use Psr \Log \NullLogger ;
24
+ use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
25
+ use Symfony \Component \Serializer \Mapping \Factory \ClassMetadataFactoryInterface ;
26
+ use Symfony \Component \Serializer \NameConverter \NameConverterInterface ;
17
27
18
28
/**
19
29
* Generic item normalizer.
24
34
*/
25
35
class ItemNormalizer extends AbstractItemNormalizer
26
36
{
37
+ private $ logger ;
38
+
39
+ public function __construct (PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , IriConverterInterface $ iriConverter , ResourceClassResolverInterface $ resourceClassResolver , PropertyAccessorInterface $ propertyAccessor = null , NameConverterInterface $ nameConverter = null , ClassMetadataFactoryInterface $ classMetadataFactory = null , ItemDataProviderInterface $ itemDataProvider = null , bool $ allowPlainIdentifiers = false , LoggerInterface $ logger = null )
40
+ {
41
+ parent ::__construct ($ propertyNameCollectionFactory , $ propertyMetadataFactory , $ iriConverter , $ resourceClassResolver , $ propertyAccessor , $ nameConverter , $ classMetadataFactory , $ itemDataProvider , $ allowPlainIdentifiers );
42
+
43
+ $ this ->logger = $ logger ?: new NullLogger ();
44
+ }
45
+
27
46
/**
28
47
* {@inheritdoc}
29
48
*
@@ -32,12 +51,19 @@ class ItemNormalizer extends AbstractItemNormalizer
32
51
public function denormalize ($ data , $ class , $ format = null , array $ context = [])
33
52
{
34
53
// Avoid issues with proxies if we populated the object
35
- if (isset ($ data ['id ' ]) && !isset ($ context [self ::OBJECT_TO_POPULATE ]) && isset ( $ context [ ' resource_class ' ]) ) {
54
+ if (isset ($ data ['id ' ]) && !isset ($ context [self ::OBJECT_TO_POPULATE ])) {
36
55
if (isset ($ context ['api_allow_update ' ]) && true !== $ context ['api_allow_update ' ]) {
37
56
throw new InvalidArgumentException ('Update is not allowed for this operation. ' );
38
57
}
39
58
40
- $ this ->updateObjectToPopulate ($ data , $ context );
59
+ if (isset ($ context ['resource_class ' ])) {
60
+ $ this ->updateObjectToPopulate ($ data , $ context );
61
+ } else {
62
+ // See https://github.com/api-platform/core/pull/2326 to understand this message.
63
+ $ this ->logger ->warning ('The "resource_class" key is missing from the context. ' , [
64
+ 'context ' => $ context ,
65
+ ]);
66
+ }
41
67
}
42
68
43
69
return parent ::denormalize ($ data , $ class , $ format , $ context );
0 commit comments