Skip to content

Commit 514bcc2

Browse files
committed
Rebase onto 7.1
1 parent 89e2f63 commit 514bcc2

File tree

3 files changed

+14
-26
lines changed

3 files changed

+14
-26
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
462462
$builtinType = $type->getBuiltinType();
463463
if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
464464
if ('' === $data) {
465-
if (LegacyLegacyType::BUILTIN_TYPE_ARRAY === $builtinType) {
465+
if (LegacyType::BUILTIN_TYPE_ARRAY === $builtinType) {
466466
return [];
467467
}
468468

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

+10-22
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1818
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
1919
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
20-
use Symfony\Component\TypeInfo\Type;
21-
use Symfony\Component\TypeInfo\Type\UnionType;
2220

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

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

65-
# todo
66-
$typeIdentifiers = [];
67-
if (null !== $keyType = ($context['key_type'] ?? null)) {
68-
if ($keyType instanceof Type) {
69-
$typeIdentifiers = array_map(fn (Type $t): string => $t->getBaseType()->getTypeIdentifier()->value, $keyType instanceof UnionType ? $keyType->getTypes() : [$keyType]);
70-
} else {
71-
$typeIdentifiers = array_map(fn (LegacyType $t): string => $t->getBuiltinType(), \is_array($keyType) ? $keyType : [$keyType]);
72-
}
73-
}
74-
75-
if ($valueType instanceof Type && \count($keyTypes = $valueType->getCollectionKeyTypes()) > 0) {
76-
$builtinTypes = array_map(static fn (Type $keyType) => $keyType->getBuiltinType(), $keyTypes);
63+
if ($valueType instanceof LegacyType && \count($keyTypes = $valueType->getCollectionKeyTypes()) > 0) {
64+
$builtinTypes = array_map(static fn (LegacyType $keyType) => $keyType->getBuiltinType(), $keyTypes);
7765
} else {
7866
$builtinTypes = array_map(static fn (LegacyType $keyType) => $keyType->getBuiltinType(), \is_array($keyType = $context['key_type'] ?? []) ? $keyType : [$keyType]);
7967
}
@@ -82,9 +70,9 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
8270
$subContext = $context;
8371
$subContext['deserialization_path'] = ($context['deserialization_path'] ?? false) ? sprintf('%s[%s]', $context['deserialization_path'], $key) : "[$key]";
8472

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

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

98-
if (Type::BUILTIN_TYPE_ARRAY === $subtype->getBuiltinType()) {
86+
if (LegacyType::BUILTIN_TYPE_ARRAY === $subtype->getBuiltinType()) {
9987
$class = $type;
10088
} else {
10189
$class = $subtype->getClassName() ?? $subtype->getBuiltinType();
@@ -128,20 +116,20 @@ public function supportsDenormalization(mixed $data, string $type, ?string $form
128116
}
129117

130118
/**
131-
* @param list<string> $typeIdentifiers
119+
* @param list<string> $builtinTypes
132120
*/
133121
private function validateKeyType(array $builtinTypes, mixed $key, string $path): void
134122
{
135-
if (!$typeIdentifiers) {
123+
if (!$builtinTypes) {
136124
return;
137125
}
138126

139-
foreach ($typeIdentifiers as $typeIdentifier) {
127+
foreach ($builtinTypes as $typeIdentifier) {
140128
if (('is_'.$typeIdentifier)($key)) {
141129
return;
142130
}
143131
}
144132

145-
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);
133+
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);
146134
}
147135
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ public function testCollectDenormalizationErrors2(?ClassMetadataFactory $classMe
12141214
'useMessageForUser' => false,
12151215
'message' => 'The type of the "string" attribute for class "Symfony\\Component\\Serializer\\Tests\\Fixtures\\Php74Full" must be one of "string" ("null" given).',
12161216
],
1217-
];
1217+
];
12181218

12191219
$this->assertSame($expected, $exceptionsAsArray);
12201220
}
@@ -1464,8 +1464,8 @@ public function testCollectDenormalizationErrorsWithWrongPropertyWithoutConstruc
14641464

14651465
try {
14661466
$serializer->deserialize('{"get": "POST"}', DummyObjectWithEnumProperty::class, 'json', [
1467-
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
1468-
]);
1467+
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
1468+
]);
14691469
} catch (\Throwable $e) {
14701470
$this->assertInstanceOf(PartialDenormalizationException::class, $e);
14711471
}

0 commit comments

Comments
 (0)