Symfony denormalization: Recursive Denormalization not working

I’am trying to desirialize the following Json structure into a Profile object

{
    "label": "lorem label",
    "info": {
        "name": "lorem name",
        "title": "lorem title"
    }
}

I have a class Profile

namespace AppDocument;

class profile
{
   /**
    * @var string
    */
   protected $label;

   /**
    * @var Info
    */
   protected $info;

// getters and setters
}

and a class Info

namespace AppDocument;

class Info
{
    /**
     * @var string
     */
    protected name;

    /**
     * @var string
     */
    protected title;

    // getters & setters
}

my controller’s code is the following

use SymfonyComponentSerializerEncoderJsonEncoder;
use SymfonyComponentPropertyInfoExtractorReflectionExtractor;
use SymfonyComponentPropertyInfoExtractorPhpDocExtractor;
use SymfonyComponentSerializerNormalizerObjectNormalizer;
use SymfonyComponentSerializerSerializer;
use AppDocument;

public function addProfile(Request $request, DocumentManager $dm, $profile_id)
{
    $phpDocNormalizer = new ObjectNormalizer(null, null, null, new PhpDocExtractor());
    $reflectionNormalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
    $serializer = new Serializer([$phpDocNormalizer, $reflectionNormalizer], [new JsonEncoder()]);

    $profile = $serializer->deserialize($request->getContent(), Profile::class, 'json');

    return new JsonResponse(Response::HTTP_CREATED);
}

As the symfony docs says, i have the phpDocExtractor set and working, and the doc annotations are set too, the PropertyInfo Component is installed too. But i keep getting this NotNormalizableValueException:

The type of the "info" attribute for class "AppDocumentProfile" must be one of "AppDocumentInfo" ("array" given).

I’ve been stuck with this one for a while now, any help is much appreciated.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *