-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
Serialization issues w/ nested DTOs & Symfony 7(.1) #139
Comments
Does If you don't have a discriminator map, I would hazard the reason you're seeing it work at all with If your interface doesn't have a discriminator, then it will fall back to I think the Serializer should throw a better exception when deserializing into an interface that doesn't have a I'm also surprised you were seeing any success at all on Symfony 7.0 🤔 |
Thanks Sam, No, BarInterface does not have a DiscriminatorMap. I guess, you are wondering how deserialization of the API JSON Input works then: We have custom Denormalizers for each DTO interface, that map an "@type" field given by the API client to the correct DTO class and does some other stuff. But as I said in my first post, I don't think that the deserialization of the API input is the problem, the error happens when deserializing the values from the database. Especially "annoying" is that the serializer in 7.0 and 7.1 actually recieves the correct list of nested objects: |
It worked really well for storing geometry data in my previous project. I had a base abstract class -> a few abstract subtypes -> several concrete implementations per subtype. I only needed one Now I could see how a custom denormalizer would sort-of-work. IIRC, when the serializer sees an interface as a type-hint on a property, it assumes there's a As for why it would work on non-nested properties, I noticed a similar inconsistency when troubleshooting #127, but I could not comprehend what I was seeing. The conditional I pointed out in that issue is necessary, but for iterable data it appeared to result in a lot of hijinx. TL;DR my advice is to use |
Thanks @Aweptimum, using |
Hello @dunglas,
we have been using doctrine-json-odm for a while now, without issues.
(Project with ApiPlatform 3.3.11, doctrine-json-odm version: 1.4.1)
Our setup is something like this:
Using Symfony 7.0 (symfony/serializer version 7.0.10) this works:
a) Persisting to the database always works
b) Loading from the database requires "|array" to be in the phpdoc, else the Symfony serializer eventually receives a
[{Baz object}]
and will not set it on the$nested
property: Even though he correctly detects thatBazInterface[]
is a collectionType (https://github.com/symfony/serializer/blob/7.0/Normalizer/AbstractObjectNormalizer.php#L494) he will then fail with:The type of the "nested" attribute for class "Bar" must be one of "BazInterface[]" ("array" given).
c) It will also work without the "|array", but only if we use
@var Baz[]
(the concrete class, not the interface)d) We cannot simply skip the phpdoc as then the serializer would not know how to deserialize the array he receives from API requests into object, he would simply leave them as plain arrays
But switching to Symfony 7.1. (serializer 7.1.3 + the new symfony/type-info 7.1.1) this no longer works:
a) again, removing "|array" from our previously working phpdoc
@var BarInterface[]|array
would cause the deserialization from the DB to fail (see above)b) but keeping "|array" now results in the Symfony serializer to not denormalize the
nested
data into objects, he simply sets the plain arrays, which causes errors later onI could not find out, what exactly is the reason, that the json_document denormalization requires the "|array" addition, as without it it would theoretically work in 7.0 & 7.1. So I opened the issue here instead of the symfony/serializer repository.
I also was not yet able to create a simple reproducer, still working on that. But maybe you already have an idea what could be the issue?
What puzzles me, is that
@var Baz[]
works without the "array", but@var BazInterface[]
doesn't, so maybe it's a Serializer issue after all.Thanks in advance,
JS
The text was updated successfully, but these errors were encountered: