Skip to content

Commit 59dd67c

Browse files
committed
bug #22550 Allow Upper Case property names in ObjectNormalizer (insekticid)
This PR was merged into the 2.8 branch. Discussion ---------- Allow Upper Case property names in ObjectNormalizer | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22547 | License | MIT 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; } } ``` Commits ------- b2b4faa3c0 Allow Upper Case property names in ObjectNormalizer
2 parents c22139a + b9cdb23 commit 59dd67c

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)