8000 [Serializer] Allow to include the severity in ConstraintViolationList · symfony/symfony@be855a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit be855a2

Browse files
dunglasfabpot
authored andcommitted
[Serializer] Allow to include the severity in ConstraintViolationList
1 parent 5a2aef1 commit be855a2

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added support for scalar values denormalization
88
* added support for `\stdClass` to `ObjectNormalizer`
99
* added the ability to ignore properties using metadata (e.g. `@Symfony\Component\Serializer\Annotation\Ignore`)
10+
* added an option to serialize constraint violations payloads (e.g. severity)
1011

1112
5.0.0
1213
-----

src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ConstraintViolationListNormalizer implements NormalizerInterface, Cacheabl
2828
const STATUS = 'status';
2929
const TITLE = 'title';
3030
const TYPE = 'type';
31+
const PAYLOAD_FIELDS = 'payload_fields';
3132

3233
private $defaultContext;
3334
private $nameConverter;
@@ -43,6 +44,18 @@ public function __construct($defaultContext = [], NameConverterInterface $nameCo
4344
*/
4445
public function normalize($object, string $format = null, array $context = [])
4546
{
47+
if (\array_key_exists(self::PAYLOAD_FIELDS, $context)) {
48+
$payloadFieldsToSerialize = $context[self::PAYLOAD_FIELDS];
49+
} elseif (\array_key_exists(self::PAYLOAD_FIELDS, $this->defaultContext)) {
50+
$payloadFieldsToSerialize = $this->defaultContext[self::PAYLOAD_FIELDS];
51+
} else {
52+
$payloadFieldsToSerialize = [];
53+
}
54+
55+
if (\is_array($payloadFieldsToSerialize) && [] !== $payloadFieldsToSerialize) {
56+
$payloadFieldsToSerialize = array_flip($payloadFieldsToSerialize);
57+
}
58+
4659
$violations = [];
4760
$messages = [];
4861
foreach ($object as $violation) {
@@ -57,6 +70,17 @@ public function normalize($object, string $format = null, array $context = [])
5770
$violationEntry['type'] = sprintf('urn:uuid:%s', $code);
5871
}
5972

73+
$constraint = $violation->getConstraint();
74+
if (
75+
[] !== $payloadFieldsToSerialize &&
76+
$constraint &&
77+
$constraint->payload &&
78+
// If some or all payload fields are whitelisted, add them
79+
$payloadFields = null === $payloadFieldsToSerialize || true === $payloadFieldsToSerialize ? $constraint->payload : array_intersect_key($constraint->payload, $payloadFieldsToSerialize)
80+
) {
81+
$violationEntry['payload'] = $payloadFields;
82+
}
83+
6084
$violations[] = $violationEntry;
6185

6286
$prefix = $propertyPath ? sprintf('%s: ', $propertyPath) : '';

src/Symfony/Component/Serializer/Tests/Normalizer/ConstraintViolationListNormalizerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1616
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
17+
use Symfony\Component\Validator\Constraints\NotNull;
1718
use Symfony\Component\Validator\ConstraintViolation;
1819
use Symfony\Component\Validator\ConstraintViolationList;
1920

@@ -106,4 +107,32 @@ public function testNormalizeWithNameConverter()
106107

107108
$this->assertEquals($expected, $normalizer->normalize($list));
108109
}
110+
111+
/**
112+
* @dataProvider payloadFieldsProvider
113+
*/
114+
public function testNormalizePayloadFields($fields, array $expected = null)
115+
{
116+
$constraint = new NotNull();
117+
$constraint->payload = ['severity' => 'warning', 'anotherField2' => 'aValue'];
118+
$list = new ConstraintViolationList([
119+
new ConstraintViolation('a', 'b', [], 'c', 'd', 'e', null, null, $constraint),
120+
]);
121+
122+
$violation = $this->normalizer->normalize($list, null, [ConstraintViolationListNormalizer::PAYLOAD_FIELDS => $fields])['violations'][0];
123+
if ([] === $fields) {
124+
$this->assertArrayNotHasKey('payload', $violation);
125+
126+
return;
127+
}
128+
$this->assertSame($expected, $violation['payload']);
129+
}
130+
131+
public function payloadFieldsProvider(): iterable
132+
{
133+
yield [['severity', 'anotherField1'], ['severity' => 'warning']];
134+
yield [null, ['severity' => 'warning', 'anotherField2' => 'aValue']];
135+
yield [true, ['severity' => 'warning', 'anotherField2' => 'aValue']];
136+
yield [[]];
137+
}
109138
}

0 commit comments

Comments
 (0)
0