Skip to content

Commit

Permalink
bug #54851 [Serializer] Fixed "Warning: Attempt to read property "val…
Browse files Browse the repository at this point in the history
…ue" 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 #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"
  • Loading branch information
xabbuh committed May 17, 2024
2 parents 0da4e3a + 90251c9 commit bb91614
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
$typeIdentifier = TypeIdentifier::OBJECT;
$class = $t->getClassName();
} else {
$typeIdentifier = $t->getTypeIdentifier()->value;
$typeIdentifier = $t->getTypeIdentifier();
$class = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,24 @@ public function testNormalizationWithMaxDepthOnStdclassObjectDoesNotThrowWarning

$this->assertSame(['string' => 'yes'], $normalized);
}

public function testDenormalizeCollectionOfScalarTypesPropertyWithPhpDocExtractor()
{
$normalizer = new AbstractObjectNormalizerWithMetadataAndPhpDocExtractor();
$data = [
'type' => 'foo',
'values' => [
['1'],
['2'],
['3'],
['4'],
['5'],
],
];
$expected = new ScalarCollectionDocBlockDummy([[1], [2], [3], [4], [5]]);

$this->assertEquals($expected, $normalizer->denormalize($data, ScalarCollectionDocBlockDummy::class));
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down Expand Up @@ -1540,3 +1558,50 @@ public function __construct(
) {
}
}

#[DiscriminatorMap('type', ['foo' => ScalarCollectionDocBlockDummy::class])]
class ScalarCollectionDocBlockDummy
{
/**
* @param array<int, array<int, string>>|null $values
*/
public function __construct(
private readonly ?array $values = null,
) {
}

/** @return array<int, array<int, string>>|null */
public function getValues(): ?array
{
return $this->values;
}
}

class AbstractObjectNormalizerWithMetadataAndPhpDocExtractor extends AbstractObjectNormalizer
{
public function __construct()
{
parent::__construct(new ClassMetadataFactory(new AttributeLoader()), null, new PropertyInfoExtractor([], [new PhpDocExtractor()]));
}

protected function extractAttributes(object $object, ?string $format = null, array $context = []): array
{
return [];
}

protected function getAttributeValue(object $object, string $attribute, ?string $format = null, array $context = []): mixed
{
return null;
}

protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void
{
}

public function getSupportedTypes(?string $format): array
{
return [
'*' => false,
];
}
}

0 comments on commit bb91614

Please sign in to comment.