8000 bug #57320 [Serializer] avoid calling undefined built-in is_*() funct… · symfony/symfony@322e7e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 322e7e4

Browse files
committed
bug #57320 [Serializer] avoid calling undefined built-in is_*() functions (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [Serializer] avoid calling undefined built-in is_*() functions | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #57314 | License | MIT Commits ------- e0a6587 avoid calling undefined built-in is_*() functions
2 parents fb64f33 + e0a6587 commit 322e7e4

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,24 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
773773
return (float) $data;
774774
}
775775

776-
if ((TypeIdentifier::FALSE === $typeIdentifier && false === $data) || (TypeIdentifier::TRUE === $typeIdentifier && true === $data)) {
777-
return $data;
778-
}
779-
780-
if (('is_'.$typeIdentifier->value)($data)) {
776+
$dataMatchesExpectedType = match ($typeIdentifier) {
777+
TypeIdentifier::ARRAY => \is_array($data),
778+
TypeIdentifier::BOOL => \is_bool($data),
779+
TypeIdentifier::CALLABLE => \is_callable($data),
780+
TypeIdentifier::FALSE => false === $data,
781+
TypeIdentifier::FLOAT => \is_float($data),
782+
TypeIdentifier::INT => \is_int($data),
783+
TypeIdentifier::ITERABLE => is_iterable($data),
784+
TypeIdentifier::MIXED => true,
785+
TypeIdentifier::NULL => null === $data,
786+
TypeIdentifier::OBJECT => \is_object($data),
787+
TypeIdentifier::RESOURCE => \is_resource($data),
788+
TypeIdentifier::STRING => \is_string($data),
789+
TypeIdentifier::TRUE => true === $data,
790+
default => false,
791+
};
792+
793+
if ($dataMatchesExpectedType) {
781794
return $data;
782795
}
783796
} catch (NotNormalizableValueException|InvalidArgumentException $e) {

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ public function testNormalizationWithMaxDepthOnStdclassObjectDoesNotThrowWarning
11321132

11331133
public function testDenormalizeCollectionOfScalarTypesPropertyWithPhpDocExtractor()
11341134
{
1135-
$normalizer = new AbstractObjectNormalizerWithMetadataAndPhpDocExtractor();
1135+
$normalizer = new AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors();
11361136
$data = [
11371137
'type' => 'foo',
11381138
'values' => [
@@ -1150,7 +1150,7 @@ public function testDenormalizeCollectionOfScalarTypesPropertyWithPhpDocExtracto
11501150

11511151
public function testDenormalizeCollectionOfUnionTypesPropertyWithPhpDocExtractor()
11521152
{
1153-
$normalizer = new AbstractObjectNormalizerWithMetadataAndPhpDocExtractor();
1153+
$normalizer = new AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors();
11541154
$data = [
11551155
'values1' => [
11561156
'foo' => 'foo',
@@ -1166,6 +1166,15 @@ public function testDenormalizeCollectionOfUnionTypesPropertyWithPhpDocExtractor
11661166

11671167
$this->assertEquals($expected, $normalizer->denormalize($data, UnionCollectionDocBlockDummy::class));
11681168
}
1169+
1170+
public function testDenormalizeMixedProperty()
1171+
{
1172+
$normalizer = new AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors();
1173+
$expected = new MixedPropertyDummy();
1174+
$expected->foo = 'bar';
1175+
1176+
$this->assertEquals($expected, $normalizer->denormalize(['foo' => 'bar'], MixedPropertyDummy::class));
1177+
}
11691178
}
11701179

11711180
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -1268,6 +1277,11 @@ class SnakeCaseNestedDummy
12681277
public $fooBar;
12691278
}
12701279

1280+
class MixedPropertyDummy
1281+
{
1282+
public mixed $foo;
1283+
}
1284+
12711285
#[DiscriminatorMap(typeProperty: 'type', mapping: [
12721286
'first' => FirstNestedDummyWithConstructorAndDiscriminator::class,
12731287
'second' => SecondNestedDummyWithConstructorAndDiscriminator::class,
@@ -1612,11 +1626,11 @@ public function __construct(
16121626
public array $values2;
16131627
}
16141628

1615-
class AbstractObjectNormalizerWithMetadataAndPhpDocExtractor extends AbstractObjectNormalizer
1629+
class AbstractObjectNormalizerWithMetadataAndPropertyTypeExtractors extends AbstractObjectNormalizer
16161630
{
16171631
public function __construct()
16181632
{
1619-
parent::__construct(new ClassMetadataFactory(new AttributeLoader()), null, new PropertyInfoExtractor([], [new PhpDocExtractor()]));
1633+
parent::__construct(new ClassMetadataFactory(new AttributeLoader()), null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]));
16201634
}
16211635

16221636
protected function extractAttributes(object $object, ?string $format = null, array $context = []): array

0 commit comments

Comments
 (0)
0