Skip to content

Commit 0b3d41f

Browse files
committed
Add AbstractObjectNormalizer::ENABLE_TYPE_CONVERSION for scalar type transformation
1 parent 37ba207 commit 0b3d41f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Make `ProblemNormalizer` give details about Messenger's `ValidationFailedException`
1212
* Add `XmlEncoder::CDATA_WRAPPING` context option
1313
* Deprecate `AnnotationLoader`, use `AttributeLoader` instead
14+
* Add `AbstractObjectNormalizer::ENABLE_TYPE_CONVERSION` for scalar type transformation
1415

1516
6.3
1617
---

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
5656
*/
5757
public const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement';
5858

59+
/**
60+
* While denormalizing, convert scalar types to the expected type.
61+
*
62+
* Enabled by default for the XML and CSV format, because all basic datatypes are represented as strings.
63+
*/
64+
public const ENABLE_TYPE_CONVERSION = 'enable_type_conversion';
65+
5966
/**
6067
* Flag to control whether fields with the value `null` should be output
6168
* when normalizing or omitted.
@@ -320,6 +327,10 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
320327
return null;
321328
}
322329

330+
if (!isset($context[self::ENABLE_TYPE_CONVERSION]) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
331+
$context[self::ENABLE_TYPE_CONVERSION] = true;
332+
}
333+
323334
$allowedAttributes = $this->getAllowedAttributes($type, $context, true);
324335
$normalizedData = $this->prepareForDenormalization($data);
325336
$extraAttributes = [];
@@ -461,7 +472,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
461472
// In XML and CSV all basic datatypes are represented as strings, it is e.g. not possible to determine,
462473
// if a value is meant to be a string, float, int or a boolean value from the serialized representation.
463474
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
464-
if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
475+
if (\is_string($data) && ($context[self::ENABLE_TYPE_CONVERSION] ?? false)) {
465476
if ('' === $data) {
466477
if (Type::BUILTIN_TYPE_ARRAY === $builtinType = $type->getBuiltinType()) {
467478
return [];
@@ -595,7 +606,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
595606
throw $missingConstructorArgumentsException;
596607
}
597608

598-
if ($data === '' && !$hasNonObjectType && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
609+
if ($data === '' && !$hasNonObjectType && ($context[self::ENABLE_TYPE_CONVERSION] ?? false)) {
599610
return null;
600611
} elseif (!$isUnionType && isset($e)) {
601612
throw $e;

0 commit comments

Comments
 (0)