From b2b4faa3c03220a0dc5a6e23782402fe1dccd612 Mon Sep 17 00:00:00 2001 From: insekticid Date: Thu, 27 Apr 2017 14:34:21 +0200 Subject: [PATCH] Allow Upper Case property names in ObjectNormalizer | Q | A | ---------------- | ----- | Bug report? | yes | Feature request? | no | BC Break report? | yes | RFC? | no | Symfony version | 2.8.19 Same problem that has been fixed here https://github.com/symfony/symfony/pull/22265 and here https://github.com/api-platform/core/pull/1037 ObjectNormalizer returns $id instead of $Id. It is bad naming convention, but is possible ```php class Entity { protected $Id; public function getId() { return $this->Id; } } ``` --- .../Serializer/Normalizer/ObjectNormalizer.php | 16 ++++++++++++++-- .../Tests/Normalizer/ObjectNormalizerTest.php | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php index 0fde26975013f..c31ba36a7a76f 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php @@ -208,10 +208,22 @@ private function extractAttributes($object) if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) { // getters and hassers - $attributes[lcfirst(substr($name, 3))] = true; + $propertyName = substr($name, 3); + + if (!$reflClass->hasProperty($propertyName)) { + $propertyName = lcfirst($propertyName); + } + + $attributes[$propertyName] = true; } elseif (strpos($name, 'is') === 0) { // issers - $attributes[lcfirst(substr($name, 2))] = true; + $propertyName = substr($name, 2); + + if (!$reflClass->hasProperty($propertyName)) { + $propertyName = lcfirst($propertyName); + } + + $attributes[$propertyName] = true; } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index c0483180dc99d..1f735cc243b94 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -488,6 +488,11 @@ public function testNormalizeStatic() $this->assertEquals(array('foo' => 'K'), $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods())); } + public function testNormalizeUpperCaseAttributes() + { + $this->assertEquals(array('Foo' => 'Foo', 'Bar' => 'BarBar'), $this->normalizer->normalize(new ObjectWithUpperCaseAttributeNames())); + } + public function testNormalizeNotSerializableContext() { $objectDummy = new ObjectDummy(); @@ -662,3 +667,14 @@ public static function getBaz() return 'L'; } } + +class ObjectWithUpperCaseAttributeNames +{ + private $Foo = 'Foo'; + public $Bar = 'BarBar'; + + public function getFoo() + { + return $this->Foo; + } +}