Skip to content
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

Serializer: Currently it is not possible for custom formats to map basic non string attributes #45153

Open
mkrauser opened this issue Jan 24, 2022 · 0 comments

Comments

@mkrauser
Copy link
Contributor

@mkrauser mkrauser commented Jan 24, 2022

Description

Description
In custom formats, values might be represented the same way, so 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 (I copied this from the old issue I linked below, so the line-numbers may be outdated):

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-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)
...

I've had this issue with XML and CSV in the past (see #33849 and the corresponding PR #33850).

Now I've implemented a custom decoder for multiplart-post-requests, so I can upload files to my api-endpoint (using API Platform and VichUploaderBundle), and the same problem occurs.

How to reproduce

I've used this decoder here: https://api-platform.com/docs/core/file-upload/#handling-the-multipart-deserialization
Basically you need to post some non-string fields and try to deserialize them to an entity, where the parameter of the setter are typed with non-string types (int/float/bool).

Conclusion
Since form/multipart is not natively supported as a format by the serializer, I figured this is more a feature request than a bug report.

But I would love to be able to make this work with custom encoders.

Thank you for your work!

P.S.: Workaround: I've use JSON.stringify and put the json-string in a field inside the multipart-request. In the decoder this field is handled separatly. A bit ugly but it works...

Example

No response

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

Successfully merging a pull request may close this issue.

None yet
1 participant