Skip to content

Commit 98105f5

Browse files
committed
[Serializer] fix denormalization of basic property-types in XML and CSV #33849
1 parent 527ea03 commit 98105f5

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,30 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
256256

257257
// In XML and CSV all basic datatypes are represented as strings, it is e.g. not possible to determine,
258258
// if a value is meant to be a string, float, int or a boolean value from the serialized representation.
259-
// That's why we have to transform the values, if one of these non-string basic datatypes is epxected.
259+
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
260260
//
261261
// This is special to xml and csv format
262262
if (
263263
\is_string($data) &&
264-
(XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format) &&
265-
\in_array($type->getBuiltinType(), [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)
264+
(XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)
266265
) {
266+
if (
267+
'' === $data &&
268+
$type->isNullable() &&
269+
\in_array($type->getBuiltinType(), [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)
270+
) {
271+
return null;
272+
}
273+
267274
switch ($type->getBuiltinType()) {
268275
case Type::BUILTIN_TYPE_BOOL:
269276
// according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
270277
if ('false' === $data || '0' === $data) {
271278
$data = false;
272279
} elseif ('true' === $data || '1' === $data) {
273280
$data = true;
281+
} else {
282+
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $currentClass, $data));
274283
}
275284
break;
276285
case Type::BUILTIN_TYPE_INT:
@@ -279,23 +288,23 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
279288
'-' == $data[0] && ctype_digit(substr($data, 1))
280289
) {
281290
$data = (int) $data;
291+
} else {
292+
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $currentClass, $data));
282293
}
283294
break;
284295
case Type::BUILTIN_TYPE_FLOAT:
285296
if (is_numeric($data)) {
286-
return '0x' === $data[0].$data[1] ? hexdec($data) : (float) $data;
297+
return (float) $data;
287298
}
288-
if (
289-
ctype_digit($data) ||
290-
'-' == $data[0] && ctype_digit(substr($data, 1))
291-
) {
292-
$data = (int) $data;
293-
} elseif ('NaN' === $data) {
299+
300+
if ('NaN' === $data) {
294301
return NAN;
295302
} elseif ('INF') {
296303
return INF;
297304
} elseif ('-INF') {
298305
return -INF;
306+
} else {
307+
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $currentClass, $data));
299308
}
300309

301310
break;
@@ -453,10 +462,10 @@ private function getCacheKey($format, array $context)
453462
unset($context['cache_key']); // avoid artificially different keys
454463
try {
455464
return md5($format.serialize([
456-
'context' => $context,
457-
'ignored' => $this->ignoredAttributes,
458-
'camelized' => $this->camelizedAttributes,
459-
]));
465+
'context' => $context,
466+
'ignored' => $this->ignoredAttributes,
467+
'camelized' => $this->camelizedAttributes,
468+
]));
460469
} catch (\Exception $exception) {
461470
// The context cannot be serialized, skip the cache
462471
return false;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy