Skip to content

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

Closed
@mkrauser

Description

@mkrauser

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions