Skip to content

Serializer does not map basic non-string types correctly in XML and CSV #33849

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

Closed
mkrauser opened this issue Oct 4, 2019 · 5 comments · Fixed by #33850
Closed

Serializer does not map basic non-string types correctly in XML and CSV #33849

mkrauser opened this issue Oct 4, 2019 · 5 comments · Fixed by #33850

Comments

@mkrauser
Copy link
Contributor

mkrauser commented Oct 4, 2019

Symfony version(s) affected: 3.4, 4.4

Description
In XML and CSV, all values are represented the same way, it is not possible to determine if a value is intended to be a string, int, float or boolean.

If something with non string properties (int, float, bool) is deserialized, an error is thrown:

PHP Fatal error:  Uncaught Symfony\Component\Serializer\Exception\NotNormalizableValueException: The type of the "width" attribute for class "App\Entities\Building" must be one of "float" ("string" given). in ./symfony-collection-serializer-bug/Normalizer/AbstractObjectNormalizer.php:298
Stack trace:
#0 ./symfony-collection-serializer-bug/Normalizer/AbstractObjectNormalizer.php(202): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->validateAndDenormalize('App\\Entities\\Bu...', 'width', '10.1', 'xml', Array)
#1 ./symfony-collection-serializer-bug/Serializer.php(172): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->denormalize(Array, 'App\\Entities\\Bu...', 'xml', Array)
#2 ./symfony-collection-serializer-bug/Serializer.php(123): Symfony\Component\Serializer\Serializer->denormalize(Array, 'App\\Entities\\Bu...', 'xml', Array)
...

How to reproduce
I'll submit a PR with a failing test

Possible Solution
The values need to be converted cast to the expected type.

Additional context
Tested with php 7.2

@mkrauser mkrauser changed the title Symfony serializer does not map basic non-string types correctly in XML and CSV Serializer does not map basic non-string types correctly in XML and CSV Oct 4, 2019
@warslett
Copy link
Contributor

This is a massive problem which makes the Serializer component completely unusable with XML. We are required to handle XML content on our API so that it can integrate with legacy technology. Same problem in API Platform @dunglas

@warslett
Copy link
Contributor

@warslett
Copy link
Contributor

@mkrauser is this issue still moving? It's holding up the fix in API Platform. Let me know if you don't have time to finish it I'll pick it up in a separate PR.

@mkrauser
Copy link
Contributor Author

@warslett Yes, definitly!

@fabpot fabpot closed this as completed Sep 2, 2020
fabpot added a commit that referenced this issue Sep 2, 2020
…es 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
@ugo-fl
Copy link

ugo-fl commented Apr 22, 2021

I still get that issue in the case of a custom normalizer (implements ContextAwareDenormalizerInterface in my specific case).

My denormalize function checks if $data corresponds to an existing entity in the database, and calls $this->serializer->denormalize($data, $class) otherwise. In the end, I had to manually cast every attributes to float.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants