Skip to content

Navigation Menu

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 01eb18d

Browse filesBrowse files
committed
feature #42240 [Serializer] Add support for preserving empty object in object property (lyrixx)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Add support for preserving empty object in object property | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | | Tickets | Fix #38192 | License | MIT | Doc PR | This PR leverage https://symfony.com/blog/new-in-symfony-5-3-inlined-serialization-context to fix #38192. Example: ```php class MyDto { public function __construct( #[Context([AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true ])] public array $mapWithOption = [], #[Context([AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true ])] public array $mapWithOptionAndData = ['foo' => 'bar'], public array $mapWithoutOption = [], public array $mapWithoutOptionAndData = ['foo' => 'bar'], ) { } } ``` Will produce: ```json {"mapWithOption":{},"mapWithOptionAndData":{"foo":"bar"},"mapWithoutOption":[],"mapWithoutOptionAndData":{"foo":"bar"}} ``` Commits ------- c422e25 [Serializer] Add support for preserving empty object in object property
2 parents 7bb4eb4 + c422e25 commit 01eb18d
Copy full SHA for 01eb18d

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 $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.