Skip to content

Commit bb91614

Browse files
committed
bug symfony#54851 [Serializer] Fixed "Warning: Attempt to read property "value" on string" (michaljusiega, xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [Serializer] Fixed "Warning: Attempt to read property "value" on string" | Q | A | ------------- | --- | Branch? | 7.1 (7.1.0-BETA1) | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fixed regresion of symfony#53160 | License | MIT Hi! I've updated my project to `7.1.0-BETA1` and I found an error. After update I got: `Warning: Attempt to read property "value" on string` in `vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:748` because in `733 line` the enum-case of value `TypeIdentifier` has been already readed. ![obraz](https://github.com/symfony/symfony/assets/16488888/5227ac76-0fb0-4b00-ab68-2394ab41d7b7) Quite not sure if tests are possible here. Commits ------- 90251c9 add test 4b3dcf1 Fixed "Warning: Attempt to read property "value" on string"
2 parents 0da4e3a + 90251c9 commit bb91614

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
730730
$typeIdentifier = TypeIdentifier::OBJECT;
731731
$class = $t->getClassName();
732732
} else {
733-
$typeIdentifier = $t->getTypeIdentifier()->value;
733+
$typeIdentifier = $t->getTypeIdentifier();
734734
$class = null;
735735
}
736736
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,24 @@ public function testNormalizationWithMaxDepthOnStdclassObjectDoesNotThrowWarning
11291129

11301130
$this->assertSame(['string' => 'yes'], $normalized);
11311131
}
1132+
1133+
public function testDenormalizeCollectionOfScalarTypesPropertyWithPhpDocExtractor()
1134+
{
1135+
$normalizer = new AbstractObjectNormalizerWithMetadataAndPhpDocExtractor();
1136+
$data = [
1137+
'type' => 'foo',
1138+
'values' => [
1139+
['1'],
1140+
['2'],
1141+
['3'],
1142+
['4'],
1143+
['5'],
1144+
],
1145+
];
1146+
$expected = new ScalarCollectionDocBlockDummy([[1], [2], [3], [4], [5]]);
1147+
1148+
$this->assertEquals($expected, $normalizer->denormalize($data, ScalarCollectionDocBlockDummy::class));
1149+
}
11321150
}
11331151

11341152
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
@@ -1540,3 +1558,50 @@ public function __construct(
15401558
) {
15411559
}
15421560
}
1561+
1562+
#[DiscriminatorMap('type', ['foo' => ScalarCollectionDocBlockDummy::class])]
1563+
class ScalarCollectionDocBlockDummy
1564+
{
1565+
/**
1566+
* @param array<int, array<int, string>>|null $values
1567+
*/
1568+
public function __construct(
1569+
private readonly ?array $values = null,
1570+
) {
1571+
}
1572+
1573+
/** @return array<int, array<int, string>>|null */
1574+
public function getValues(): ?array
1575+
{
1576+
return $this->values;
1577+
}
1578+
}
1579+
1580+
class AbstractObjectNormalizerWithMetadataAndPhpDocExtractor extends AbstractObjectNormalizer
1581+
{
1582+
public function __construct()
1583+
{
1584+
parent::__construct(new ClassMetadataFactory(new AttributeLoader()), null, new PropertyInfoExtractor([], [new PhpDocExtractor()]));
1585+
}
1586+
1587+
protected function extractAttributes(object $object, ?string $format = null, array $context = []): array
1588+
{
1589+
return [];
1590+
}
1591+
1592+
protected function getAttributeValue(object $object, string $attribute, ?string $format = null, array $context = []): mixed
1593+
{
1594+
return null;
1595+
}
1596+
1597+
protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void
1598+
{
1599+
}
1600+
1601+
public function getSupportedTypes(?string $format): array
1602+
{
1603+
return [
1604+
'*' => false,
1605+
];
1606+
}
1607+
}

0 commit comments

Comments
 (0)