如何解决如何从非资源对象中删除 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 举报,一经查实,本站将立刻删除。