Недостающая гидра: всего предметов в API - PullRequest
0 голосов
/ 25 марта 2020

У меня сейчас проблема, которую мне не удалось объяснить. У меня есть сущность symfony / api-platform, определенная так:

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity
 * @ApiResource(
 *     normalizationContext={"groups"={"node_read"}},
 *     denormalizationContext={"groups"={"node_write"}},
 *     collectionOperations={
 *          "get"={"method"="GET"},
 *          "post"={"method"="POST", "security"="is_granted('ROLE_ADMIN')"}
 *     },
 *     itemOperations={
 *          "get"={"method"="GET"},
 *          "put"={"method"="PUT", "security"="is_granted('ROLE_ADMIN')"},
 *          "patch"={"method"="PATCH", "security"="is_granted('ROLE_ADMIN')"},
 *          "delete"={"method"="DELETE", "security"="is_granted('ROLE_ADMIN')"}
 *     }
 * )
 * @ApiFilter(ExistsFilter::class, properties={"parent"})
 */
class Node
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"node_read"})
     */
    private ?int $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"node_read", "node_write"})
     */
    private string $key;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"node_read", "node_write"})
     */
    private string $label;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"node_read", "node_write"})
     */
    private string $type;

    /**
     * One Category has Many Categories.
     *
     * @OneToMany(targetEntity="Node", mappedBy="parent")
     * @Groups({"node_read"})
     */
    private Collection $children;

    /**
     * Many Nodes have One Node.
     *
     * @ManyToOne(targetEntity="Node", inversedBy="children")
     * @JoinColumn(name="parent_id", referencedColumnName="id")
     * @Groups({"node_write"})
     */
    private ?Node $parent;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private string $description;

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

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

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

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

        return $this;
    }

    public function getChildren(): Collection
    {
        return $this->children;
    }

    public function setChildren(Collection $children): self
    {
        $this->children = $children;

        return $this;
    }

    public function getKey(): string
    {
        return $this->key;
    }

    public function setKey(string $key): self
    {
        $this->key = $key;

        return $this;
    }

    public function getLabel(): string
    {
        return $this->label;
    }

    public function setLabel(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function setType(string $type): self
    {
        $this->type = $type;

        return $this;
    }

    public function getDescription(): string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }
}

При выполнении вызова get для коллекции у меня разные результаты между средой разработки и производством.

В программе env, API отсутствует ключ метаданных гидры hydra:total.

Результаты в dev env:

context: "/api/contexts/Node"
@id: "/api/nodes"
@type: "hydra:Collection"
hydra:member: [{@id: "/api/nodes/1", @type: "Node", id: 1, key: "risk-of-rain-2", label: "Risk Of Rain 2",…},…]
hydra:totalItems: 2
hydra:view: {@id: "/api/nodes?exists%5Bparent%5D=false", @type: "hydra:PartialCollectionView"}
hydra:search: {@type: "hydra:IriTemplate", hydra:template: "/api/nodes{?exists[parent]}",…}

приводит к продукту env:

@id: "/api/nodes"
@type: "hydra:Collection"
hydra:member: [{@id: "/api/nodes/12", @type: "Node", id: 12, key: "feedback", label: "Feedback", type: "Game",…},…]
hydra:view: {@id: "/api/nodes?exists%5Bparent%5D=false", @type: "hydra:PartialCollectionView"}
hydra:search: {@type: "hydra:IriTemplate", hydra:template: "/api/nodes{?exists[parent]}",…}

Как вы в prod я вижу, что мне не хватает ключа hydra:totalItems: 2, который позволяет мне заставить реагировать администратора.

Я сравнил как проектную composer зависимость, так и PHP версию, а prod - ISO для dev env.

1 Ответ

0 голосов
/ 25 марта 2020

Я обнаружил, что моя проблема вообще не связана с предоставленным исходным кодом, но с развертыванием.

Rsyn c не удалил удаленный файл проекта, и у меня был переопределенный поставщик данных.

Решением было использование опции --delete в команде rsyn c.

...