Jmsserializerbundle: Specify @Serializer/Group for @Discriminator field

Created on 3 Mar 2016  Â·  5Comments  Â·  Source: schmittjoh/JMSSerializerBundle

This is a example code. How to add "type" filed into a specific group ?

<?php
namespace Entity;
use JMS\Serializer\Annotation as JMS;
/**
 * @JMS\Discriminator(field = "type", map = {
 *    "user": "Entity\User",
 *    "mod": "Entity\Moderator"
 * })
 */
abstract class AbstractUser
{
  /**
   * @JMS\Type("integer")
   */
  public $id;
  /**
   * @JMS\Type("string")
   */
  public $name;
  /**
   * @JMS\Type("string")
   */
  public $type;
}

Most helpful comment

Same here. Thank you @lordelph for the workaround.

All 5 comments

I've just run into a similar issue - if I'm using groups, then the discriminator will not appear in the serialized data.

I found that if my serialization context contained 'Default', then the discriminator will appear. So, one workaround is to ensure that all fields either have groups or are excluded, and always serialized with a context of Default + whatever groups you want.

It would be nice if there was a way to specify which groups the discriminator should appear in.

Same here. Thank you @lordelph for the workaround.

solved with https://github.com/schmittjoh/serializer/pull/579, will be included in v1.5

@goetas i think this is not solved, im using newest version and i have same issue

@lordelph have you better solution now?

@patie can you submit a failing test case?

Was this page helpful?
0 / 5 - 0 ratings