Skip to content

Commit ed8290e

Browse files
RafaelKrnicolas-grekas
authored andcommitted
[Serializer] Fix normalizing objects with accessors having the same name as a property
1 parent e3b664d commit ed8290e

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,22 @@ protected function extractAttributes(object $object, ?string $format = null, arr
100100
$attributeName = null;
101101

102102
// ctype_lower check to find out if method looks like accessor but actually is not, e.g. hash, cancel
103-
if (3 < \strlen($name) && !ctype_lower($name[3]) && match ($name[0]) {
104-
'g' => str_starts_with($name, 'get'),
105-
'h' => str_starts_with($name, 'has'),
106-
'c' => str_starts_with($name, 'can'),
107-
default => false,
108-
}) {
109-
// getters, hassers and canners
110-
$attributeName = substr($name, 3);
111-
112-
if (!$reflClass->hasProperty($attributeName)) {
113-
$attributeName = lcfirst($attributeName);
114-
}
115-
} elseif ('is' !== $name && str_starts_with($name, 'is') && !ctype_lower($name[2])) {
116-
// issers
117-
$attributeName = substr($name, 2);
118-
119-
if (!$reflClass->hasProperty($attributeName)) {
120-
$attributeName = lcfirst($attributeName);
103+
if (match ($name[0]) {
104+
'g' => str_starts_with($name, 'get') && isset($name[$i = 3]),
105+
'h' => str_starts_with($name, 'has') && isset($name[$i = 3]),
106+
'c' => str_starts_with($name, 'can') && isset($name[$i = 3]),
107+
'i' => str_starts_with($name, 'is') && isset($name[$i = 2]),
108+
default => false,
109+
} && !ctype_lower($name[$i])
110+
) {
111+
if ($reflClass->hasProperty($name)) {
112+
$attributeName = $name;
113+
} else {
114+
$attributeName = substr($name, $i);
115+
116+
if (!$reflClass->hasProperty($attributeName)) {
117+
$attributeName = lcfirst($attributeName);
118+
}
121119
}
122120
}
123121

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,20 @@ public function testNormalizeWithMethodNamesSimilarToAccessors()
955955
123 => 321,
956956
], $normalized);
957957
}
958+
959+
public function testNormalizeObjectWithBooleanPropertyAndIsserMethodWithSameName()
960+
{
961+
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
962+
$normalizer = new ObjectNormalizer($classMetadataFactory);
963+
964+
$object = new ObjectWithBooleanPropertyAndIsserWithSameName();
965+
$normalized = $normalizer->normalize($object);
966+
967+
$this->assertSame([
968+
'foo' => 'foo',
969+
'isFoo' => true,
970+
], $normalized);
971+
}
958972
}
959973

960974
class ProxyObjectDummy extends ObjectDummy
@@ -1297,3 +1311,19 @@ public function isolate()
12971311
$this->accessorishCalled = true;
12981312
}
12991313
}
1314+
1315+
class ObjectWithBooleanPropertyAndIsserWithSameName
1316+
{
1317+
private $foo = 'foo';
1318+
private $isFoo = true;
1319+
1320+
public function getFoo()
1321+
{
1322+
return $this->foo;
1323+
}
1324+
1325+
public function isFoo()
1326+
{
1327+
return $this->isFoo;
1328+
}
1329+
}

0 commit comments

Comments
 (0)