API-платформа платформы POST операции swagger страница пуста - PullRequest
0 голосов
/ 05 июня 2018

Symfony 3.4.10 Api-платформа / core

Я настроил API-платформу / core для проекта Symfony 3.4.10, все работает хорошо, и я могу получить доступ к странице swagger для тестирования через "/ api"(url route, чтобы показать интерфейс тестирования API).

Для операций GET все в порядке.Но для операции POST над сущностью с именем «Trajet» (см. Код) «пример значения» показывает только «{}» и ничего больше.Если я попытаюсь нажать кнопку «Попробуйте», то получу пустое место, где ничего не показано, вместо традиционной формы для проверки почтовой операции (см. Захват).

захват страницы чванства

Я не понимаю, что я пропустил.Может кто-нибудь, пожалуйста, дайте мне идеи, как это исправить и откуда проблема может возникнуть.У меня нет идеи найти решение.

Вот код моей сущности Trajet, для которой после операции пусто и, похоже, ее нельзя использовать.

<?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 Ответ

0 голосов
/ 10 июня 2018

Я думаю, это потому, что вы забыли определить группу «write» в атрибуте сущности, если вы переименуете ее как «read» в denormalization_context (или добавите группу «write» в атрибутах сущности), она должна работать.

Примерно так:

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

Более подробно здесь: https://api -platform.com / docs / core / serialization

Я надеюсь, что этомогу вам помочь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...