@@ -69,14 +69,14 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
69
69
continue ;
70
70
}
71
71
72
- if (!$ type = $ argument ->getType ()) {
72
+ if (!$ argument ->getType ()) {
73
73
throw new \LogicException (sprintf ('Could not resolve the "$%s" controller argument: argument should be typed. ' , $ argument ->getName ()));
74
74
}
75
75
76
76
if ($ this ->validator ) {
77
77
$ violations = new ConstraintViolationList ();
78
78
try {
79
- $ payload = $ this ->$ payloadMapper ($ request , $ type , $ attributes [0 ]);
79
+ $ payload = $ this ->$ payloadMapper ($ request , $ argument , $ attributes [0 ]);
80
80
} catch (PartialDenormalizationException $ e ) {
81
81
$ trans = $ this ->translator ? $ this ->translator ->trans (...) : fn ($ m , $ p ) => strtr ($ m , $ p );
82
82
foreach ($ e ->getErrors () as $ error ) {
@@ -92,15 +92,15 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
92
92
}
93
93
94
94
if (null !== $ payload ) {
95
- $ violations ->addAll ($ this ->validator ->validate ($ payload ));
95
+ $ violations ->addAll ($ this ->validator ->validate ($ payload, null , $ attributes [ 0 ]-> groups ?? null ));
96
96
}
97
97
98
98
if (\count ($ violations )) {
99
99
throw new HttpException ($ validationFailedCode , implode ("\n" , array_map (static fn ($ e ) => $ e ->getMessage (), iterator_to_array ($ violations ))), new ValidationFailedException ($ payload , $ violations ));
100
100
}
101
101
} else {
102
102
try {
103
- $ payload = $ this ->$ payloadMapper ($ request , $ type , $ attributes [0 ]);
103
+ $ payload = $ this ->$ payloadMapper ($ request , $ argument , $ attributes [0 ]);
104
104
} catch (PartialDenormalizationException $ e ) {
105
105
throw new HttpException ($ validationFailedCode , implode ("\n" , array_map (static fn ($ e ) => $ e ->getMessage (), $ e ->getErrors ())), $ e );
106
106
}
@@ -114,19 +114,19 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
114
114
return [];
115
115
}
116
116
117
- private function mapQueryString (Request $ request , string $ type , MapQueryString $ attribute ): ?object
117
+ private function mapQueryString (Request $ request , ArgumentMetadata $ argument , MapQueryString $ attribute ): ?object
118
118
{
119
119
if (!$ data = $ request ->query ->all ()) {
120
120
return null ;
121
121
}
122
122
123
- return $ this ->serializer ->denormalize ($ data , $ type , null , self ::CONTEXT_DENORMALIZE + $ attribute ->context );
123
+ return $ this ->serializer ->denormalize ($ data , $ argument -> getType () , null , self ::CONTEXT_DENORMALIZE + $ attribute ->context );
124
124
}
125
125
126
- private function mapRequestPayload (Request $ request , string $ type , MapRequestPayload $ attribute ): ?object
126
+ private function mapRequestPayload (Request $ request , ArgumentMetadata $ argument , MapRequestPayload $ attribute ): ?object
127
127
{
128
128
if ($ data = $ request ->request ->all ()) {
129
- return $ this ->serializer ->denormalize ($ data , $ type , null , self ::CONTEXT_DENORMALIZE + $ attribute ->context );
129
+ return $ this ->serializer ->denormalize ($ data , $ argument -> getType () , null , self ::CONTEXT_DENORMALIZE + $ attribute ->context );
130
130
}
131
131
132
132
if ('' === $ data = $ request ->getContent ()) {
@@ -137,12 +137,17 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
137
137
throw new HttpException (Response::HTTP_UNSUPPORTED_MEDIA_TYPE , 'Unsupported format. ' );
138
138
}
139
139
140
+ $ acceptedFormats = is_string ($ attribute ->format ) ? [$ attribute ->format ] : $ attribute ->format ;
141
+ if (is_array ($ attribute ->format ) && !in_array ($ format , $ acceptedFormats , true )) {
142
+ throw new HttpException (Response::HTTP_UNSUPPORTED_MEDIA_TYPE , sprintf ('Unsupported format, expects "%s", but "%s" given. ' , implode ('", " ' , $ acceptedFormats ), $ format ));
143
+ }
144
+
140
145
if ('form ' === $ format ) {
141
146
throw new HttpException (Response::HTTP_BAD_REQUEST , 'Request payload contains invalid "form" data. ' );
142
147
}
143
148
144
149
try {
145
- return $ this ->serializer ->deserialize ($ data , $ type , $ format , self ::CONTEXT_DESERIALIZE + $ attribute ->context );
150
+ return $ this ->serializer ->deserialize ($ data , $ argument -> getType () , $ format , self ::CONTEXT_DESERIALIZE + $ attribute ->context );
146
151
} catch (UnsupportedFormatException $ e ) {
147
152
throw new HttpException (Response::HTTP_UNSUPPORTED_MEDIA_TYPE , sprintf ('Unsupported format: "%s". ' , $ format ), $ e );
148
153
} catch (NotEncodableValueException $ e ) {
0 commit comments