8000 [Serializer] Use name converter when normalizing constraint violation list by norkunas · Pull Request #30717 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Use name converter when normalizing constraint violation list #30717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

<!-- Normalizer -->
<service id="serializer.normalizer.constraint_violation_list" class="Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer">
<argument type="collection" />
<argument type="service" id="serializer.name_converter.metadata_aware" />
<!-- Run before serializer.normalizer.object -->
<tag name="serializer.normalizer" priority="-915" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;

/**
Expand All @@ -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;
}

/**
Expand All @@ -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,
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
0