Skip to content

[PropertyAccess] getReadAccessInfo can not be cached due naming restrictions when using Anonymous Class object #23136

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
mablae opened this issue Jun 11, 2017 · 2 comments

Comments

@mablae
Copy link
Contributor

mablae commented Jun 11, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? no
Symfony version 3.2.6

Trying to serialize a Anonymous Class with the symfony/serializer results in an Exception due to naming of the $key that get_class($object) returns in https://github.com/symfony/symfony/blob/master/src/Symfony/Component/PropertyAccess/PropertyAccessor.php#L436

@mablae
Copy link
Contributor Author

mablae commented Jun 11, 2017

I was able to quick and dirty fix it by replacing

$key = $class.'..'.$property

with

$key = str_replace('/', '.',str_replace('@', '.',$class)).'..'.$property;

in the PropertyAccessor.php

Is this is a suitable way to solve this issue? Ping @dunglas

@mablae mablae changed the title [PropertyAccess] Anonymous Class can not be cached due naming restrictions [PropertyAccess] getReadAccessInfo can not be cached due naming restrictions when using Anonymous Class object Jun 11, 2017
@mablae
Copy link
Contributor Author

mablae commented Jun 11, 2017

Way to reproduce:

$latitude = 54;
$longitude = 9;

$serializer = $this->get('serializer');

$anonObject = new class($latitude, $longitude) implements Point {
  
  
                private $latitude;
                private $longitude;

                public function __construct($latitude, $longitude) {
                    $this->latitude = $latitude; 
                    $this->longitude = $longitude;
                }        
     
             
                public function getLongitude() : float
                {
                     return $this->longitude;
                }

               
                public function getLatitude(): float
                {
                    return $this->latitude;
                }
}


$serialized = $serializer->serialize($anonObject, 'json');

fabpot added a commit that referenced this issue Jun 14, 2017
This PR was merged into the 3.2 branch.

Discussion
----------

[PropertyAccess] Fix Usage with anonymous classes

| Q             | A
| ------------- | ---
| Branch?       | 3.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23136
| License       | MIT

Replace forbidden characters in the the class names of Anonymous Classes in form of
"class@anonymous /symfony/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php0x7f3f5f267ad5"

Wrapped in eval to avoid PHP parsing errors < 7 and using `rawurlenceode` for perf reasons

Thanks @nicolas-grekas for the help and patience. Let me know if anything is missing.

Commits
-------

3f7fd43 Fix Usage with anonymous classes
@fabpot fabpot closed this as completed Jun 14, 2017
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

4 participants