Skip to content

[Serializer] [ObjectNormalizer] Fix isser methods where the property has the same name #61097

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

Draft
wants to merge 2 commits into
base: 6.4
Choose a base branch
from
Draft
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 @@ -114,10 +114,15 @@ protected function extractAttributes(object $object, ?string $format = null, arr
}
} elseif ('is' !== $name && str_starts_with($name, 'is') && !ctype_lower($name[2])) {
// issers
$attributeName = substr($name, 2);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
if ($reflClass->hasProperty($name)) {
// when property and isser method have the same name, we need to keep the is* prefix
$attributeName = $name;
} else {
$attributeName = substr($name, 2);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,21 @@ public function testNormalizeWithMethodNamesSimilarToAccessors()
123 => 321,
], $normalized);
}

public function testNormalizeObjectWithBooleanPropertyAndIsserMethodWithSameName()
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizer = new ObjectNormalizer($classMetadataFactory);

$object = new ObjectWithBooleanPropertyAndIsserWithSameName();
$normalized = $normalizer->normalize($object);

$this->assertSame([
'foo' => 'foo',
// Previously property name was converted to 'foo'
'isFoo' => true,
], $normalized);
}
}

class ProxyObjectDummy extends ObjectDummy
Expand Down Expand Up @@ -1297,3 +1312,20 @@ public function isolate()
$this->accessorishCalled = true;
}
}

class ObjectWithBooleanPropertyAndIsserWithSameName
{
private $foo = 'foo';

private $isFoo = true;

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

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