Skip to content

Commit 29aa053

Browse files
authored
Merge pull request #1932 from bendavies/constraint-violation-normalizer-name-converter
use name converter in ConstraintViolationListNormalizer
2 parents fb5adf1 + 504e71b commit 29aa053

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

src/Bridge/Symfony/Bundle/Resources/config/hydra.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<service id="api_platform.hydra.normalizer.constraint_violation_list" class="ApiPlatform\Core\Hydra\Serializer\ConstraintViolationListNormalizer" public="false">
3333
<argument type="service" id="api_platform.router" />
3434
<argument>%api_platform.validator.serialize_payload_fields%</argument>
35+
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
3536

3637
<tag name="serializer.normalizer" priority="64" />
3738
</service>

src/Bridge/Symfony/Bundle/Resources/config/problem.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<service id="api_platform.problem.normalizer.constraint_violation_list" class="ApiPlatform\Core\Problem\Serializer\ConstraintViolationListNormalizer" public="false">
1616
<argument>%api_platform.validator.serialize_payload_fields%</argument>
17+
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
1718

1819
<tag name="serializer.normalizer" priority="16" />
1920
</service>

src/Hydra/Serializer/ConstraintViolationListNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Core\Api\UrlGeneratorInterface;
1717
use ApiPlatform\Core\Serializer\AbstractConstraintViolationListNormalizer;
18+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1819

1920
/**
2021
* Converts {@see \Symfony\Component\Validator\ConstraintViolationListInterface} to a Hydra error representation.
@@ -27,9 +28,9 @@ final class ConstraintViolationListNormalizer extends AbstractConstraintViolatio
2728

2829
private $urlGenerator;
2930

30-
public function __construct(UrlGeneratorInterface $urlGenerator, array $serializePayloadFields = null)
31+
public function __construct(UrlGeneratorInterface $urlGenerator, array $serializePayloadFields = null, NameConverterInterface $nameConverter = null)
3132
{
32-
parent::__construct($serializePayloadFields);
33+
parent::__construct($serializePayloadFields, $nameConverter);
3334

3435
$this->urlGenerator = $urlGenerator;
3536
}

src/Serializer/AbstractConstraintViolationListNormalizer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Core\Serializer;
1515

16+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1617
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1718
use Symfony\Component\Validator\ConstraintViolationListInterface;
1819

@@ -28,9 +29,11 @@ abstract class AbstractConstraintViolationListNormalizer implements NormalizerIn
2829
const FORMAT = null; // Must be overrode
2930

3031
private $serializePayloadFields;
32+
private $nameConverter;
3133

32-
public function __construct(array $serializePayloadFields = null)
34+
public function __construct(array $serializePayloadFields = null, NameConverterInterface $nameConverter = null)
3335
{
36+
$this->nameConverter = $nameConverter;
3437
$this->serializePayloadFields = $serializePayloadFields;
3538
}
3639

@@ -47,7 +50,10 @@ protected function getMessagesAndViolations(ConstraintViolationListInterface $co
4750
$violations = $messages = [];
4851

4952
foreach ($constraintViolationList as $violation) {
50-
$violationData = ['propertyPath' => $violation->getPropertyPath(), 'message' => $violation->getMessage()];
53+
$violationData = [
54+
'propertyPath' => $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath()) : $violation->getPropertyPath(),
55+
'message' => $violation->getMessage(),
56+
];
5157

5258
$constraint = $violation->getConstraint();
5359
if ($this->serializePayloadFields && $constraint && $constraint->payload) {

tests/Hydra/Serializer/ConstraintViolationNormalizerTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use ApiPlatform\Core\Api\UrlGeneratorInterface;
1717
use ApiPlatform\Core\Hydra\Serializer\ConstraintViolationListNormalizer;
1818
use PHPUnit\Framework\TestCase;
19+
use Prophecy\Argument;
20+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1921
use Symfony\Component\Validator\Constraints\NotNull;
2022
use Symfony\Component\Validator\ConstraintViolation;
2123
use Symfony\Component\Validator\ConstraintViolationList;
@@ -28,8 +30,9 @@ class ConstraintViolationNormalizerTest extends TestCase
2830
public function testSupportNormalization()
2931
{
3032
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
33+
$nameConverterProphecy = $this->prophesize(NameConverterInterface::class);
3134

32-
$normalizer = new ConstraintViolationListNormalizer($urlGeneratorProphecy->reveal(), []);
35+
$normalizer = new ConstraintViolationListNormalizer($urlGeneratorProphecy->reveal(), [], $nameConverterProphecy->reveal());
3336

3437
$this->assertTrue($normalizer->supportsNormalization(new ConstraintViolationList(), ConstraintViolationListNormalizer::FORMAT));
3538
$this->assertFalse($normalizer->supportsNormalization(new ConstraintViolationList(), 'xml'));
@@ -39,9 +42,14 @@ public function testSupportNormalization()
3942
public function testNormalize()
4043
{
4144
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
45+
$nameConverterProphecy = $this->prophesize(NameConverterInterface::class);
46+
4247
$urlGeneratorProphecy->generate('api_jsonld_context', ['shortName' => 'ConstraintViolationList'])->willReturn('/context/foo')->shouldBeCalled();
48+
$nameConverterProphecy->normalize(Argument::type('string'))->will(function ($args) {
49+
return '_'.$args[0];
50+
});
4351

44-
$normalizer = new ConstraintViolationListNormalizer($urlGeneratorProphecy->reveal(), ['severity', 'anotherField1']);
52+
$normalizer = new ConstraintViolationListNormalizer($urlGeneratorProphecy->reveal(), ['severity', 'anotherField1'], $nameConverterProphecy->reveal());
4553

4654
// Note : we use NotNull constraint and not Constraint class because Constraint is abstract
4755
$constraint = new NotNull();
@@ -55,18 +63,18 @@ public function testNormalize()
5563
'@context' => '/context/foo',
5664
'@type' => 'ConstraintViolationList',
5765
'hydra:title' => 'An error occurred',
58-
'hydra:description' => 'd: a
59-
4: 1',
66+
'hydra:description' => '_d: a
67+
_4: 1',
6068
'violations' => [
6169
[
62-
'propertyPath' => 'd',
70+
'propertyPath' => '_d',
6371
'message' => 'a',
6472
'payload' => [
6573
'severity' => 'warning',
6674
],
6775
],
6876
[
69-
'propertyPath' => '4',
77+
'propertyPath' => '_4',
7078
'message' => '1',
7179
],
7280
],

tests/Problem/Serializer/ConstraintViolationNormalizerTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use ApiPlatform\Core\Problem\Serializer\ConstraintViolationListNormalizer;
1717
use PHPUnit\Framework\TestCase;
18+
use Prophecy\Argument;
19+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1820
use Symfony\Component\Validator\Constraints\NotNull;
1921
use Symfony\Component\Validator\ConstraintViolation;
2022
use Symfony\Component\Validator\ConstraintViolationList;
@@ -26,7 +28,8 @@ class ConstraintViolationNormalizerTest extends TestCase
2628
{
2729
public function testSupportNormalization()
2830
{
29-
$normalizer = new ConstraintViolationListNormalizer([]);
31+
$nameConverterProphecy = $this->prophesize(NameConverterInterface::class);
32+
$normalizer = new ConstraintViolationListNormalizer([], $nameConverterProphecy->reveal());
3033

3134
$this->assertTrue($normalizer->supportsNormalization(new ConstraintViolationList(), ConstraintViolationListNormalizer::FORMAT));
3235
$this->assertFalse($normalizer->supportsNormalization(new ConstraintViolationList(), 'xml'));
@@ -35,7 +38,12 @@ public function testSupportNormalization()
3538

3639
public function testNormalize()
3740
{
38-
$normalizer = new ConstraintViolationListNormalizer(['severity', 'anotherField1']);
41+
$nameConverterProphecy = $this->prophesize(NameConverterInterface::class);
42+
$normalizer = new ConstraintViolationListNormalizer(['severity', 'anotherField1'], $nameConverterProphecy->reveal());
43+
44+
$nameConverterProphecy->normalize(Argument::type('string'))->will(function ($args) {
45+
return '_'.$args[0];
46+
});
3947

4048
// Note : we use NotNull constraint and not Constraint class because Constraint is abstract
4149
$constraint = new NotNull();
@@ -48,18 +56,18 @@ public function testNormalize()
4856
$expected = [
4957
'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
5058
'title' => 'An error occurred',
51-
'detail' => 'd: a
52-
4: 1',
59+
'detail' => '_d: a
60+
_4: 1',
5361
'violations' => [
5462
[
55-
'propertyPath' => 'd',
63+
'propertyPath' => '_d',
5664
'message' => 'a',
5765
'payload' => [
5866
'severity' => 'warning',
5967
],
6068
],
6169
[
62-
'propertyPath' => '4',
70+
'propertyPath' => '_4',
6371
'message' => '1',
6472
],
6573
],

0 commit comments

Comments
 (0)