Skip to content

Commit 616c582

Browse files
committed
Merge branch '7.0' into 7.1
* 7.0: [Serializer] Fix `GetSetMethodNormalizer` not working with setters with optional args [Cache] Fix support for predis/predis:^2.0
2 parents 820382c + fdcbb53 commit 616c582

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
@@ -99,7 +99,7 @@ private function isSetMethod(\ReflectionMethod $method): bool
9999
{
100100
return !$method->isStatic()
101101
&& !$method->getAttributes(Ignore::class)
102-
&& 1 === $method->getNumberOfRequiredParameters()
102+
&& 0 < $method->getNumberOfParameters()
103103
&& str_starts_with($method->name, 'set');
104104
}
105105

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,18 @@ public function testSupportsAndDenormalizeWithOnlyParentSetter()
551551
$obj = $this->normalizer->denormalize(['foo' => 'foo'], GetSetDummyChild::class);
552552
$this->assertSame('foo', $obj->getFoo());
553553
}
554+
555+
/**
556+
* @testWith [{"foo":"foo"}, "getFoo", "foo"]
557+
* [{"bar":"bar"}, "getBar", "bar"]
558+
*/
559+
public function testSupportsAndDenormalizeWithOptionalSetterArgument(array $data, string $method, string $expected)
560+
{
561+
$this->assertTrue($this->normalizer->supportsDenormalization($data, GetSetDummyWithOptionalAndMultipleSetterArgs::class));
562+
563+
$obj = $this->normalizer->denormalize($data, GetSetDummyWithOptionalAndMultipleSetterArgs::class);
564+
$this->assertSame($expected, $obj->$method());
565+
}
554566
}
555567

556568
class GetSetDummy
@@ -872,3 +884,29 @@ public function setFoo($foo)
872884
$this->foo = $foo;
873885
}
874886
}
887+
888+
class GetSetDummyWithOptionalAndMultipleSetterArgs
889+
{
890+
private $foo;
891+
private $bar;
892+
893+
public function getFoo()
894+
{
895+
return $this->foo;
896+
}
897+
898+
public function setFoo($foo = null)
899+
{
900+
$this->foo = $foo;
901+
}
902+
903+
public function getBar()
904+
{
905+
return $this->bar;
906+
}
907+
908+
public function setBar($bar = null, $other = true)
909+
{
910+
$this->bar = $bar;
911+
}
912+
}

0 commit comments

Comments
 (0)