API平台POST操作swagger页面为空

时间:2018-06-05 12:57:28

标签: swagger symfony-3.4 api-platform.com

Symfony 3.4.10 API-平台/芯

我在symfony 3.4.10项目上设置了API-Platform / core,一切运行良好,我可以通过“/ api”访问swagger页面进行测试(url route to show api test interface)。

对于GET操作,一切都是正确的。 但对于名为“Trajet”的实体(请参阅代码)的POST操作,“示例值”仅显示“{}”而没有其他内容。如果我尝试按钮“试一试”,我会得到一个空白区域,其中没有任何东西可以代替传统形式来测试后期操作(参见捕获)。

capture of the swagger page

我不明白我错过了什么。有人可以给我一些想法如何解决它以及问题可能来自哪里。我不想找到解决方案。

这是我的实体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 :(得分:0)

我认为这是因为你忘了定义&#34;写&#34;您的实体属性中的组,如果您通过&#34;将其重命名为&#34;在denormalization_context中(或在实体属性中添加&#34;编写&#34;组),它应该可以正常工作。

这样的事情:

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

此处提供更多详细信息:https://api-platform.com/docs/core/serialization

我希望这可以帮到你