Skip to content

Commit 6e657e8

Browse files
Maximilian Zumbansennicolas-grekas
authored andcommitted
[Serializer] [ObjectNormalizer] Use bool filter when FILTER_BOOL is set
1 parent babd9be commit 6e657e8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
566566
return (float) $data;
567567
}
568568

569+
if (LegacyType::BUILTIN_TYPE_BOOL === $builtinType && \is_string($data) && ($context[self::FILTER_BOOL] ?? false)) {
570+
return filter_var($data, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE);
571+
}
572+
569573
if ((LegacyType::BUILTIN_TYPE_FALSE === $builtinType && false === $data) || (LegacyType::BUILTIN_TYPE_TRUE === $builtinType && true === $data)) {
570574
return $data;
571575
}
@@ -787,6 +791,10 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
787791
return (float) $data;
788792
}
789793

794+
if (TypeIdentifier::BOOL === $typeIdentifier && \is_string($data) && ($context[self::FILTER_BOOL] ?? false)) {
795+
return filter_var($data, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE);
796+
}
797+
790798
$dataMatchesExpectedType = match ($typeIdentifier) {
791799
TypeIdentifier::ARRAY => \is_array($data),
792800
TypeIdentifier::BOOL => \is_bool($data),

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,34 @@ public function provideBooleanTypesData()
11951195
[['foo' => false], TruePropertyDummy::class],
11961196
];
11971197
}
1198+
1199+
/**
1200+
* @dataProvider provideDenormalizeWithFilterBoolData
1201+
*/
1202+
public function testDenormalizeBooleanTypeWithFilterBool(array $data, ?bool $expectedFoo)
1203+
{
1204+
$normalizer = new AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors();
1205+
1206+
$dummy = $normalizer->denormalize($data, BoolPropertyDummy::class, null, [AbstractNormalizer::FILTER_BOOL => true]);
1207+
1208+
$this->assertSame($expectedFoo, $dummy->foo);
1209+
}
1210+
1211+
public function provideDenormalizeWithFilterBoolData(): array
1212+
{
1213+
return [
1214+
[['foo' => 'true'], true],
1215+
[['foo' => '1'], true],
1216+
[['foo' => 'yes'], true],
1217+
[['foo' => 'false'], false],
1218+
[['foo' => '0'], false],
1219+
[['foo' => 'no'], false],
1220+
[['foo' => ''], false],
1221+
[['foo' => null], null],
1222+
[['foo' => 'null'], null],
1223+
[['foo' => 'something'], null],
1224+
];
1225+
}
11981226
}
11991227

12001228
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -1480,6 +1508,12 @@ class TruePropertyDummy
14801508
public $foo;
14811509
}
14821510

1511+
class BoolPropertyDummy
1512+
{
1513+
/** @var null|bool */
1514+
public $foo;
1515+
}
1516+
14831517
class SerializerCollectionDummy implements SerializerInterface, DenormalizerInterface
14841518
{
14851519
private array $normalizers;

0 commit comments

Comments
 (0)