Skip to content

Commit

Permalink
bug #22550 Allow Upper Case property names in ObjectNormalizer (insek…
Browse files Browse the repository at this point in the history
…ticid)

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 #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
-------

b2b4faa Allow Upper Case property names in ObjectNormalizer
  • Loading branch information
fabpot committed Apr 29, 2017
2 parents 04e48bd + b2b4faa commit 07fc060
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Expand Up @@ -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;
}
}

Expand Down
Expand Up @@ -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();
Expand Down Expand Up @@ -662,3 +667,14 @@ public static function getBaz()
return 'L';
}
}

class ObjectWithUpperCaseAttributeNames
{
private $Foo = 'Foo';
public $Bar = 'BarBar';

public function getFoo()
{
return $this->Foo;
}
}

0 comments on commit 07fc060

Please sign in to comment.