diff --git a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php index 814b6f8729ffe..648decf67d080 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php @@ -62,7 +62,16 @@ public function normalize(mixed $object, ?string $format = null, array $context $violations = []; $messages = []; foreach ($object as $violation) { - $propertyPath = $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath(), null, $format, $context) : $violation->getPropertyPath(); + $propertyPath = $violation->getPropertyPath(); + + if (null !== $this->nameConverter) { + $propertyPath = $this->nameConverter->normalize( + $violation->getPropertyPath(), + \is_object($violation->getRoot()) ? \get_class($violation->getRoot()) : null, + $format, + $context + ); + } $violationEntry = [ 'propertyPath' => $propertyPath, diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php index bb69392f58abf..a595162b611ce 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php @@ -12,8 +12,13 @@ namespace Symfony\Component\Serializer\Tests\Normalizer; use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\Mapping\ClassMetadata; +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; +use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader; use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; +use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter; use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer; +use Symfony\Component\Serializer\Tests\Dummy\DummyClassOne; use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; @@ -113,6 +118,35 @@ public function testNormalizeWithNameConverter() $this->assertEquals($expected, $normalizer->normalize($list)); } + public function testNormalizeWithMetadataAwareNameConverter() + { + $attributeLoader = new AttributeLoader(); + $attributeLoader->loadClassMetadata(new ClassMetadata(DummyClassOne::class)); + + $nameConverter = new MetadataAwareNameConverter(new ClassMetadataFactory($attributeLoader)); + $normalizer = new ConstraintViolationListNormalizer([], $nameConverter); + + $list = new ConstraintViolationList([ + new ConstraintViolation('too long', 'a', [], new DummyClassOne(), 'code', ''), + ]); + + $expected = [ + 'type' => 'https://symfony.com/errors/validation', + 'title' => 'Validation Failed', + 'detail' => 'identifier: too long', + 'violations' => [ + [ + 'propertyPath' => 'identifier', + 'title' => 'too long', + 'template' => 'a', + 'parameters' => [], + ], + ], + ]; + + $this->assertEquals($expected, $normalizer->normalize($list)); + } + /** * @dataProvider payloadFieldsProvider */
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: