diff --git a/src/State/Provider/DeserializeProvider.php b/src/State/Provider/DeserializeProvider.php index be399f4f1bf..e7ad02c78b2 100644 --- a/src/State/Provider/DeserializeProvider.php +++ b/src/State/Provider/DeserializeProvider.php @@ -100,12 +100,13 @@ public function provide(Operation $operation, array $uriVariables = [], array $c if (!$exception instanceof NotNormalizableValueException) { continue; } - $message = (new Type($exception->getExpectedTypes() ?? []))->message; + $expectedTypes = $this->normalizeExpectedTypes($exception->getExpectedTypes()); + $message = (new Type($expectedTypes))->message; $parameters = []; if ($exception->canUseMessageForUser()) { $parameters['hint'] = $exception->getMessage(); } - $violations->add(new ConstraintViolation($this->translator->trans($message, ['{{ type }}' => implode('|', $exception->getExpectedTypes() ?? [])], 'validators'), $message, $parameters, null, $exception->getPath(), null, null, (string) Type::INVALID_TYPE_ERROR)); + $violations->add(new ConstraintViolation($this->translator->trans($message, ['{{ type }}' => implode('|', $expectedTypes)], 'validators'), $message, $parameters, null, $exception->getPath(), null, null, (string) Type::INVALID_TYPE_ERROR)); } if (0 !== \count($violations)) { throw new ValidationException($violations); @@ -114,4 +115,22 @@ public function provide(Operation $operation, array $uriVariables = [], array $c return $data; } + + private function normalizeExpectedTypes(?array $expectedTypes = null): array + { + $normalizedTypes = []; + + foreach ($expectedTypes ?? [] as $expectedType) { + $normalizedType = $expectedType; + + if (class_exists($expectedType) || interface_exists($expectedType)) { + $classReflection = new \ReflectionClass($expectedType); + $normalizedType = $classReflection->getShortName(); + } + + $normalizedTypes[] = $normalizedType; + } + + return $normalizedTypes; + } }