From 7711c53afe119889496ac447a26b8baee40b12ee Mon Sep 17 00:00:00 2001 From: Anthony Tenneriello Date: Fri, 14 Jun 2024 10:35:12 +0200 Subject: [PATCH 1/3] [Serializer][Validator] Fix propertyPath when normalize constraint violations with MetadataAwareNameConverter. --- .../ConstraintViolationListNormalizer.php | 11 +++++- .../ConstraintViolationListNormalizerTest.php | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php index 814b6f8729ffe..c80c5fce3e137 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..e5484d09df16f 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 */ From fbaf2683ee7af8620bf9aee27fc131d3e5a16cca Mon Sep 17 00:00:00 2001 From: Anthony Tenneriello Date: Fri, 14 Jun 2024 10:52:45 +0200 Subject: [PATCH 2/3] [Serializer][Validator] Fix static analysis report. --- .../ConstraintViolationListNormalizer.php | 2 +- .../ConstraintViolationListNormalizerTest.php | 32 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php index c80c5fce3e137..648decf67d080 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php @@ -67,7 +67,7 @@ public function normalize(mixed $object, ?string $format = null, array $context if (null !== $this->nameConverter) { $propertyPath = $this->nameConverter->normalize( $violation->getPropertyPath(), - is_object($violation->getRoot()) ? get_class($violation->getRoot()) : null, + \is_object($violation->getRoot()) ? \get_class($violation->getRoot()) : null, $format, $context ); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php index e5484d09df16f..e854e94eea279 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php @@ -55,23 +55,23 @@ public function testNormalize() 'detail' => 'd: a 4: 1', 'violations' => [ - [ - 'propertyPath' => 'd', - 'title' => 'a', - 'template' => 'b', - 'type' => 'urn:uuid:f', - 'parameters' => [ - 'value' => 'foo', - ], - ], - [ - 'propertyPath' => '4', - 'title' => '1', - 'template' => '2', - 'type' => 'urn:uuid:6', - 'parameters' => [], + [ + 'propertyPath' => 'd', + 'title' => 'a', + 'template' => 'b', + 'type' => 'urn:uuid:f', + 'parameters' => [ + 'value' => 'foo', ], ], + [ + 'propertyPath' => '4', + 'title' => '1', + 'template' => '2', + 'type' => 'urn:uuid:6', + 'parameters' => [], + ], + ], ]; $this->assertEquals($expected, $this->normalizer->normalize($list)); @@ -140,7 +140,7 @@ public function testNormalizeWithMetadataAwareNameConverter() 'title' => 'too long', 'template' => 'a', 'parameters' => [], - ] + ], ], ]; From a952a70ce24210a28048e8868f33f921cf52fd94 Mon Sep 17 00:00:00 2001 From: Anthony Tenneriello Date: Fri, 21 Jun 2024 21:18:57 +0200 Subject: [PATCH 3/3] [Serializer][Validator] Revert fabbot CS fix unrelated to the PR --- .../ConstraintViolationListNormalizerTest.php | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php index e854e94eea279..a595162b611ce 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php @@ -55,23 +55,23 @@ public function testNormalize() 'detail' => 'd: a 4: 1', 'violations' => [ - [ - 'propertyPath' => 'd', - 'title' => 'a', - 'template' => 'b', - 'type' => 'urn:uuid:f', - 'parameters' => [ - 'value' => 'foo', + [ + 'propertyPath' => 'd', + 'title' => 'a', + 'template' => 'b', + 'type' => 'urn:uuid:f', + 'parameters' => [ + 'value' => 'foo', + ], + ], + [ + 'propertyPath' => '4', + 'title' => '1', + 'template' => '2', + 'type' => 'urn:uuid:6', + 'parameters' => [], ], ], - [ - 'propertyPath' => '4', - 'title' => '1', - 'template' => '2', - 'type' => 'urn:uuid:6', - 'parameters' => [], - ], - ], ]; $this->assertEquals($expected, $this->normalizer->normalize($list));