From dd93b707cc17722a04ccfe240d7a7837ee183209 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 27 Mar 2019 07:02:36 +0200 Subject: [PATCH] Use name converter when normalizing constraint violation list --- .../Resources/config/serializer.xml | 2 + .../FrameworkExtensionTest.php | 13 +++++++ .../ConstraintViolationListNormalizer.php | 7 +++- .../ConstraintViolationListNormalizerTest.php | 39 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml index 0a06fd1edec39..a4f218ca37352 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml @@ -32,6 +32,8 @@ + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index ddd9d64286ff5..21e01a2107232 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -43,6 +43,7 @@ use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader; use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader; +use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer; use Symfony\Component\Serializer\Normalizer\DataUriNormalizer; use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; @@ -1176,6 +1177,18 @@ public function testObjectNormalizerRegistered() $this->assertEquals(-1000, $tag[0]['priority']); } + public function testConstraintViolationListNormalizerRegistered() + { + $container = $this->createContainerFromFile('full'); + + $definition = $container->getDefinition('serializer.normalizer.constraint_violation_list'); + $tag = $definition->getTag('serializer.normalizer'); + + $this->assertEquals(ConstraintViolationListNormalizer::class, $definition->getClass()); + $this->assertEquals(-915, $tag[0]['priority']); + $this->assertEquals(new Reference('serializer.name_converter.metadata_aware'), $definition->getArgument(1)); + } + public function testSerializerCacheActivated() { $container = $this->createContainerFromFile('serializer_enabled'); diff --git a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php index 051ceaf3a385e..649c095ff46a9 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Serializer\Normalizer; +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; use Symfony\Component\Validator\ConstraintViolationListInterface; /** @@ -30,10 +31,12 @@ class ConstraintViolationListNormalizer implements NormalizerInterface, Cacheabl const TYPE = 'type'; private $defaultContext; + private $nameConverter; - public function __construct($defaultContext = []) + public function __construct($defaultContext = [], NameConverterInterface $nameConverter = null) { $this->defaultContext = $defaultContext; + $this->nameConverter = $nameConverter; } /** @@ -44,7 +47,7 @@ public function normalize($object, $format = null, array $context = []) $violations = []; $messages = []; foreach ($object as $violation) { - $propertyPath = $violation->getPropertyPath(); + $propertyPath = $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath(), null, $format, $context) : $violation->getPropertyPath(); $violationEntry = [ 'propertyPath' => $propertyPath, diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php index 86008f361aef0..24fc7cd2be896 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Serializer\Tests\Normalizer; use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; @@ -67,4 +68,42 @@ public function testNormalize() $this->assertEquals($expected, $this->normalizer->normalize($list)); } + + public function testNormalizeWithNameConverter() + { + $normalizer = new ConstraintViolationListNormalizer([], new CamelCaseToSnakeCaseNameConverter()); + + $list = new ConstraintViolationList([ + new ConstraintViolation('too short', 'a', [], 'c', 'shortDescription', ''), + new ConstraintViolation('too long', 'b', [], '3', 'product.shortDescription', 'Lorem ipsum dolor sit amet'), + new ConstraintViolation('error', 'b', [], '3', '', ''), + ]); + + $expected = [ + 'type' => 'https://symfony.com/errors/validation', + 'title' => 'Validation Failed', + 'detail' => 'short_description: too short +product.short_description: too long +error', + 'violations' => [ + [ + 'propertyPath' => 'short_description', + 'title' => 'too short', + 'parameters' => [], + ], + [ + 'propertyPath' => 'product.short_description', + 'title' => 'too long', + 'parameters' => [], + ], + [ + 'propertyPath' => '', + 'title' => 'error', + 'parameters' => [], + ], + ], + ]; + + $this->assertEquals($expected, $normalizer->normalize($list)); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy