Search code examples
symfonyserializationfosrestbundlediscriminatorjms-serializer

JMS @Discriminator filed doesn't appear if specific group is serializing


I'm using Symfony 2.8, FOSRestBundle and JMSSerializerBundle.

Problem

Discriminator field type of Document entity doesn't apear in serialized model when I serialize specific group ("api" group in folowing example) of entity Citizen.

Doctrine Entities

Document:

namespace MyBundle\Entity;

use JMS\Serializer\Annotation as JMS;
…    

/**
 * @JMS\Discriminator(field = "type", map = {
 *      "doc1"              = "MyBundle\Entity\Document1",
 *      "doc2"              = "MyBundle\Entity\Document2"
 * })
 */
class Document 
…

Citizen:

class Citizen
{
 …
    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(
     *      targetEntity="MyBundle\Entity\Document",
     *      cascade={ "PERSIST", "REMOVE" },
     *      orphanRemoval=true,
     *      mappedBy="citizen"
     * )
     *
     * @JMS\Groups({"api"})
     */
    private $documents;
    …

What I get

{
…
"documents": [
    {
        "number": "000000",
        "date": "01.01.1970",
        "serial": "0000",
        "place": ""
    }
],
…
}

What I need

{
…
"documents": [
    {
        "type": "doc1",
        "number": "000000",
        "date": "01.01.1970",
        "serial": "0000",
        "place": ""
    }
],
…
}

If I remove specific serialization group, then type field is present in serialized output.

Thanks in advance


Solution

  • Just found issue on github. Seems for now, workaround with Default group is needed, see lordelph's comment