16
16
use ApiPlatform \Core \Api \FormatMatcher ;
17
17
use ApiPlatform \Core \Api \FormatsProviderInterface ;
18
18
use ApiPlatform \Core \Exception \InvalidArgumentException ;
19
+ use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
20
+ use ApiPlatform \Core \Metadata \Resource \ToggleableOperationAttributeTrait ;
19
21
use ApiPlatform \Core \Serializer \SerializerContextBuilderInterface ;
20
22
use ApiPlatform \Core \Util \RequestAttributesExtractor ;
21
23
use Symfony \Component \HttpFoundation \Request ;
31
33
*/
32
34
final class DeserializeListener
33
35
{
36
+ use ToggleableOperationAttributeTrait;
37
+
38
+ public const OPERATION_ATTRIBUTE_KEY = 'deserialize ' ;
39
+
34
40
private $ serializer ;
35
41
private $ serializerContextBuilder ;
36
42
private $ formats = [];
@@ -40,7 +46,7 @@ final class DeserializeListener
40
46
/**
41
47
* @throws InvalidArgumentException
42
48
*/
43
- public function __construct (SerializerInterface $ serializer , SerializerContextBuilderInterface $ serializerContextBuilder , /* FormatsProviderInterface */ $ formatsProvider )
49
+ public function __construct (SerializerInterface $ serializer , SerializerContextBuilderInterface $ serializerContextBuilder , /* FormatsProviderInterface */ $ formatsProvider, ResourceMetadataFactoryInterface $ resourceMetadataFactory = null )
44
50
{
45
51
$ this ->serializer = $ serializer ;
46
52
$ this ->serializerContextBuilder = $ serializerContextBuilder ;
@@ -54,6 +60,7 @@ public function __construct(SerializerInterface $serializer, SerializerContextBu
54
60
55
61
$ this ->formatsProvider = $ formatsProvider ;
56
62
}
63
+ $ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
57
64
}
58
65
59
66
/**
@@ -69,18 +76,12 @@ public function onKernelRequest(GetResponseEvent $event): void
69
76
|| $ request ->isMethodSafe (false )
70
77
|| !($ attributes = RequestAttributesExtractor::extractAttributes ($ request ))
71
78
|| !$ attributes ['receive ' ]
72
- || (
73
- '' === ($ requestContent = $ request ->getContent ())
74
- && ('POST ' === $ method || 'PUT ' === $ method )
75
- )
79
+ || $ this ->isOperationAttributeDisabled ($ attributes , self ::OPERATION_ATTRIBUTE_KEY )
76
80
) {
77
81
return ;
78
82
}
79
83
80
84
$ context = $ this ->serializerContextBuilder ->createFromRequest ($ request , false , $ attributes );
81
- if (isset ($ context ['input ' ]) && \array_key_exists ('class ' , $ context ['input ' ]) && null === $ context ['input ' ]['class ' ]) {
82
- return ;
83
- }
84
85
85
86
// BC check to be removed in 3.0
86
87
if (null !== $ this ->formatsProvider ) {
@@ -96,9 +97,7 @@ public function onKernelRequest(GetResponseEvent $event): void
96
97
97
98
$ request ->attributes ->set (
98
99
'data ' ,
99
- $ this ->serializer ->deserialize (
100
- $ requestContent , $ context ['resource_class ' ], $ format , $ context
101
- )
100
+ $ this ->serializer ->deserialize ($ request ->getContent (), $ context ['resource_class ' ], $ format , $ context )
102
101
);
103
102
}
104
103
0 commit comments