1
votes

Symfony 3.4.10
Api-platform/core

I set up API-Platform/core on a Symfony 3.4.10 project, everything work well and I can access the swagger page for test via "/api" (url route to show api test interface).

For the GET operations everything is right. But for the POST operation on an entity named "Trajet" (see code) the "example value" show only "{}" and nothing else. If I try the button "Try it out" I get a blank space where nothing is shew instead of the traditional form to test a post operation (see capture).

capture of the swagger page:
capture of the swagger page

I don't understand what I missed. Could someone please give me ideas how to fix it and where the problem can come from. I'm out of ideas to find a solution.

Here is the code of my entity Trajet for which post operation is empty and seem to be not usable.

<?php

namespace Covoituragesimple\AnnonceBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Trajet
 * @ApiResource(
 *  collectionOperations={"get", "post"},
 *  itemOperations={"get"},
 *      attributes={
 *          "normalization_context"={"groups"={"read"}},
 *          "denormalization_context"={"groups"={"write"}}
 *      }
 * )
 * @ORM\Table(name="trajet")
 * @ORM\Entity(repositoryClass="Covoituragesimple\AnnonceBundle\Repository\TrajetRepository")
 */
class Trajet
{   
   /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"read"})
     */
    private $id;

  
    /**
     * @var int
     *
     * @ORM\Column(name="numberofseats", type="integer")
     * @Assert\NotBlank()
     * @Groups({"read"})
     */
    private $numberofseats;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="creationdate", type="datetime")
     * @Assert\Type("\DateTime")
     * @Groups({"read"})
     */
    private $creationdate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="rdvdatetime", type="datetime")
     * @Assert\Type("\DateTime")
     * @Groups({"read"})
     */
    private $rdvdatetime;


    /**
    * @ORM\ManyToOne(targetEntity="Covoituragesimple\AnnonceBundle\Entity\Commune") 
    */
    private $commune;
   
    /**
     * @var string
     *
     * @ORM\Column(name="chercheoupropose", type="text")
     * @Groups({"read"})
     */
    private $chercheoupropose;

   
    /**
     * @var string
     *
     * @ORM\Column(name="message", type="text")
     * @Assert\NotBlank(
     *     message="Un petit message pour votre annonce"
     * )
     * @Groups({"read"})
     */
    private $message;

    /**
     * @var string
     *
     * @ORM\Column(name="contrepartietype", type="text")
     * @Groups({"read"})
     */
    private $contrepartietype;

    /**
    * @ORM\ManyToOne(targetEntity="Covoituragesimple\AnnonceBundle\Entity\Module")
    * @Groups({"read"}) 
    */
    protected $module;

    /**
     * @var string
     *
     * @ORM\Column(name="contrepartiemsg", nullable=true, type="text")
     * @Groups({"read"})
     */
    private $contrepartiemsg;


    /**
    * @ORM\ManyToOne(targetEntity="Covoituragesimple\AnnonceBundle\Entity\Covoitureur",
        inversedBy="trajets",
        cascade={"persist"}) 
    */
    protected $covoitureur;

    /**
    * @ORM\OneToMany(targetEntity="Covoituragesimple\AnnonceBundle\Entity\Contact", mappedBy="trajet", cascade={"remove"})
    */
    private $contacts; 


    /**
     * @var bool
     *
     * @ORM\Column(name="moderated", type="boolean")
     */
    private $moderated = false;

    /**
     * @var bool
     *
     * @ORM\Column(name="active", type="boolean")
     */
    private $active = true;

    
     /**
     * Constructor
     */
    public function __construct()
    {
        $this->setNumberofseats(1);
        $this->setMessage("");  
        $this->setRdvdatetime(new \DateTime('today'));
        $this->setCreationdate(new \DateTime('now'));
        $this->contacts = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set numberofseats.
     *
     * @param int $numberofseats
     *
     * @return Trajet
     */
    public function setNumberofseats($numberofseats)
    {
        $this->numberofseats = $numberofseats;

        return $this;
    }

    /**
     * Get numberofseats.
     *
     * @return int
     */
    public function getNumberofseats()
    {
        return $this->numberofseats;
    }

    /**
     * Set creationdate.
     *
     * @param \DateTime $creationdate
     *
     * @return Trajet
     */
    public function setCreationdate($creationdate)
    {
        $this->creationdate = $creationdate;

        return $this;
    }

    /**
     * Get creationdate.
     *
     * @return \DateTime
     */
    public function getCreationdate()
    {
        return $this->creationdate;
    }

    /**
     * Set rdvdatetime.
     *
     * @param \DateTime $rdvdatetime
     *
     * @return Trajet
     */
    public function setRdvdatetime($rdvdatetime)
    {
        $this->rdvdatetime = $rdvdatetime;

        return $this;
    }

    /**
     * Get rdvdatetime.
     *
     * @return \DateTime
     */
    public function getRdvdatetime()
    {
        return $this->rdvdatetime;
    }

    
    /**
     * Set chercheoupropose.
     *
     * @param string $chercheoupropose
     *
     * @return Trajet
     */
    public function setChercheoupropose($chercheoupropose)
    {
        $this->chercheoupropose = $chercheoupropose;

        return $this;
    }

    /**
     * Get chercheoupropose.
     *
     * @return string
     */
    public function getChercheoupropose()
    {
        return $this->chercheoupropose;
    }

    /**
     * Set message.
     *
     * @param string $message
     *
     * @return Trajet
     */
    public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

    /**
     * Get message.
     *
     * @return string
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * Set contrepartietype.
     *
     * @param string $contrepartietype
     *
     * @return Trajet
     */
    public function setContrepartietype($contrepartietype)
    {
        $this->contrepartietype = $contrepartietype;

        return $this;
    }

    /**
     * Get contrepartietype.
     *
     * @return string
     */
    public function getContrepartietype()
    {
        return $this->contrepartietype;
    }

    /**
     * Set contrepartiemsg.
     *
     * @param string|null $contrepartiemsg
     *
     * @return Trajet
     */
    public function setContrepartiemsg($contrepartiemsg = null)
    {
        $this->contrepartiemsg = $contrepartiemsg;

        return $this;
    }

    /**
     * Get contrepartiemsg.
     *
     * @return string|null
     */
    public function getContrepartiemsg()
    {
        return $this->contrepartiemsg;
    }

    /**
     * Set moderated.
     *
     * @param bool $moderated
     *
     * @return Trajet
     */
    public function setModerated($moderated)
    {
        $this->moderated = $moderated;

        return $this;
    }

    /**
     * Get moderated.
     *
     * @return bool
     */
    public function getModerated()
    {
        return $this->moderated;
    }

    /**
     * Set active.
     *
     * @param bool $active
     *
     * @return Trajet
     */
    public function setActive($active)
    {
        $this->active = $active;

        return $this;
    }

    /**
     * Get active.
     *
     * @return bool
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Set module.
     *
     * @param \Covoituragesimple\AnnonceBundle\Entity\Module|null $module
     *
     * @return Trajet
     */
    public function setModule(\Covoituragesimple\AnnonceBundle\Entity\Module $module = null)
    {
        $this->module = $module;

        return $this;
    }

    /**
     * Get module.
     *
     * @return \Covoituragesimple\AnnonceBundle\Entity\Module|null
     */
    public function getModule()
    {
        return $this->module;
    }

    /**
     * Set covoitureur.
     *
     * @param \Covoituragesimple\AnnonceBundle\Entity\Covoitureur|null $covoitureur
     *
     * @return Trajet
     */
    public function setCovoitureur(\Covoituragesimple\AnnonceBundle\Entity\Covoitureur $covoitureur = null)
    {
        $this->covoitureur = $covoitureur;

        return $this;
    }

    /**
     * Get covoitureur.
     *
     * @return \Covoituragesimple\AnnonceBundle\Entity\Covoitureur|null
     */
    public function getCovoitureur()
    {
        return $this->covoitureur;
    }

    /**
     * Add contact.
     *
     * @param \Covoituragesimple\AnnonceBundle\Entity\Contact $contact
     *
     * @return Trajet
     */
    public function addContact(\Covoituragesimple\AnnonceBundle\Entity\Contact $contact)
    {
        $this->contacts[] = $contact;

        return $this;
    }

    /**
     * Remove contact.
     *
     * @param \Covoituragesimple\AnnonceBundle\Entity\Contact $contact
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeContact(\Covoituragesimple\AnnonceBundle\Entity\Contact $contact)
    {
        return $this->contacts->removeElement($contact);
    }

    /**
     * Get contacts.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getContacts()
    {
        return $this->contacts;
    }

    /**
     * Set commune.
     *
     * @param \Covoituragesimple\AnnonceBundle\Entity\Commune|null $commune
     *
     * @return Trajet
     */
    public function setCommune(\Covoituragesimple\AnnonceBundle\Entity\Commune $commune = null)
    {
        $this->commune = $commune;

        return $this;
    }

    /**
     * Get commune.
     *
     * @return \Covoituragesimple\AnnonceBundle\Entity\Commune|null
     */
    public function getCommune()
    {
        return $this->commune;
    }
}
1
I went to the slack chanel of API-Platform and get ideas of what to do or look for from a guy nickamed Dunglas (thanks to him). First idea was to try to remove the cache with rm -Rf. Done but not fixing the problem. Second idea: check anotation but annotation seems to be good. Third idea : be sure to have PropertyInfo installed. I used composer "require symfony/property-info" --> done but not fixing the problem neither. Updating symfony/symfony (v3.4.10 => v3.4.11): Installing symfony/property-info (v4.1.0) - PampleBee
Last idea from Dunglas : "you may try to download the standard distribution, paste your entities inside and check if the error perist". I'll do that and tell here. - PampleBee
did you ever managed to solve this? - AnonymousAngelo

1 Answers

0
votes

I think it's because you forget to define "write" group in your entity attribute, if you rename it by "read" in denormalization_context (or add "write" group in your entity attributes), it should work.

Something like this:

* @ApiResource(
*  collectionOperations={"get", "post"},
*  itemOperations={"get"},
*      attributes={
*          "normalization_context"={"groups"={"read"}},
*          "denormalization_context"={"groups"={"read"}} // <-- here
*      }
* )

More detail here: https://api-platform.com/docs/core/serialization

I hope this can help you