Skip to content

Commit

Permalink
bug #54828 [Serializer] Fix GetSetMethodNormalizer not working with…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
derrabus committed May 2, 2024
2 parents fd8cee6 + 74bc0eb commit a48b386
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Expand Up @@ -107,7 +107,7 @@ private function isSetMethod(\ReflectionMethod $method): bool
{
return !$method->isStatic()
&& (\PHP_VERSION_ID < 80000 || !$method->getAttributes(Ignore::class))
&& 1 === $method->getNumberOfRequiredParameters()
&& 0 < $method->getNumberOfParameters()
&& str_starts_with($method->name, 'set');
}

Expand Down
Expand Up @@ -538,6 +538,18 @@ public function testSupportsAndDenormalizeWithOnlyParentSetter()
$obj = $this->normalizer->denormalize(['foo' => 'foo'], GetSetDummyChild::class);
$this->assertSame('foo', $obj->getFoo());
}

/**
* @testWith [{"foo":"foo"}, "getFoo", "foo"]
* [{"bar":"bar"}, "getBar", "bar"]
*/
public function testSupportsAndDenormalizeWithOptionalSetterArgument(array $data, string $method, string $expected)
{
$this->assertTrue($this->normalizer->supportsDenormalization($data, GetSetDummyWithOptionalAndMultipleSetterArgs::class));

$obj = $this->normalizer->denormalize($data, GetSetDummyWithOptionalAndMultipleSetterArgs::class);
$this->assertSame($expected, $obj->$method());
}
}

class GetSetDummy
Expand Down Expand Up @@ -861,3 +873,29 @@ public function setFoo($foo)
$this->foo = $foo;
}
}

class GetSetDummyWithOptionalAndMultipleSetterArgs
{
private $foo;
private $bar;

public function getFoo()
{
return $this->foo;
}

public function setFoo($foo = null)
{
$this->foo = $foo;
}

public function getBar()
{
return $this->bar;
}

public function setBar($bar = null, $other = true)
{
$this->bar = $bar;
}
}

0 comments on commit a48b386

Please sign in to comment.