gpt4 book ai didi

api-platform.com - "Parent"属性没有出现在 "Example Value"与 denormalizationContext 组

转载 作者:行者123 更新时间:2023-12-04 04:19:43 25 4
gpt4 key购买 nike

我使用 api-platform v3 和 symfony v5。我对显示的属性有疑问。例如,我们有简单的实体类类别:

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

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

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

/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
*/
private $parent;

/**
* @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
*/
private $children;

public function __construct()
{
$this->children = new ArrayCollection();
}

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

public function getTitle(): ?string
{
return $this->title;
}

public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}

public function getParent(): ?self
{
return $this->parent;
}

public function setParent(?self $parent): self
{
$this->parent = $parent;

return $this;
}

/**
* @return Collection|self[]
*/
public function getChildren(): Collection
{
return $this->children;
}

public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}

return $this;
}

public function removeChild(self $child): self
{
if ($this->children->contains($child)) {
$this->children->removeElement($child);
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}

return $this;
}
}

到目前为止,我们看到了正确且合乎逻辑的“示例值”: Intermediate result

现在我想显示 title 和 parent 属性以供读写。为此,我输入组注释:

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(
* normalizationContext={"groups" = {"category:read"}},
* denormalizationContext={"groups" = {"category:write"}}
* )
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
//...

/**
* @ORM\Column(type="string", length=255)
* @Groups({"category:read", "category:write"})
*/
private $title;

/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
* @Groups({"category:read", "category:write"})
*/
private $parent;

/**
* @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
*/
private $children;

//...
}

之后,我们在 swagger 文档中看到了“title”属性。但是“parent”属性是不可见的: Result after added groups annotation

为什么我在“示例值”部分看不到“父”属性?

作为临时解决方案,我描述了 documentation swagger:

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(
* collectionOperations={
* "get",
* "post" = {
* "sequrity" = "is_granted('ROLE_ADMIN')",
* "openapi_context" = {
* "requestBody" = {
* "content" = {
* "application/json" = {
* "schema" = {
* "type" = "object",
* "required" = {
* "title"
* },
* "properties" = {
* "title" = {
* "type" = "string"
* },
* "parent" = {
* "type" = "string"
* }
* }
* }
* }
* }
* }
* }
* }
* },
* normalizationContext={"groups" = {"category:read"}},
* denormalizationContext={"groups" = {"category:write"}}
* )
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
//...
}

我想知道如何正确地做这件事。为什么组不适用于“父”属性?

最佳答案

现在遇到了同样的问题。尝试为 $parent 属性使用注解 @ApiProperty(readableLink=false)

/**
* @ORM\ManyToOne(targetEntity=CounterpartCategory::class, inversedBy="children")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @Groups({"category:read", "category:write"})
* @ApiProperty(readableLink=false)
*/
private ?self $parent;

关于api-platform.com - "Parent"属性没有出现在 "Example Value"与 denormalizationContext 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com