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

Discriminator field filtered by exclusion strategy #189

Closed
baptisteCable opened this issue Nov 28, 2013 · 2 comments
Closed

Discriminator field filtered by exclusion strategy #189

baptisteCable opened this issue Nov 28, 2013 · 2 comments

Comments

@baptisteCable
Copy link

It seems impossible to combine discriminator and group exclusion strategy. For instance:

/**
 * @Serializer\Discriminator(field = "type", map = {
 *    "car": "Car",
 *    "moped": "Moped",
 * })
 **/
abstract class Vehicle
{
    /**
     * @Serializer\Type("integer")
     * @Serializer\Groups({"details"})
     */
    public $km;

    public function __construct($km)
    {
        $this->km = (integer)$km;
    }
}

class Moped extends Vehicle
{
}

class Car extends Vehicle
{
}

$context = SerializationContext::create();
$context->setGroups(array('details'));
$serializer = SerializerBuilder::create()->build();

$json = $serializer->serialize(new Car(200), 'json', $context);

The result is {"km":200} and not {"km":200, "type":"car"}

I guess the issue comes from the exclusion strategy. The discriminator field does not belong to the "details" group and is excluded.

Is there a way to force the creation of this field? Does it belong to a hidden group?

If it is not the case, I suggest adding a default group to every discriminator field (e.g. "_discriminator") to make possible to see them by adding this group in the SerializationContext.

For instance, I could obtain the expected result with the following code:

$context = SerializationContext::create();
$context->setGroups(array('details', '_discriminator'));
$serializer = SerializerBuilder::create()->build();

$json = $serializer->serialize(new Car(200), 'json', $context);
@gregory90
Copy link

Fields which don't belong to any custom groups, belongs to group named 'Default'. So you could have:

$context = SerializationContext::create();
$context->setGroups(array('details', 'Default'));
$serializer = SerializerBuilder::create()->build();

$json = $serializer->serialize(new Car(200), 'json', $context);

@baptisteCable
Copy link
Author

Thank you :-)

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