Skip to content

Commit 93f4f13

Browse files
committed
Merge branch '5.4' into 6.4
* 5.4: [Serializer] Fix `GetSetMethodNormalizer` not working with setters with optional args
2 parents e3df70b + a48b386 commit 93f4f13

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function isSetMethod(\ReflectionMethod $method): bool
117117
{
118118
return !$method->isStatic()
119119
&& !$method->getAttributes(Ignore::class)
120-
&& 1 === $method->getNumberOfRequiredParameters()
120+
&& 0 < $method->getNumberOfParameters()
121121
&& str_starts_with($method->name, 'set');
122122
}
123123

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,18 @@ public function testSupportsAndDenormalizeWithOnlyParentSetter()
543543
$obj = $this->normalizer->denormalize(['foo' => 'foo'], GetSetDummyChild::class);
544544
$this->assertSame('foo', $obj->getFoo());
545545
}
546+
547+
/**
548+
* @testWith [{"foo":"foo"}, "getFoo", "foo"]
549+
* [{"bar":"bar"}, "getBar", "bar"]
550+
*/
551+
public function testSupportsAndDenormalizeWithOptionalSetterArgument(array $data, string $method, string $expected)
552+
{
553+
$this->assertTrue($this->normalizer->supportsDenormalization($data, GetSetDummyWithOptionalAndMultipleSetterArgs::class));
554+
555+
$obj = $this->normalizer->denormalize($data, GetSetDummyWithOptionalAndMultipleSetterArgs::class);
556+
$this->assertSame($expected, $obj->$method());
557+
}
546558
}
547559

548560
class GetSetDummy
@@ -864,3 +876,29 @@ public function setFoo($foo)
864876
$this->foo = $foo;
865877
}
866878
}
879+
880+
class GetSetDummyWithOptionalAndMultipleSetterArgs
881+
{
882+
private $foo;
883+
private $bar;
884+
885+
public function getFoo()
886+
{
887+
return $this->foo;
888+
}
889+
890+
public function setFoo($foo = null)
891+
{
892+
$this->foo = $foo;
893+
}
894+
895+
public function getBar()
896+
{
897+
return $this->bar;
898+
}
899+
900+
public function setBar($bar = null, $other = true)
901+
{
902+
$this->bar = $bar;
903+
}
904+
}

0 commit comments

Comments
 (0)