Skip to content

Commit a48b386

Browse files
committed
bug #54828 [Serializer] Fix GetSetMethodNormalizer not working with setters with optional args (HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix `GetSetMethodNormalizer` not working with setters with optional args | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #54784 | License | MIT Prior to #52917 setters could have an optional argument or even multiple ones. This restores the previous behavior. Commits ------- 74bc0eb [Serializer] Fix `GetSetMethodNormalizer` not working with setters with optional args
2 parents fd8cee6 + 74bc0eb commit a48b386

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
@@ -107,7 +107,7 @@ private function isSetMethod(\ReflectionMethod $method): bool
107107
{
108108
return !$method->isStatic()
109109
&& (\PHP_VERSION_ID < 80000 || !$method->getAttributes(Ignore::class))
110-
&& 1 === $method->getNumberOfRequiredParameters()
110+
&& 0 < $method->getNumberOfParameters()
111111
&& str_starts_with($method->name, 'set');
112112
}
113113

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,18 @@ public function testSupportsAndDenormalizeWithOnlyParentSetter()
538538
$obj = $this->normalizer->denormalize(['foo' => 'foo'], GetSetDummyChild::class);
539539
$this->assertSame('foo', $obj->getFoo());
540540
}
541+
542+
/**
543+
* @testWith [{"foo":"foo"}, "getFoo", "foo"]
544+
* [{"bar":"bar"}, "getBar", "bar"]
545+
*/
546+
public function testSupportsAndDenormalizeWithOptionalSetterArgument(array $data, string $method, string $expected)
547+
{
548+
$this->assertTrue($this->normalizer->supportsDenormalization($data, GetSetDummyWithOptionalAndMultipleSetterArgs::class));
549+
550+
$obj = $this->normalizer->denormalize($data, GetSetDummyWithOptionalAndMultipleSetterArgs::class);
551+
$this->assertSame($expected, $obj->$method());
552+
}
541553
}
542554

543555
class GetSetDummy
@@ -861,3 +873,29 @@ public function setFoo($foo)
861873
$this->foo = $foo;
862874
}
863875
}
876+
877+
class GetSetDummyWithOptionalAndMultipleSetterArgs
878+
{
879+
private $foo;
880+
private $bar;
881+
882+
public function getFoo()
883+
{
884+
return $this->foo;
885+
}
886+
887+
public function setFoo($foo = null)
888+
{
889+
$this->foo = $foo;
890+
}
891+
892+
public function getBar()
893+
{
894+
return $this->bar;
895+
}
896+
897+
public function setBar($bar = null, $other = true)
898+
{
899+
$this->bar = $bar;
900+
}
901+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy