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

Override existing property with another #1067

Closed
mikolajzieba opened this issue Mar 27, 2019 · 1 comment
Closed

Override existing property with another #1067

mikolajzieba opened this issue Mar 27, 2019 · 1 comment

Comments

@mikolajzieba
Copy link

Q A
Bug report? no
Feature request? yes
BC Break report? yes/no
RFC? no

I try to override one property with another on serialization, but I wasn't able to do so without removing target property from the serialized object.

I want to replace property "id" with property "fooBar".
When property "id" doesn't exists in the serialized object, it replaces it as desired, but when it exists - it doesn't.

It's caused by overriding virtualProperty config when source property exists: https://github.com/schmittjoh/serializer/blob/2.2.0/src/Metadata/Driver/YamlDriver.php#L86

Steps required to reproduce the problem

Serializer config:

      getFooBar:
        name: id
        serialized_name: id
        type: int

Serialized class:

class SomeClass {
 public $id;
 public $fooBar;
 public function getFooBar() {
    return $this->fooBar;
 }
}

$toSerialize = new SomeClass();
$toSerialize->id = 1;
$toSerialize->fooBar = 999;

Expected Result

{"id":"999"}

Actual Result

{"id":"1"}
@goetas
Copy link
Collaborator

goetas commented Mar 27, 2019

This can already be achieved by using a virtual property as "id" filed. and inside there

function getId(){
   return $this->id ?: $this->fooBar;
}

@goetas goetas closed this as completed Mar 27, 2019
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

2 participants