Skip to content

Commit

Permalink
[Serializer] Fix GetSetMethodNormalizer not working with setters wi…
Browse files Browse the repository at this point in the history
…th optional args
  • Loading branch information
HypeMC committed May 2, 2024
1 parent fd8cee6 commit 74bc0eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 74bc0eb

Please sign in to comment.