Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Serializer issue - NotNormalizableValueException

I have an issue when I'm using the serializer with FOSRestBundle in Symfony 4.1

I have the following error message :

Could not normalize object of type App\Entity\Youp, no supporting normalizer found. Symfony\Component\Serializer\Exception\NotNormalizableValueException

I don't understand why I have this issue, Symfony's Serializer should have an serializer object or I miss something ?

See bellow my controller and my entity :

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\YoupRepository")
 */
class Youp
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }
}

<?php 

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations as Rest;

use App\Entity\Youp;

class BidonController extends FOSRestController {

  /**
   * @Rest\View()
   * @Rest\Get("/youps")
   */
  public function getPharmacies() {
    $youps = $this->getDoctrine()->getRepository(Youp::class)->findAll();
    return $youps; 
  }
}
like image 236
edwix Avatar asked Oct 17 '18 18:10

edwix


People also ask

When to use the Symfony serializer normalizer?

This normalizer is particularly helpful when you want to gradually migrate from an existing codebase using simple json_encode to the Symfony Serializer by allowing you to mix which normalizers are used for which classes. Unlike with json_encode circular references can be handled.

What is objectnormalizer in Symfony?

The ObjectNormalizer also takes care of methods starting with has, can , add and remove. The support of canners (methods prefixed by can) was introduced in Symfony 6.1. When serializing, you can set a callback to format a specific object property: Normalizers turn objects into arrays and vice versa.

How to DENORMALIZE data in Symfony?

It is configured by default in Symfony applications with the Serializer component enabled. This normalizer reads the content of the class by calling the "getters" (public methods starting with "get"). It will denormalize data by calling the constructor and the "setters" (public methods starting with "set").

What is the serializer component?

The Serializer Component¶. The Serializer component is meant to be used to turn objects into a specific format (XML, JSON, YAML, ...) and the other way around. In order to do so, the Serializer component follows the following schema. As you can see in the picture above, an array is used as an intermediary between objects and serialized contents.


1 Answers

Your object's properties are private so the serializer doesn't know how to normalize or get any data from your object. You can either set the properties to public or enable the ObjectNormalizer (which uses the PropertyAccess Component to access the private/protected properties) and/or GetSetMethodNormalizer (which reads the content of the class by calling the "getters") using the following service-definition in your configuration:

services:
  # [..]
  Symfony\Component\Serializer\Normalizer\ObjectNormalizer:
    class: Symfony\Component\Serializer\Normalizer\ObjectNormalizer
    public: false
    tags:
      - { name: 'serializer.normalizer' }

  Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer:
    class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
    public: false
    tags:
      - { name: 'serializer.normalizer' }

Clear your cache afterwards. More information about the normalizers already included in the serializer component can be found in the documentation

like image 106
Nicolai Fröhlich Avatar answered Oct 01 '22 02:10

Nicolai Fröhlich