-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hey! I think @VincentLanglet has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
ping @lyrixx as the failing tests seem related to your recent addition |
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 symfony 4.4.29 (yeah, branch 4.X to be sure it's "old") $serializer = new Symfony\Component\Serializer\Serializer(
[
new Symfony\Component\Serializer\Normalizer\ObjectNormalizer(),
new Symfony\Component\Serializer\Normalizer\ArrayDenormalizer(),
],
[
'json' => new Symfony\Component\Serializer\Encoder\JsonEncoder(),
]
);
class DummyList implements \Countable, \IteratorAggregate
{
public $list;
public function __construct(array $list)
{
$this->list = $list;
}
public function count(): int
{
return \count($this->list);
}
public function getIterator():\Traversable
{
return new \ArrayIterator($this->list);
}
}
var_dump($serializer->serialize(new DummyList([]), 'json', [
Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true,
]));
var_dump($serializer->serialize(new DummyList(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 |
@lyrixx You are right. |
symfony/src/Symfony/Component/Serializer/Serializer.php Lines 169 to 171 in 29c4062
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 in 29c4062
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
to
6fa3f87
Compare
@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 |
PR welcome to fix the issue @lyrixx |
9f724ca
to
9b22a45
Compare
Okay I'll do that |
@nicolas-grekas see #42619. I'll open a PR on 6.0 once merged to really fix the bug |
@dunglas So the |
I would keep |
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
to
b2090d0
Compare
PR is green, remaining failures are false positives. |
(merging because if there is any discussion to have on return types, it should be done on 5.4) |
@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 🙏