Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] Fix GetSetMethodNormalizer not working with setters with optional args #54828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.