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

Serializing traits with JMSSerializer and YAML #424

Closed
benatespina opened this issue Nov 14, 2014 · 7 comments
Closed

Serializing traits with JMSSerializer and YAML #424

benatespina opened this issue Nov 14, 2014 · 7 comments

Comments

@benatespina
Copy link

When trying to serialize a model that uses traits, JMSSerializer does not serialize properties included by that trait. I am using yaml to configure the serializer but it seems that it's not working.

trait IdentityTrait
{

    protected $id;

    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    public function getId()
    {
        return $this->id;
    }
}

class OurClass {
   use IdentityTrait;

   protected $test;

   public function getTest() {
       $this->test;
   }
}

JMSSerializerBundle is used and the following yaml is located in Resources/config/serializer/Model.Traits.IdentityTrait.yml

MyProject\Component\Core\Model\Traits\IdentityTrait:
    exclusion_policy: NONE
    properties:
    id:
        expose: true

And the OurClass configuration is located in Resources/config/serializer/Model.OurClass.yml

 MyProject\Component\Core\Model\OurClass:
     exclusion_policy: NONE
     properties:
         test:
             expose: true

Some code has been ignored to focus on the problem.

Maybe the trait file name is incorrect?

@sebastienvermeille
Copy link

I have the same problem I am using KnpDoctrinebehaviors for my translations and jms does not serialize my translated fields. Did you find a workaround ? @benatespina

@tarlepp
Copy link

tarlepp commented Mar 20, 2016

So have anyone solution for this?

@stephanvierkant
Copy link

stephanvierkant commented Nov 26, 2016

See this issue: #102

I'm using jms/serializer version 1.4.* and it's working fine. Couldn't find in the changelog which version added this feature, but upgrading to a newer version than 1.0.0 helps.

@yanict
Copy link

yanict commented Jan 15, 2017

I'm having the same issue (version 1.1.0).
@stephanvierkant, you say that using version 1.4.* is working but this version doesn't seem to even exist.
Could you give more inputs please ?

@stephanvierkant
Copy link

stephanvierkant commented Jan 17, 2017

@yanict I'm using version 1.4.2 of jms/serializer and version 1.1.0 of jms/serializer-bundle. Please note the difference between the library (serializer) and the bundle that implements it (serializer-bundle).

@dTrimon
Copy link

dTrimon commented Feb 10, 2017

Hello, I'm having trouble with this feature too.
Shall the configuration of the serialization be inserted in the hosting entity like in this example:

Resources/config/serializer/Model.OurClass.yml

MyProject\Component\Core\Model\OurClass:
    exclusion_policy: NONE
    properties:
        test:
            expose: true
        id:
            expose: true

Or is it possible to define the serialization elswhere, and how? I tried another file like @benatespina `but it seems never called

@goetas
Copy link
Collaborator

goetas commented May 17, 2017

Metadata are per-class when using YAML or XML, defining metadata for a trait will not work.
You have to define your properties inside the metadata file for the class that is using the trait.

@goetas goetas closed this as completed May 17, 2017
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

7 participants