You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #33850 [Serializer] fix denormalization of basic property-types in XML and CSV (mkrauser)
This PR was submitted for the 3.4 branch but it was squashed and merged into the 5.2-dev branch instead.
Discussion
----------
[Serializer] fix denormalization of basic property-types in XML and CSV
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Fix#33849
| License | MIT
| Doc PR |
Like I explained in the Issue, the serializer cannot de-serialize non-string basic properties (int, float, bool). This PR add's some logic to cast to the expected types.
Similar logic is already present in the [XmlUtils](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Config/Util/XmlUtils.php#L215)-Class of the Config-Component
Commits
-------
3824daf [Serializer] fix denormalization of basic property-types in XML and CSV
// according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
401
+
if ('false' === $data || '0' === $data) {
402
+
$data = false;
403
+
} elseif ('true' === $data || '1' === $data) {
404
+
$data = true;
405
+
} else {
406
+
thrownewNotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $currentClass, $data));
407
+
}
408
+
break;
409
+
case Type::BUILTIN_TYPE_INT:
410
+
if (
411
+
ctype_digit($data) ||
412
+
'-' === $data[0] && ctype_digit(substr($data, 1))
413
+
) {
414
+
$data = (int) $data;
415
+
} else {
416
+
thrownewNotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $currentClass, $data));
417
+
}
418
+
break;
419
+
case Type::BUILTIN_TYPE_FLOAT:
420
+
if (is_numeric($data)) {
421
+
return (float) $data;
422
+
}
423
+
424
+
switch ($data) {
425
+
case'NaN':
426
+
returnNAN;
427
+
case'INF':
428
+
returnINF;
429
+
case'-INF':
430
+
return -INF;
431
+
default:
432
+
thrownewNotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $currentClass, $data));
433
+
}
434
+
435
+
break;
436
+
}
437
+
}
438
+
382
439
if (null !== $collectionValueType && Type::BUILTIN_TYPE_OBJECT === $collectionValueType->getBuiltinType()) {
0 commit comments