如何从非资源对象中删除 hydra 属性?

问题描述

我有两个简单的实体:

产品实体:

/**
 * Product Entity.
 *
 * @ORM\Entity()
 * @ORM\Table(name="products")
 *
 * @ApiResource(
 *     shortName="ProductCatalog:Product",*     description="Entity responsible for Product.",*     routePrefix="/product-catalog",*     attributes={
 *          "security"="is_granted('ROLE_USER')"
 *     },*     normalizationContext={"groups"={"product:read"}},*     itemOperations={
 *          "get"={
 *              "normalization_context"={
 *                  "swagger_deFinition_name"="Read"
 *              },*              "openapi_context"={
 *                  "summary"="Returns data about product.",*                  "description"="Returns data about product."
 *              }
 *          }
 *     },*     collectionoperations={}
 * )
 */
class Product
{
    /**
     * Object identifier.
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id",type="integer")
     *
     * @Groups("product:read")
     */
    private ?int $id = null;

    /**
     * Assigned product category object.
     *
     * @ORM\ManyToOne(targetEntity=Category::class,inversedBy="products")
     * @ORM\JoinColumn(nullable=false)
     *
     * @Groups("product:read")
     */
    private ?Category $category = null;

    /**
     * Name of product.
     *
     * @ORM\Column(type="string",length=255,nullable=true)
     *
     * @Groups("product:read")
     */
    private ?string $name = null;

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

    public function getCategory(): ?Category
    {
        return $this->category;
    }

    public function getName(): ?string
    {
        return $this->name;
    }
}

和类别实体:

/**
 * Product category entity.
 *
 * @ORM\Entity()
 * @ORM\Table(name="product_categories")
 */
class Category
{
    /**
     * Object identifier.
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id",type="integer")
     *
     * @Groups("product:read")
     */
    private ?int $id = null;

    /**
     * Collection with assigned Product objects.
     *
     * @var Collection|Product[]
     *
     * @ORM\OnetoMany(targetEntity="Product",mappedBy="category",cascade={"persist","remove"})
     */
    private Collection $products;

    /**
     * Category name.
     *
     * @ORM\Column(type="string",nullable=true)
     *
     * @Groups("product:read")
     */
    private ?string $name = null;

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

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

    /**
     * @return Product[]|Collection
     */
    public function getProducts(): Collection
    {
        return $this->products;
    }

    public function getName(): ?string
    {
        return $this->name;
    }
}

Product 实体被标记为 Resource 并在 API 中公开,而 Category 实体是普通实体,不应在 API 中公开。

生成一个端点:/api/product-catalog/products/{id},如果我调用这个端点,我会得到:

{
    "@context": "/api/contexts/ProductCatalog:Product","@id": "/api/product-catalog/products/10","@type": "ProductCatalog:Product","id": 10,"category": {
        "@type": "Category","@id": "_:2172","id": 2,"name": "Wood"
    },"name": "Wooden chair"
}

我期望这样的输出

{
    "@context": "/api/contexts/ProductCatalog:Product","category": {
        "id": 2,"name": "Wooden chair"
}

我的问题是:如何从 Category 对象中删除 @type 和 @id 属性

提醒一下,Category 实体是一个与 API 无关的普通实体(未标记为 ApiResource)。

在我看来,如果我们在另一个对象(在 API 中公开)中使用某个对象(它是普通对象,未在 API 中公开),那么这个普通对象应该被视为简单的对象类型,如 string 或 int 或 boolean (无需创建架构、Hydra 属性等)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)