1616use Symfony \Component \HttpKernel \Exception \UnsupportedMediaTypeHttpException ;
1717use Symfony \Component \OptionsResolver \OptionsResolver ;
1818use Symfony \Component \Serializer \Exception \Exception as SymfonySerializerException ;
19- use Symfony \Component \Validator \ValidatorInterface ;
19+ use Symfony \Component \Validator \ValidatorInterface as LegacyValidatorInterface ;
20+ use Symfony \Component \Validator \Validator \ValidatorInterface ;
2021use Sensio \Bundle \FrameworkExtraBundle \Request \ParamConverter \ParamConverterInterface ;
2122use Sensio \Bundle \FrameworkExtraBundle \Configuration \ParamConverter ;
2223use JMS \Serializer \Exception \UnsupportedFormatException ;
@@ -45,7 +46,7 @@ abstract class AbstractRequestBodyParamConverter implements ParamConverterInterf
4546 * @param array|null $groups An array of groups to be used in the serialization context
4647 * @param string|null $version A version string to be used in the serialization context
4748 * @param object $serializer
48- * @param ValidatorInterface $validator
49+ * @param LegacyValidatorInterface| ValidatorInterface $validator
4950 * @param string|null $validationErrorsArgument
5051 *
5152 * @throws \InvalidArgumentException
@@ -54,7 +55,7 @@ public function __construct(
5455 $ serializer ,
5556 $ groups = null ,
5657 $ version = null ,
57- ValidatorInterface $ validator = null ,
58+ $ validator = null ,
5859 $ validationErrorsArgument = null
5960 ) {
6061 $ this ->serializer = $ serializer ;
@@ -67,6 +68,15 @@ public function __construct(
6768 $ this ->context ['version ' ] = $ version ;
6869 }
6970
71+ if ($ validator !== null && !$ validator instanceof LegacyValidatorInterface && !$ validator instanceof ValidatorInterface) {
72+ throw new \InvalidArgumentException (sprintf (
73+ 'Validator has expected to be an instance of %s or %s, "%s" given ' ,
74+ 'Symfony\Component\Validator\ValidatorInterface ' ,
75+ 'Symfony\Component\Validator\Validator\ValidatorInterface ' ,
76+ get_class ($ validator )
77+ ));
78+ }
79+
7080 if (null !== $ validator && null === $ validationErrorsArgument ) {
7181 throw new \InvalidArgumentException ('"$validationErrorsArgument" cannot be null when using the validator ' );
7282 }
@@ -119,14 +129,21 @@ protected function execute(Request $request, ParamConverter $configuration)
119129
120130 if (null !== $ this ->validator ) {
121131 $ validatorOptions = $ this ->getValidatorOptions ($ options );
122- $ request ->attributes ->set (
123- $ this ->validationErrorsArgument ,
124- $ this ->validator ->validate (
132+
133+ if ($ this ->validator instanceof ValidatorInterface) {
134+ $ errors = $ this ->validator ->validate ($ object , null , $ validatorOptions ['groups ' ]);
135+ } else {
136+ $ errors = $ this ->validator ->validate (
125137 $ object ,
126138 $ validatorOptions ['groups ' ],
127139 $ validatorOptions ['traverse ' ],
128140 $ validatorOptions ['deep ' ]
129- )
141+ );
142+ }
143+
144+ $ request ->attributes ->set (
145+ $ this ->validationErrorsArgument ,
146+ $ errors
130147 );
131148 }
132149
@@ -176,3 +193,4 @@ protected function getValidatorOptions(array $options)
176193 return $ resolver ->resolve (isset ($ options ['validator ' ]) ? $ options ['validator ' ] : array ());
177194 }
178195}
196+
0 commit comments