feature #29130 [Serializer] Normalize constraint violation parameters (ogizanagi)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Serializer] Normalize constraint violation parameters

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see below -->
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | yes <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | N/A   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A?

Adding violation constraints' parameters to the normalized data, as these are valuable for an API client.

I used `parameters` for now, as it's the name used in `ConstraintViolationInterface::getParameters()`, but what about `placeholders` or `context`?

Commits
-------

32c90ebc8e [Serializer] Normalize constraint violation parameters
This commit is contained in:
Fabien Potencier 2019-03-17 08:15:05 +01:00
commit b4431769a1
3 changed files with 11 additions and 1 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
4.3.0
-----
* added the list of constraint violations' parameters in `ConstraintViolationListNormalizer`
4.2.0
-----

View File

@ -49,6 +49,7 @@ class ConstraintViolationListNormalizer implements NormalizerInterface, Cacheabl
$violationEntry = [
'propertyPath' => $propertyPath,
'title' => $violation->getMessage(),
'parameters' => $violation->getParameters(),
];
if (null !== $code = $violation->getCode()) {
$violationEntry['type'] = sprintf('urn:uuid:%s', $code);

View File

@ -38,7 +38,7 @@ class ConstraintViolationListNormalizerTest extends TestCase
public function testNormalize()
{
$list = new ConstraintViolationList([
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, 'f'),
new ConstraintViolation('a', 'b', ['value' => 'foo'], 'c', 'd', 'e', null, 'f'),
new ConstraintViolation('1', '2', [], '3', '4', '5', null, '6'),
]);
@ -52,11 +52,15 @@ class ConstraintViolationListNormalizerTest extends TestCase
'propertyPath' => 'd',
'title' => 'a',
'type' => 'urn:uuid:f',
'parameters' => [
'value' => 'foo',
],
],
[
'propertyPath' => '4',
'title' => '1',
'type' => 'urn:uuid:6',
'parameters' => [],
],
],
];