8000 [Serializer] Add support for union collection value types in `ArrayDenormalizer` by Jeroeny · Pull Request #52018 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add support for union collection value types in ArrayDenormalizer #52018

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

Open
wants to merge 2 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Rebase onto 7.1
  • Loading branch information
Jeroeny committed May 6, 2024
commit 514bcc2abacef4b1edc5800d52c7d02027fbf304
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
$builtinType = $type->getBuiltinType();
if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
if ('' === $data) {
if (LegacyLegacyType::BUILTIN_TYPE_ARRAY === $builtinType) {
if (LegacyType::BUILTIN_TYPE_ARRAY === $builtinType) {
return [];
}

Expand Down
32 changes: 10 additions & 22 deletions src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\UnionType;

/**
* Denormalizes arrays of objects.
Expand Down Expand Up @@ -51,7 +49,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
}
if (!\is_array($data)) {
$valueType = $context['value_type'] ?? null;
$expected = $valueType ? 'array<'.implode('|', array_map(fn (Type $type) => $type->getClassName() ?? $type->getBuiltinType(), $valueType->getCollectionValueTypes())).'>' : $type;
$expected = $valueType ? 'array<'.implode('|', array_map(fn (LegacyType $type) => $type->getClassName() ?? $type->getBuiltinType(), $valueType->getCollectionValueTypes())).'>' : $type;

throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Data expected to be "%s", "%s" given.', $expected, get_debug_type($data)), $data, ['array'], $context['deserialization_path'] ?? null);
}
Expand All @@ -62,18 +60,8 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
$type = substr($type, 0, -2);
$valueType = $context['value_type'] ?? null;

# todo
$typeIdentifiers = [];
if (null !== $keyType = ($context['key_type'] ?? null)) {
if ($keyType instanceof Type) {
$typeIdentifiers = array_map(fn (Type $t): string => $t->getBaseType()->getTypeIdentifier()->value, $keyType instanceof UnionType ? $keyType->getTypes() : [$keyType]);
} else {
$typeIdentifiers = array_map(fn (LegacyType $t): string => $t->getBuiltinType(), \is_array($keyType) ? $keyType : [$keyType]);
}
}

if ($valueType instanceof Type && \count($keyTypes = $valueType->getCollectionKeyTypes()) > 0) {
$builtinTypes = array_map(static fn (Type $keyType) => $keyType->getBuiltinType(), $keyTypes);
if ($valueType instanceof LegacyType && \count($keyTypes = $valueType->getCollectionKeyTypes()) > 0) {
$builtinTypes = array_map(static fn (LegacyType $keyType) => $keyType->getBuiltinType(), $keyTypes);
} else {
$builtinTypes = array_map(static fn (LegacyType $keyType) => $keyType->getBuiltinType(), \is_array($keyType = $context['key_type'] ?? []) ? $keyType : [$keyType]);
}
Expand All @@ -82,9 +70,9 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
$subContext = $context;
$subContext['deserialization_path'] = ($context['deserialization_path'] ?? false) ? sprintf('%s[%s]', $context['deserialization_path'], $key) : "[$key]";

$this->validateKeyType($typeIdentifiers, $key, $subContext['deserialization_path']);
$this->validateKeyType($builtinTypes, $key, $subContext['deserialization_path']);

if ($valueType instanceof Type) {
if ($valueType instanceof LegacyType) {
foreach ($valueType->getCollectionValueTypes() as $subtype) {
try {
$subContext['value_type'] = $subtype;
Expand All @@ -95,7 +83,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
continue 2;
}

if (Type::BUILTIN_TYPE_ARRAY === $subtype->getBuiltinType()) {
if (LegacyType::BUILTIN_TYPE_ARRAY === $subtype->getBuiltinType()) {
$class = $type;
} else {
$class = $subtype->getClassName() ?? $subtype->getBuiltinType();
Expand Down Expand Up @@ -128,20 +116,20 @@ public function supportsDenormalization(mixed $data, string $type, ?string $form
}

/**
* @param list<string> $typeIdentifiers
* @param list<string> $builtinTypes
*/
private function validateKeyType(array $builtinTypes, mixed $key, string $path): void
{
if (!$typeIdentifiers) {
if (!$builtinTypes) {
return;
}

foreach ($typeIdentifiers as $typeIdentifier) {
foreach ($builtinTypes as $typeIdentifier) {
if (('is_'.$typeIdentifier)($key)) {
return;
}
}

throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, implode('", "', $typeIdentifiers), get_debug_type($key)), $key, $typeIdentifiers, $path, true);
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, implode('", "', $builtinTypes), get_debug_type($key)), $key, $builtinTypes, $path, true);
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ public function testCollectDenormalizationErrors2(?ClassMetadataFactory $classMe
'useMessageForUser' => false,
'message' => 'The type of the "string" attribute for class "Symfony\\Component\\Serializer\\Tests\\Fixtures\\Php74Full" must be one of "string" ("null" given).',
],
];
];

$this->assertSame($expected, $exceptionsAsArray);
}
Expand Down Expand Up @@ -1464,8 +1464,8 @@ public function testCollectDenormalizationErrorsWithWrongPropertyWithoutConstruc

try {
$serializer->deserialize('{"get": "POST"}', DummyObjectWithEnumProperty::class, 'json', [
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
} catch (\Throwable $e) {
$this->assertInstanceOf(PartialDenormalizationException::class, $e);
}
Expand Down
0