Skip to content

[Serializer] Fixed "Warning: Attempt to read property "value" on string" #54851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
];
}
}
Loading