diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index 64a51cfc0d9e7..29eacdd91f80f 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -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); + } } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 6273b50deabf5..ba9153a5cba6d 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -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 @@ -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; + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy