{"action":"created","issue":{"url":"https://api.github.com/repos/schmittjoh/JMSSerializerBundle/issues/292","repository_url":"https://api.github.com/repos/schmittjoh/JMSSerializerBundle","labels_url":"https://api.github.com/repos/schmittjoh/JMSSerializerBundle/issues/292/labels{/name}","comments_url":"https://api.github.com/repos/schmittjoh/JMSSerializerBundle/issues/292/comments","events_url":"https://api.github.com/repos/schmittjoh/JMSSerializerBundle/issues/292/events","html_url":"https://github.com/schmittjoh/JMSSerializerBundle/issues/292","id":13930866,"number":292,"title":"@Discriminator Error:   The error is:         The discriminator field name \"type\" for base-class \"MyBundle\\Entity\\MyClass\" was not found in     input data","user":{"login":"danieledangeli","id":3957293,"avatar_url":"https://avatars.githubusercontent.com/u/3957293?v=3","gravatar_id":"","url":"https://api.github.com/users/danieledangeli","html_url":"https://github.com/danieledangeli","followers_url":"https://api.github.com/users/danieledangeli/followers","following_url":"https://api.github.com/users/danieledangeli/following{/other_user}","gists_url":"https://api.github.com/users/danieledangeli/gists{/gist_id}","starred_url":"https://api.github.com/users/danieledangeli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danieledangeli/subscriptions","organizations_url":"https://api.github.com/users/danieledangeli/orgs","repos_url":"https://api.github.com/users/danieledangeli/repos","events_url":"https://api.github.com/users/danieledangeli/events{/privacy}","received_events_url":"https://api.github.com/users/danieledangeli/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":66,"created_at":"2013-05-03T11:33:31Z","updated_at":"2016-04-21T12:47:46Z","closed_at":null,"body":"Hi, i have a problem with @discriminator for abstract class.\r\nFor example, this structure of classes don't work correctly when I try to serialize and deserialize the objects.\r\n\r\n```php\r\nclass Saloon\r\n{\r\n    /**\r\n     *@Type(\"integer\")\r\n     */\r\n    public $id;\r\n\r\n    /**\r\n     * @Type(\"MyME\\HelloBundle\\Controller\\Vehicle\")\r\n     */\r\n    public $veichle;\r\n}\r\n\r\n\r\n/**\r\n * @Discriminator(field = \"type\", map = {\"car\": \"MyME\\HelloBundle\\Controller\\Car\", * \"moped\":\"MyME\\HelloBundle\\Controller\\Moped\"})\r\n */\r\nabstract class Vehicle {\r\n\r\n    public $id;\r\n\r\n\r\n}\r\nclass Car extends Vehicle {\r\n\r\n    /**\r\n     *@Type(\"integer\")\r\n    */\r\n    public $id;\r\n    /**\r\n     *@Type(\"string\")\r\n     */\r\n    public $car_at;\r\n}\r\nclass Moped extends Vehicle {\r\n\r\n    /**\r\n     *@Type(\"integer\")\r\n     */\r\n    public $id;\r\n    /**\r\n     *@Type(\"string\")\r\n     */\r\n    public $moped_at;\r\n\r\n}\r\n```\r\n\r\nexample usage:\r\n\r\n\r\n```php\r\n        $car = new Car();\r\n        $car->id = 1; \r\n        $car->car_at = \"car\";\r\n      \r\n        $car2 = new Moped();\r\n        $car2->id = 2; \r\n        $car2->moped_at = \"moped\";\r\n   \r\n\r\n        $saloon = new Saloon();\r\n        $sal0on->veichle = $car2;\r\n        $serializer = SerializerBuilder::create()->build();\r\n        $jsonveic = $serializer->serialize($saloon,'json');\r\n\r\n        $saloon_des = $serializer->deserialize($jsonveic,'MyME\\HelloBundle\\Controller\\Saloon','json');\r\n```\r\nThe error is:\r\nThe discriminator field name \"type\" for base-class \"MyME\\HelloBundle\\Controller\\Vehicle\" was not found in     input data\r\n        \r\n\r\n"},"comment":{"url":"https://api.github.com/repos/schmittjoh/JMSSerializerBundle/issues/comments/212902842","html_url":"https://github.com/schmittjoh/JMSSerializerBundle/issues/292#issuecomment-212902842","issue_url":"https://api.github.com/repos/schmittjoh/JMSSerializerBundle/issues/292","id":212902842,"user":{"login":"MlleDelphine","id":11458134,"avatar_url":"https://avatars.githubusercontent.com/u/11458134?v=3","gravatar_id":"","url":"https://api.github.com/users/MlleDelphine","html_url":"https://github.com/MlleDelphine","followers_url":"https://api.github.com/users/MlleDelphine/followers","following_url":"https://api.github.com/users/MlleDelphine/following{/other_user}","gists_url":"https://api.github.com/users/MlleDelphine/gists{/gist_id}","starred_url":"https://api.github.com/users/MlleDelphine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MlleDelphine/subscriptions","organizations_url":"https://api.github.com/users/MlleDelphine/orgs","repos_url":"https://api.github.com/users/MlleDelphine/repos","events_url":"https://api.github.com/users/MlleDelphine/events{/privacy}","received_events_url":"https://api.github.com/users/MlleDelphine/received_events","type":"User","site_admin":false},"created_at":"2016-04-21T12:47:46Z","updated_at":"2016-04-21T12:47:46Z","body":"Ok ! Thanks to @molchanoviv I found a full workaround (serialize / deserialize) to that issue. I created a listener which listen to both PreSerialize and PostSerialize events :\r\n\r\n```\r\nnamespace App\\Bundle\\AppBundle\\EventListener;\r\n\r\nuse JMS\\Serializer\\EventDispatcher\\Events;\r\nuse JMS\\Serializer\\EventDispatcher\\EventSubscriberInterface;\r\nuse JMS\\Serializer\\EventDispatcher\\ObjectEvent;\r\nuse JMS\\Serializer\\EventDispatcher\\PreSerializeEvent;\r\n\r\nclass JMSSerializerListener implements EventSubscriberInterface\r\n{\r\n    public static function getSubscribedEvents()\r\n    {\r\n        return [\r\n            ['event' => Events::PRE_SERIALIZE, 'method' => 'onPreSerialize'],\r\n            ['event' => Events::POST_SERIALIZE, 'method' => 'onPostSerialize']\r\n        ];\r\n    }\r\n\r\n    /**\r\n     * @param PreSerializeEvent $event\r\n     */\r\n    public function onPreSerialize(PreSerializeEvent $event)\r\n    {\r\n        $object = $event->getObject();\r\n\r\n        if (is_object($object) &&\r\n            is_subclass_of($object, 'App\\Bundle\\AppBundle\\Entity\\ParentEntity')  &&\r\n            get_class($object) !== $event->getType()['name']\r\n        ) {\r\n            $event->setType(get_class($event->getObject()));\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @param ObjectEvent $event\r\n     */\r\n    public function onPostSerialize(ObjectEvent $event){\r\n        $object = $event->getObject();\r\n        if (is_object($object) &&\r\n            (is_a($object, 'App\\Bundle\\AppBundle\\Entity\\ChildEntity')\r\n                || is_a($object, 'App\\Bundle\\AppBundle\\Entity\\ChildEntity2') ) ) {\r\n            $event->getVisitor()->addData(\"serializedType\", $object->getType());\r\n        }\r\n    }\r\n}\r\n```\r\n**Listener definition :**\r\n\r\n```\r\n<?xml version=\"1.0\" ?>\r\n\r\n<container xmlns=\"http://symfony.com/schema/dic/services\"\r\n           xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n           xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">\r\n\r\n    <parameters>      \r\n        <parameter key=\"cfa.events.jmsserializer_listener.class\">App\\Bundle\\AppBundle\\EventListener\\JMSSerializerListener</parameter>\r\n    </parameters>\r\n\r\n    <services>\r\n         <service id=\"cfa.events.jmsserializer_listener\" class=\"%cfa.events.jmsserializer_listener.class%\">\r\n            <tag name=\"jms_serializer.event_subscriber\"/>\r\n        </service>\r\n\r\n    </services>\r\n</container>\r\n```\r\n\r\n**Where :**\r\n\r\n - `ParentEntity` is abstract class with : \r\n\r\n```\r\n/**\r\n * @JMSSer\\ExclusionPolicy(\"all\")\r\n * @JMSSer\\Discriminator(field=\"serializedType\", map={\r\n *      \"child\"=\"App\\Bundle\\AppBundle\\Entity\\ChildEntity\",\r\n *      \"child2\"=\"App\\Bundle\\AppBundle\\Entity\\ChildEntity2\",\r\n * })\r\n```\r\n\r\n - `ChildEntity` and `ChildEntity2` extend `ParentEntity`\r\n\r\nNo need to use `@JMSSerialize\\VirtualProperty` anywhere.\r\n\r\nMaybe I miss something but it works really well like that and for both serialization and deserialization !"}}