Skip to content

Commit e5014d8

Browse files
committed
Fix a bug preventing to serialize validator's payload
1 parent bf1f930 commit e5014d8

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function getConfigTreeBuilder()
9494
->arrayNode('validator')
9595
->addDefaultsIfNotSet()
9696
->children()
97-
->variableNode('serialize_payload_fields')->defaultValue([])->info('Enable the serialization of payload fields when a validation error is thrown.')->end()
97+
->variableNode('serialize_payload_fields')->defaultNull()->info('Enable the serialization of payload fields when a validation error is thrown.')->end()
9898
->end()
9999
->end()
100100
->arrayNode('eager_loading')

src/Serializer/AbstractConstraintViolationListNormalizer.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class AbstractConstraintViolationListNormalizer implements NormalizerIn
3535
public function __construct(array $serializePayloadFields = null, NameConverterInterface $nameConverter = null)
3636
{
3737
$this->nameConverter = $nameConverter;
38-
$this->serializePayloadFields = $serializePayloadFields;
38+
$this->serializePayloadFields = null === $serializePayloadFields ? null : array_flip($serializePayloadFields);
3939
}
4040

4141
/**
@@ -66,10 +66,14 @@ protected function getMessagesAndViolations(ConstraintViolationListInterface $co
6666
];
6767

6868
$constraint = $violation->getConstraint();
69-
if ($this->serializePayloadFields && $constraint && $constraint->payload) {
69+
if (
70+
null !== $this->serializePayloadFields &&
71+
$constraint &&
72+
$constraint->payload &&
7073
// If some fields are whitelisted, only them are added
71-
$payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, array_flip($this->serializePayloadFields));
72-
$payloadFields && $violationData['payload'] = $payloadFields;
74+
$payloadFields = [] === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
75+
) {
76+
$violationData['payload'] = $payloadFields;
7377
}
7478

7579
$violations[] = $violationData;

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11121112
'api_platform.graphql.graphiql.enabled' => true,
11131113
'api_platform.graphql.graphql_playground.enabled' => true,
11141114
'api_platform.resource_class_directories' => Argument::type('array'),
1115-
'api_platform.validator.serialize_payload_fields' => [],
1115+
'api_platform.validator.serialize_payload_fields' => null,
11161116
'api_platform.elasticsearch.enabled' => false,
11171117
];
11181118

tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
104104
'default_operation_path_resolver' => 'api_platform.operation_path_resolver.underscore',
105105
'path_segment_name_generator' => 'api_platform.path_segment_name_generator.underscore',
106106
'validator' => [
107-
'serialize_payload_fields' => [],
107+
'serialize_payload_fields' => null,
108108
],
109109
'name_converter' => null,
110110
'enable_fos_user' => true,

tests/Hydra/Serializer/ConstraintViolationNormalizerTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public function testSupportNormalization()
4040
$this->assertTrue($normalizer->hasCacheableSupportsMethod());
4141
}
4242

43-
public function testNormalize()
43+
/**
44+
* @dataProvider payloadFieldsProvider
45+
*/
46+
public function testNormalize(?array $fields, array $result)
4447
{
4548
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
4649
$nameConverterProphecy = $this->prophesize(NameConverterInterface::class);
@@ -50,7 +53,7 @@ public function testNormalize()
5053
return '_'.$args[0];
5154
});
5255

53-
$normalizer = new ConstraintViolationListNormalizer($urlGeneratorProphecy->reveal(), ['severity', 'anotherField1'], $nameConverterProphecy->reveal());
56+
$normalizer = new ConstraintViolationListNormalizer($urlGeneratorProphecy->reveal(), $fields, $nameConverterProphecy->reveal());
5457

5558
// Note : we use NotNull constraint and not Constraint class because Constraint is abstract
5659
$constraint = new NotNull();
@@ -70,16 +73,24 @@ public function testNormalize()
7073
[
7174
'propertyPath' => '_d',
7275
'message' => 'a',
73-
'payload' => [
74-
'severity' => 'warning',
75-
],
7676
],
7777
[
7878
'propertyPath' => '_4',
7979
'message' => '1',
8080
],
8181
],
8282
];
83+
if ([] !== $result) {
84+
$expected['violations'][0]['payload'] = $result;
85+
}
86+
8387
$this->assertEquals($expected, $normalizer->normalize($list));
8488
}
89+
90+
public function payloadFieldsProvider(): iterable
91+
{
92+
yield [['severity', 'anotherField1'], ['severity' => 'warning']];
93+
yield [[], ['severity' => 'warning', 'anotherField2' => 'aValue']];
94+
yield [null, []];
95+
}
8596
}

0 commit comments

Comments
 (0)