Skip to content

Commit b9cdb23

Browse files
committed
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 symfony/symfony#22265 and here api-platform/core#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; } } ```
1 parent df3d919 commit b9cdb23

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Normalizer/ObjectNormalizer.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,22 @@ private function extractAttributes($object)
208208

209209
if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
210210
// getters and hassers
211-
$attributes[lcfirst(substr($name, 3))] = true;
211+
$propertyName = substr($name, 3);
212+
213+
if (!$reflClass->hasProperty($propertyName)) {
214+
$propertyName = lcfirst($propertyName);
215+
}
216+
217+
$attributes[$propertyName] = true;
212218
} elseif (strpos($name, 'is') === 0) {
213219
// issers
214-
$attributes[lcfirst(substr($name, 2))] = true;
220+
$propertyName = substr($name, 2);
221+
222+
if (!$reflClass->hasProperty($propertyName)) {
223+
$propertyName = lcfirst($propertyName);
224+
}
225+
226+
$attributes[$propertyName] = true;
215227
}
216228
}
217229

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ public function testNormalizeStatic()
488488
$this->assertEquals(array('foo' => 'K'), $this->normalizer->normalize(new ObjectWithStaticPropertiesAndMethods()));
489489
}
490490

491+
public function testNormalizeUpperCaseAttributes()
492+
{
493+
$this->assertEquals(array('Foo' => 'Foo', 'Bar' => 'BarBar'), $this->normalizer->normalize(new ObjectWithUpperCaseAttributeNames()));
494+
}
495+
491496
public function testNormalizeNotSerializableContext()
492497
{
493498
$objectDummy = new ObjectDummy();
@@ -662,3 +667,14 @@ public static function getBaz()
662667
return 'L';
663668
}
664669
}
670+
671+
class ObjectWithUpperCaseAttributeNames
672+
{
673+
private $Foo = 'Foo';
674+
public $Bar = 'BarBar';
675+
676+
public function getFoo()
677+
{
678+
return $this->Foo;
679+
}
680+
}

0 commit comments

Comments
 (0)