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;
    }
}
```
This commit is contained in:
insekticid 2017-04-27 14:34:21 +02:00
parent 8c28bf706b
commit b2b4faa3c0
2 changed files with 30 additions and 2 deletions

View File

@ -208,10 +208,22 @@ class ObjectNormalizer extends AbstractNormalizer
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;
}
}

View File

@ -488,6 +488,11 @@ class ObjectNormalizerTest extends TestCase
$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 @@ class ObjectWithStaticPropertiesAndMethods
return 'L';
}
}
class ObjectWithUpperCaseAttributeNames
{
private $Foo = 'Foo';
public $Bar = 'BarBar';
public function getFoo()
{
return $this->Foo;
}
}