From 2d4f75520c97961e4dbb806244b7312629b1c232 Mon Sep 17 00:00:00 2001 From: Rafael Kraut <14234815+RafaelKr@users.noreply.github.com> Date: Thu, 10 Jul 2025 15:48:06 +0200 Subject: [PATCH 1/2] Fix ObjectNormalizer for isser methods where the property has the same name --- .../Normalizer/ObjectNormalizer.php | 13 ++++++---- .../Tests/Normalizer/ObjectNormalizerTest.php | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) 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..c449e429b8b52 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -955,6 +955,20 @@ 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); + + // Previously property name was converted to 'foo' + $this->assertSame([ + 'isFoo' => true, + ], $normalized); + } } class ProxyObjectDummy extends ObjectDummy @@ -1297,3 +1311,13 @@ public function isolate() $this->accessorishCalled = true; } } + +class ObjectWithBooleanPropertyAndIsserWithSameName +{ + private $isFoo = true; + + public function isFoo() + { + return $this->isFoo; + } +} From d6f13d782b677bd0e700b70b9d0c7587d6bc4afb Mon Sep 17 00:00:00 2001 From: Rafael Kraut <14234815+RafaelKr@users.noreply.github.com> Date: Thu, 10 Jul 2025 16:21:17 +0200 Subject: [PATCH 2/2] Add another property to the testNormalizeObjectWithBooleanPropertyAndIsserMethodWithSameName test class --- .../Tests/Normalizer/ObjectNormalizerTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index c449e429b8b52..ba9153a5cba6d 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -964,8 +964,9 @@ public function testNormalizeObjectWithBooleanPropertyAndIsserMethodWithSameName $object = new ObjectWithBooleanPropertyAndIsserWithSameName(); $normalized = $normalizer->normalize($object); - // Previously property name was converted to 'foo' $this->assertSame([ + 'foo' => 'foo', + // Previously property name was converted to 'foo' 'isFoo' => true, ], $normalized); } @@ -1314,8 +1315,15 @@ public function isolate() 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