Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Serializer] add return types#42512
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
carsonbot commentedAug 13, 2021
Hey! I think@VincentLanglet has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
chalasr commentedAug 13, 2021
ping@lyrixx as the failing tests seem related to your recent addition |
lyrixx commentedAug 13, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I noticed that when I added tests. There is a bug IMHO in the serializer, but if we fix it, it would be a BC break. So I let everything in place. Reproducer with symfony4.4.29 (yeah, branch 4.X to be sure it's "old") $serializer =newSymfony\Component\Serializer\Serializer( [newSymfony\Component\Serializer\Normalizer\ObjectNormalizer(),newSymfony\Component\Serializer\Normalizer\ArrayDenormalizer(), ], ['json' =>newSymfony\Component\Serializer\Encoder\JsonEncoder(), ]);class DummyListimplements \Countable, \IteratorAggregate{public$list;publicfunction__construct(array$list) {$this->list =$list; }publicfunctioncount():int {return\count($this->list); }publicfunctiongetIterator():\Traversable {returnnew \ArrayIterator($this->list); }}var_dump($serializer->serialize(newDummyList([]),'json', [Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS =>true,]));var_dump($serializer->serialize(newDummyList(range('a','d')),'json', [Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS =>true,])); => As you can see, this is (IMHO) wrong. And to be 100% sure, with normalize instead of serialize: TL;DR: I did not broke anything (👼🏼) because I did not want to break the BC when I found this bug. I don't know what is the best way to deal with this :/ I can fix the bug if you want... (super easy) ping@Foxprodev |
Foxprodev commentedAug 14, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@lyrixx You are right. |
Foxprodev commentedAug 14, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
symfony/src/Symfony/Component/Serializer/Serializer.php Lines 169 to 171 in29c4062
and return stdClass when data array is empty (additionally it is 8+ times faster than ArrayObject)symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php Lines 214 to 216 in29c4062
Yes.
I will open PR if you like this option P.S. Currently in one of my projects I have normalizer, which just returns object as it is (for performance tuning). So it's possible only with mixed return type. |
29c4062 to6fa3f87Comparedunglas commentedAug 17, 2021
@lyrixx AFAIK custom collection structures have never been supported and lead to undefined behaviors (as showed by your test). Only native collection structures are currently well-supported ( @Foxprodev I agree that returning a structure containing the normalized/serialized data and the related metadata would be useful, we also need such metadata for API Platform (for instance to store the identifiers of serialized resources to generated cache tags, or to generate various HTTP headers). However, I fear that it's a huge change that would be made at the same time as the planned Serializer refactoring (having only 1 object normalizer using PropertyInfo and PropertyAccess under the hood etc):#30818 |
nicolas-grekas commentedAug 17, 2021
PR welcome to fix the issue@lyrixx |
9f724ca to9b22a45Comparelyrixx commentedAug 18, 2021
Okay I'll do that |
lyrixx commentedAug 18, 2021
@nicolas-grekas see#42619. I'll open a PR on 6.0 once merged to really fix the bug |
Foxprodev commentedAug 18, 2021
@dunglas So the |
dunglas commentedAug 18, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I would keep |
nicolas-grekas commentedAug 18, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
It's possible to widen, but it's going to be very hard to tighten without causing a BC break. Better start with narrow types. |
9b22a45 tob2090d0Comparenicolas-grekas commentedAug 19, 2021
PR is green, remaining failures are false positives. |
nicolas-grekas commentedAug 19, 2021
(merging because if there is any discussion to have on return types, it should be done on 5.4) |
thrashzone13 commentedSep 29, 2021
@nicolas-grekas return type of serializer method isn't always string. It can be array if array is passed as format param. |
Extracted from#42496
Not all possible return types are patched for the attached components, to save breaking BC cross-components, for now at least.
Serializer's test fails. Help wanted 🙏