8000 bug #59501 [Serializer] [ObjectNormalizer] Filter int when using FILT… · symfony/symfony@717ca76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 717ca76

Browse files
bug #59501 [Serializer] [ObjectNormalizer] Filter int when using FILTER_BOOL (DjordyKoert)
This PR was submitted for the 7.2 branch but it was squashed and merged into the 7.1 branch instead. Discussion ---------- [Serializer] [ObjectNormalizer] Filter int when using FILTER_BOOL | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix ... | License | MIT Improves on #57541 to also allow numeric values `1` and `0` with `FILTER_BOOL => true` Currently attempting to use the Serializer component to deserialize to deserialize a `1` or `0` result in the following error: ``` The type of the "booleanProperty" attribute for class "App\MyDTO" must be one of "bool" ("int" given). ``` Commits ------- 5288eba [Serializer] [ObjectNormalizer] Filter int when using FILTER_BOOL
2 parents f8296b7 + 5288eba commit 717ca76

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
569569
return (float) $data;
570570
}
571571

572-
if (LegacyType::BUILTIN_TYPE_BOOL === $builtinType && \is_string($data) && ($context[self::FILTER_BOOL] ?? false)) {
572+
if (LegacyType::BUILTIN_TYPE_BOOL === $builtinType && (\is_string($data) || \is_int($data)) && ($context[self::FILTER_BOOL] ?? false)) {
573573
return filter_var($data, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE);
574574
}
575575

@@ -854,7 +854,7 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
854854
return (float) $data;
855855
}
856856

857-
if (TypeIdentifier::BOOL === $typeIdentifier && \is_string($data) && ($context[self::FILTER_BOOL] ?? false)) {
857+
if (TypeIdentifier::BOOL === $typeIdentifier && (\is_string($data) || \is_int($data)) && ($context[self::FILTER_BOOL] ?? false)) {
858858
return filter_var($data, \FILTER_VALIDATE_BOOL, \FILTER_NULL_ON_FAILURE);
859859
}
860860

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,15 +1216,34 @@ public static function provideDenormalizeWithFilterBoolData(): array
12161216
{
12171217
return [
12181218
[['foo' => 'true'], true],
1219+
[['foo' => 'True'], true],
1220+
[['foo' => 'TRUE'], true],
12191221
[['foo' => '1'], true],
1222+
[['foo' => 1], true],
12201223
[['foo' => 'yes'], true],
1224+
[['foo' => 'Yes'], true],
1225+
[['foo' => 'YES'], true],
1226+
[['foo' => 'on'], true],
1227+
[['foo' => 'On'], true],
1228+
[['foo' => 'ON'], true],
12211229
[['foo' => 'false'], false],
1230+
[['foo' => 'False'], false],
1231+
[['foo' => 'FALSE'], false],
12221232
[['foo' => '0'], false],
1233+
[['foo' => 0], false],
12231234
[['foo' => 'no'], false],
1235+
[['foo' => 'No'], false],
1236+
[['foo' => 'NO'], false],
1237+
[['foo' => 'off'], false],
1238+
[['foo' => 'Off'], false],
1239+
[['foo' => 'OFF'], false],
12241240
[['foo' => ''], false],
12251241
[['foo' => null], null],
12261242
[['foo' => 'null'], null],
12271243
[['foo' => 'something'], null],
1244+
[['foo' => 'foo'], null],
1245+
[['foo' => 1234567890], null],
1246+
[['foo' => -1234567890], null],
12281247
];
12291248
}
12301249

@@ -1253,10 +1272,7 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
12531272

12541273
public function testTemplateTypeWhenAnObjectIsPassedToDenormalize()
12551274
{
1256-
$normalizer = new class (
1257-
classMetadataFactory: new ClassMetadataFactory(new AttributeLoader()),
1258-
propertyTypeExtractor: new PropertyInfoExtractor(typeExtractors: [new PhpStanExtractor(), new ReflectionExtractor()])
1259-
) extends AbstractObjectNormalizerDummy {
1275+
$normalizer = new class(classMetadataFactory: new ClassMetadataFactory(new AttributeLoader()), propertyTypeExtractor: new PropertyInfoExtractor(typeExtractors: [new PhpStanExtractor(), new ReflectionExtractor()])) extends AbstractObjectNormalizerDummy {
12601276
protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []): bool
12611277
{
12621278
return true;
@@ -1279,10 +1295,7 @@ public function testDenormalizeTemplateType()
12791295
$this->markTestSkipped('The PropertyInfo component before Symfony 7.1 does not support template types.');
12801296
}
12811297

1282-
$normalizer = new class (
1283-
classMetadataFactory: new ClassMetadataFactory(new AttributeLoader()),
1284-
propertyTypeExtractor: new PropertyInfoExtractor(typeExtractors: [new PhpStanExtractor(), new ReflectionExtractor()])
1285-
) extends AbstractObjectNormalizerDummy {
1298+
$normalizer = new class(classMetadataFactory: new ClassMetadataFactory(new AttributeLoader()), propertyTypeExtractor: new PropertyInfoExtractor(typeExtractors: [new PhpStanExtractor(), new ReflectionExtractor()])) extends AbstractObjectNormalizerDummy {
12861299
protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []): bool
12871300
{
12881301
return true;
@@ -1587,7 +1600,7 @@ class TruePropertyDummy
15871600

15881601
class BoolPropertyDummy
15891602
{
1590-
/** @var null|bool */
1603+
/** @var bool|null */
15911604
public $foo;
15921605
}
15931606

0 commit comments

Comments
 (0)
0