Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit01eb18d

Browse 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 leveragehttps://symfony.com/blog/new-in-symfony-5-3-inlined-serialization-context tofix#38192.Example:```phpclass 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 parents7bb4eb4 +c422e25 commit01eb18d

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

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

Lines 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‎

Lines 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‎

Lines 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'] =newclass() {
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
publicfunctiontestNormalizeScalar()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp