Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3cff073

Browse filesBrowse files
committed
[Serializer] Add support for preserving empty object in object property
1 parent cbe1f81 commit 3cff073
Copy full SHA for 3cff073

File tree

3 files changed

+9
-1
lines changed
Filter options

3 files changed

+9
-1
lines changed

‎src/Symfony/Component/Serializer/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support of PHP backed enumerations
8+
* Add support for preserving empty object in object property
89

910
5.3
1011
---

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,10 @@ private function updateData(array $data, string $attribute, $attributeValue, str
592592
return $data;
593593
}
594594

595+
if ([] === $attributeValue && ($context[self::PRESERVE_EMPTY_OBJECTS] ?? $this->defaultContext[self::PRESERVE_EMPTY_OBJECTS] ?? false)) {
596+
$attributeValue = new \ArrayObject();
597+
}
598+
595599
if ($this->nameConverter) {
596600
$attribute = $this->nameConverter->normalize($attribute, $class, $format, $context);
597601
}

‎src/Symfony/Component/Serializer/Tests/SerializerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,10 @@ public function testNormalizePreserveEmptyArrayObject()
535535
$object['foo'] = new \ArrayObject();
536536
$object['bar'] = new \ArrayObject(['notempty']);
537537
$object['baz'] = new \ArrayObject(['nested' => new \ArrayObject()]);
538-
$this->assertEquals('{"foo":{},"bar":["notempty"],"baz":{"nested":{}}}', $serializer->serialize($object, 'json', [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true]));
538+
$object['innerObject'] = new class() {
539+
public array $map = [];
540+
};
541+
$this->assertEquals('{"foo":{},"bar":["notempty"],"baz":{"nested":{}},"innerObject":{"map":{}}}', $serializer->serialize($object, 'json', [AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true]));
539542
}
540543

541544
public function testNormalizeScalar()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.