You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug #42730 [Serializer] Return an ArrayObject for empty collection objects when PRESERVE_EMPTY_OBJECTS is set (nicolas-grekas)
This PR was merged into the 5.4 branch.
Discussion
----------
[Serializer] Return an ArrayObject for empty collection objects when PRESERVE_EMPTY_OBJECTS is set
| Q | A
| ------------- | ---
| Branch? | 5.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
In #42619, we tried adding a BC layer for this behavior change, but when reviewing #42699, I figured out there is no possible BC layer: userland implementations (eg an entity returning an empty doctrine collection) won't extend ArrayObject because we ask them to do so.
I therefor propose to return an ArrayObject as of 5.4, relying on the intuition that this should not have much BC impact in practice.
/cc `@lyrixx`
Commits
-------
7856fe7 [Serializer] Return an ArrayObject for empty collection objects when PRESERVE_EMPTY_OBJECTS is set
Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Serializer.php
+6-11Lines changed: 6 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -164,18 +164,13 @@ public function normalize($data, string $format = null, array $context = [])
164
164
return$data;
165
165
}
166
166
167
+
if (\is_array($data) && !$data && ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? false)) {
168
+
returnnew \ArrayObject();
169
+
}
170
+
167
171
if (is_iterable($data)) {
168
-
if (is_countable($data) && 0 === \count($data)) {
169
-
switch (true) {
170
-
case ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && \is_object($data):
171
-
if (!$datainstanceof \ArrayObject) {
172
-
trigger_deprecation('symfony/serializer', '5.4', 'Returning empty object of class "%s" from "%s()" is deprecated. This class should extend "ArrayObject".', get_debug_type($data), __METHOD__);
173
-
}
174
-
175
-
return$data;
176
-
case ($context[self::EMPTY_ARRAY_AS_OBJECT] ?? false) && \is_array($data):
177
-
returnnew \ArrayObject();
178
-
}
172
+
if ($datainstanceof \Countable && ($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && !\count($data)) {
0 commit comments