Skip to content

Commit 51bc4ba

Browse files
committed
SerializedName used in message in ValidationExceptionNormalizer
1 parent b60ad3d commit 51bc4ba

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Bridge/Symfony/Bundle/Resources/config/graphql.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
</service>
235235

236236
<service id="api_platform.graphql.normalizer.validation_exception" class="ApiPlatform\Core\GraphQl\Serializer\Exception\ValidationExceptionNormalizer">
237+
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
237238
<tag name="serializer.normalizer" priority="-780" />
238239
</service>
239240

src/GraphQl/Serializer/Exception/ValidationExceptionNormalizer.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use GraphQL\Error\Error;
1818
use GraphQL\Error\FormattedError;
1919
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2021
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2122
use Symfony\Component\Validator\ConstraintViolation;
2223

@@ -30,6 +31,13 @@
3031
*/
3132
final class ValidationExceptionNormalizer implements NormalizerInterface
3233
{
34+
private $nameConverter;
35+
36+
public function __construct(NameConverterInterface $nameConverter = null)
37+
{
38+
$this->nameConverter = $nameConverter;
39+
}
40+
3341
/**
3442
* {@inheritdoc}
3543
*/
@@ -38,7 +46,7 @@ public function normalize($object, $format = null, array $context = []): array
3846
/** @var ValidationException */
3947
$validationException = $object->getPrevious();
4048
$error = FormattedError::createFromException($object);
41-
$error['message'] = $validationException->getMessage();
49+
$error['message'] = $this->normalizeValidationException($validationException);
4250
$error['extensions']['status'] = Response::HTTP_BAD_REQUEST;
4351
$error['extensions']['category'] = 'user';
4452
$error['extensions']['violations'] = [];
@@ -61,4 +69,24 @@ public function supportsNormalization($data, $format = null): bool
6169
{
6270
return $data instanceof Error && $data->getPrevious() instanceof ValidationException;
6371
}
72+
73+
private function normalizeValidationException(ValidationException $exception): string
74+
{
75+
$message = '';
76+
foreach ($exception->getConstraintViolationList() as $violation) {
77+
if ('' !== $message) {
78+
$message .= "\n";
79+
}
80+
81+
$class = \is_object($root = $violation->getRoot()) ? \get_class($root) : null;
82+
$propertyPath = $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath(), $class) : $violation->getPropertyPath();
83+
if ($propertyPath) {
84+
$message .= "$propertyPath: ";
85+
}
86+
87+
$message .= $violation->getMessage();
88+
}
89+
90+
return $message;
91+
}
6492
}

0 commit comments

Comments
 (0)