Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyInfo] Property case depends on the second character's case #32656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jdsaphir opened this issue Jul 22, 2019 · 2 comments
Closed

[PropertyInfo] Property case depends on the second character's case #32656

jdsaphir opened this issue Jul 22, 2019 · 2 comments

Comments

@jdsaphir
Copy link

Symfony version(s) affected: 4.3.2

Description
Using camelCase, with an attribute $aFooBar, naming the getter/setter getAFooBar()/setAFooBar() returns the property name as AFooBar instead of aFooBar.

I'm not sure if this is a bug or intended behavior, but it seems weird that the second character's case dictates the first character case, as:

  • getAFooBar returns the property name as AFooBar (1&2 uppercase => 1&2 uppercase)
  • getAfooBar returns the property name as afooBar (1 uppercase & 2 lowercase => 1&2 lowercase)

Found it out the hard way through api-platform/core#2941

Generating the getter with make:entity from the attribute $aFooBar generates getAFooBar() which in turn gives the property name AFooBar.

How to reproduce
Create an entity such as the one below and get its properties.

// ./src/Entity/UDemandeur.php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\UDemandeurRepository")
 * @ORM\Table(name="U_DEMANDEUR")
 */
class UDemandeur {
    /**
     * @ApiProperty(identifier=true)
     * @ORM\Id()
     * @ORM\Column(type="string", name="U_DEMANDEURID", unique=true, length=255)
     */
    private $uDemandeurId;

    public function getUDemandeurId(): ?string
    {
        return $this->uDemandeurId;
    }
}
@s1k3s
Copy link

s1k3s commented Jul 28, 2019

This can be reproduced on 3.4. @xabbuh This issue concerns PropertyInfo, not PropertyAccess and it's caused by this condition here:

https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php#L122

Basically in this case the first letter is not lowercased because it passes the regex test. I'm not sure what the fix should be, but I tend to agree with the comments in the api-platform issue that we should probably define a list of rules based on which we perform the extraction and document it somewhere.

You could update the condition to something like this:

if (!$reflectionClass->hasProperty($propertyName) &&
       (!preg_match('/^[A-Z]{2,}/', $propertyName) || preg_match('/^[A-Z]{2}/', $propertyName)))

but then it would break other cases such as:

class MyClass
{
    public function getDOB()
    {
    }
}

// would return dOB

@jdsaphir
Copy link
Author

Would it be enough to check if the property is in full caps maybe?

fabpot added a commit that referenced this issue Oct 13, 2019
…om public method name (antograssiot)

This PR was merged into the 3.4 branch.

Discussion
----------

[PropertyInfo] Respect property name case when guessing from public method name

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #32656
| License       | MIT
| Doc PR        |

Using camelCase, with an attribute `$aFooBar`, naming the getter/setter `getAFooBar()`/`setAFooBar()`,  returns the property name as AFooBar instead of aFooBar.

# Before
Property name `'AFooBar'`

# After
Property name `'aFooBar'` as expected

Commits
-------

843bb76 [PropertyInfo] Respect property name case when guessing from public method name
@fabpot fabpot closed this as completed Oct 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants