Skip to content

Commit 80f1df4

Browse files
committed
ObjectNormalizer: allow null and scalar
1 parent a34db19 commit 80f1df4

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,11 @@ public function normalize(mixed $object, ?string $format = null, array $context
210210
foreach ($stack as $attribute => $attributeValue) {
211211
$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);
212212

213-
if (null === $attributeValue || \is_scalar($attributeValue)) {
214-
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
215-
continue;
216-
}
217-
218213
if (!$this->serializer instanceof NormalizerInterface) {
214+
if (null === $attributeValue || \is_scalar($attributeValue)) {
215+
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
216+
continue;
217+
}
219218
throw new LogicException(\sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer.', $attribute));
220219
}
221220

@@ -465,7 +464,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
465464

466465
// This try-catch should cover all NotNormalizableValueException (and all return branches after the first
467466
// exception) so we could try denormalizing all types of an union type. If the target type is not an union
468-
// type, we will just re-throw the catched exception.
467+
// type, we will just re-throw the caught exception.
469468
// In the case of no denormalization succeeds with an union type, it will fall back to the default exception
470469
// with the acceptable types list.
471470
try {

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ protected function createChildContext(array $parentContext, string $attribute, ?
960960
$this->assertSame($firstChildContextCacheKey, $secondChildContextCacheKey);
961961
}
962962

963-
public function testChildContextKeepsOriginalContextCacheKey()
963+
public function testChildContextChangesContextCacheKey()
964964
{
965965
$foobar = new Dummy();
966966
$foobar->foo = new EmptyDummy();
@@ -992,7 +992,7 @@ protected function createChildContext(array $parentContext, string $attribute, ?
992992
$serializer = new Serializer([$normalizer]);
993993
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);
994994

995-
$this->assertSame('hardcoded-foo', $normalizer->childContextCacheKey);
995+
$this->assertSame('hardcoded-baz', $normalizer->childContextCacheKey);
996996
}
997997

998998
public function testChildContextCacheKeyStaysFalseWhenOriginalCacheKeyIsFalse()

src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ public function testNormalize()
9292
$obj->setObject($object);
9393

9494
$this->serializer
95-
->expects($this->once())
9695
->method('normalize')
97-
->with($object, 'any')
98-
->willReturn('string_object')
96+
->willReturnCallback(fn ($data) => $data === $object ? 'string_object' : $data)
9997
;
10098

10199
$this->assertEquals(
@@ -111,6 +109,29 @@ public function testNormalize()
111109
);
112110
}
113111

112+
public function testNormalizeWithoutSerializer()
113+
{
114+
$obj = new GetSetDummy();
115+
$obj->setFoo('foo');
116+
$obj->setBar('bar');
117+
$obj->setBaz(true);
118+
$obj->setCamelCase('camelcase');
119+
120+
$this->normalizer = new GetSetMethodNormalizer();
121+
122+
$this->assertEquals(
123+
[
124+
'foo' => 'foo',
125+
'bar' => 'bar',
126+
'baz' => true,
127+
'fooBar' => 'foobar',
128+
'camelCase' => 'camelcase',
129+
'object' => null,
130+
],
131+
$this->normalizer->normalize($obj, 'any')
132+
);
133+
}
134+
114135
public function testDenormalize()
115136
{
116137
$obj = $this->normalizer->denormalize(
@@ -125,6 +146,7 @@ public function testDenormalize()
125146

126147
public function testIgnoredAttributesInContext()
127148
{
149+
$this->serializer->method('normalize')->willReturnArgument(0);
128150
$ignoredAttributes = ['foo', 'bar', 'baz', 'object'];
129151
$obj = new GetSetDummy();
130152
$obj->setFoo('foo');
@@ -281,6 +303,7 @@ protected function getDenormalizerForGroups(): GetSetMethodNormalizer
281303

282304
public function testGroupsNormalizeWithNameConverter()
283305
{
306+
$this->serializer->method('normalize')->willReturnArgument(0);
284307
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
285308
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
286309
$this->normalizer->setSerializer($this->serializer);
@@ -460,6 +483,7 @@ public function testHasGetterDenormalize()
460483

461484
public function testHasGetterNormalize()
462485
{
486+
$this->serializer->method('normalize')->willReturnArgument(0);
463487
$obj = new ObjectWithHasGetterDummy();
464488
$obj->setFoo(true);
465489

@@ -477,6 +501,7 @@ public function testCallMagicMethodDenormalize()
477501

478502
public function testCallMagicMethodNormalize()
479503
{
504+
$this->serializer->method('normalize')->willReturnArgument(0);
480505
$obj = new ObjectWithMagicMethod();
481506

482507
$this->assertSame(
@@ -537,6 +562,7 @@ public function testDenormalizeWithDiscriminator()
537562

538563
public function testSupportsAndNormalizeWithOnlyParentGetter()
539564
{
565+
$this->serializer->method('normalize')->willReturnArgument(0);
540566
$obj = new GetSetDummyChild();
541567
$obj->setFoo('foo');
542568

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ public function testNormalize()
112112
$obj->setGo(true);
113113

114114
$this->serializer
115-
->expects($this->once())
116115
->method('normalize')
117-
->with($object, 'any')
118-
->willReturn('string_object')
116+
->willReturnCallback(static fn ($data) => $data === $object ? 'string_object' : $data)
119117
;
120118

121119
$this->assertEquals(
@@ -132,8 +130,35 @@ public function testNormalize()
132130
);
133131
}
134132

133+
public function testNormalizeWithoutSerializer()
134+
{
135+
$obj = new ObjectDummy();
136+
$obj->setFoo('foo');
137+
$obj->bar = 'bar';
138+
$obj->setBaz(true);
139+
$obj->setCamelCase('camelcase');
140+
$obj->setObject(null);
141+
$obj->setGo(true);
142+
143+
$this->normalizer = new ObjectNormalizer();
144+
145+
$this->assertEquals(
146+
[
147+
'foo' => 'foo',
148+
'bar' => 'bar',
149+
'baz' => true,
150+
'fooBar' => 'foobar',
151+
'camelCase' => 'camelcase',
152+
'object' => null,
153+
'go' => true,
154+
],
155+
$this->normalizer->normalize($obj, 'any')
156+
);
157+
}
158+
135159
public function testNormalizeObjectWithUninitializedProperties()
136160
{
161+
$this->serializer->method('normalize')->willReturnArgument(0);
137162
$obj = new Php74Dummy();
138163
$this->assertEquals(
139164
['initializedProperty' => 'defaultValue'],
@@ -153,6 +178,7 @@ public function testNormalizeObjectWithUnsetProperties()
153178

154179
public function testNormalizeObjectWithLazyProperties()
155180
{
181+
$this->serializer->method('normalize')->willReturnArgument(0);
156182
$obj = new LazyObjectInner();
157183
unset($obj->foo);
158184
$this->assertEquals(
@@ -163,6 +189,7 @@ public function testNormalizeObjectWithLazyProperties()
163189

164190
public function testNormalizeObjectWithUninitializedPrivateProperties()
165191
{
192+
$this->serializer->method('normalize')->willReturnArgument(0);
166193
$obj = new Php74DummyPrivate();
167194
$this->assertEquals(
168195
['initializedProperty' => 'defaultValue'],
@@ -172,6 +199,7 @@ public function testNormalizeObjectWithUninitializedPrivateProperties()
172199

173200
public function testNormalizeObjectWithPrivatePropertyWithoutGetter()
174201
{
202+
$this->serializer->method('normalize')->willReturnArgument(0);
175203
$obj = new DummyPrivatePropertyWithoutGetter();
176204
$this->assertEquals(
177205
['bar' => 'bar'],
@@ -504,6 +532,7 @@ protected function getDenormalizerForGroups(): ObjectNormalizer
504532

505533
public function testGroupsNormalizeWithNameConverter()
506534
{
535+
$this->serializer->method('normalize')->willReturnArgument(0);
507536
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
508537
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
509538
$this->normalizer->setSerializer($this->serializer);
@@ -687,11 +716,13 @@ public function testNoTraversableSupport()
687716

688717
public function testNormalizeStatic()
689718
{
719+
$this->serializer->method('normalize')->willReturnArgument(0);
690720
$this->assertEquals(['foo' => 'K'], $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods()));
691721
}
692722

693723
public function testNormalizeUpperCaseAttributes()
694724
{
725+
$this->serializer->method('normalize')->willReturnArgument(0);
695726
$this->assertEquals(['Foo' => 'Foo', 'Bar' => 'BarBar'], $this->normalizer->normalize(new ObjectWithUpperCaseAttributeNames()));
696727
}
697728

@@ -877,6 +908,7 @@ public function testObjectClassResolver()
877908

878909
public function testNormalizeStdClass()
879910
{
911+
$this->serializer->method('normalize')->willReturnArgument(0);
880912
$o1 = new \stdClass();
881913
$o1->foo = 'f';
882914
$o1->bar = 'b';
@@ -962,7 +994,7 @@ public function testObjectNormalizerWithAttributeLoaderAndObjectHasStaticPropert
962994

963995
protected function getNormalizerForAccessors($accessorPrefixes = null): ObjectNormalizer
964996
{
965-
$accessorPrefixes = $accessorPrefixes ?? ReflectionExtractor::$defaultAccessorPrefixes;
997+
$accessorPrefixes ??= ReflectionExtractor::$defaultAccessorPrefixes;
966998
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
967999
$propertyAccessorBuilder = (new PropertyAccessorBuilder())
9681000
->setReadInfoExtractor(

0 commit comments

Comments
 (0)