Skip to content
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

Virtual Property do not get serialized if getter name conflict with a class property #1164

Closed
homersimpsons opened this issue Feb 12, 2020 · 3 comments

Comments

@homersimpsons
Copy link

homersimpsons commented Feb 12, 2020

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Version 1.14.0 (via https://github.com/schmittjoh/JMSSerializerBundle)

(This issue might be a problem in https://github.com/schmittjoh/metadata)

Steps required to reproduce the problem

Declare a class as follow

class Foo {
    private $property;

    /**
     * @Serializer\VirtualProperty()
     */
    public function getProperty() {
        return $this->property ?? 42;
    }
}

What is important here is:

  • The getter name match property (to conflict with getter)
  • The VirtualProperty annotation (to tell serializer we do need this property in serialized result)

Expected Result

  • I do get {property: 42}

Actual Result

  • I do get {}

Ideas to solve the issue

I did dig a bit into the code, to me the issue lie in https://github.com/schmittjoh/metadata.

In fact ClassMetadata->propertyMetadata here do have only one property indexed with key property and this property do not have getter.

So my guess is that the getter get override by the property which lead to no data instead of the one provided by getProperty().

Looking at current master the bug might still lie here https://github.com/schmittjoh/metadata/blob/092b94df35862670a1c0411b66a872070ed2b53d/src/ClassMetadata.php#L53-L56

Workaround

The current workAround is to have a wrapper around getProperty() with a different name:

class Foo {
    private $property;

    /**
     * @Serializer\VirtualProperty()
     * @SerializedName("property")
     */
    public function workAround_getProperty() {
        return $this->getProperty();
    }

    public function getProperty() {
        return $this->property ?? 42;
    }
}

P.S.: I did not check if this happen on 2.x / 3.x

@goetas
Copy link
Collaborator

goetas commented Feb 12, 2020

Was solved some time ago with #1126

@goetas goetas closed this as completed Feb 12, 2020
@homersimpsons
Copy link
Author

Thank you, I failed to find this issue.

@tostiheld
Copy link

If I understand correctly, this should have been fixed from version 3.3.0 (by looking at the merge commit). However, I am using 3.9.0 with the symfony bundle version 3.7.0 and unfortunately I am still running into this issue (with the same POC as in the original issue).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants