本文介绍了我如何注释作为值对象的属性,以便 API 平台为 swagger 文档生成其字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Station 的实体.

I have an Entity called Station.

这个实体有一个名为 attributes 的属性,它是 StationAttributes 值对象.

This entity has a property called attributes which is StationAttributes value object.

我尝试将属性设置为 StationAttributes:

I try to set the property to be StationAttributes:

/**
* @var StationAttributes
* @ORM\Column(name="attributes", type="station_attributes", nullable=true)
*/
private $attributes;

但是,API 平台会生成如下所示的 Station 模型:

However, the API Platform generates Station model that looks like this:

{
...
"attributes": "string"
}

我希望它是这样的:

{
...
"attributes": {
    "field": true,
    "field2": "value2",
  }
}

我怎样才能做到这一点?

How can I achieve this?

推荐答案

我继续将 StationAttributes 注册为 ApiResource/Model,然后我将 swagger 上下文添加到属性属性.

I went ahead and registered StationAttributes as ApiResource/Model and then I've added swagger context to attributes property.

/**
 * @var StationAttributes
 *
 * @ApiProperty(
 *     attributes={
 *         "swagger_context"={
 *              "$ref"="#/definitions/StationAttributes"
 *          }
 *     }
 * )
 *
 * @ORM\Column(name="attributes", type="station_attributes", nullable=true)
 */
private $attributes;

这篇关于我如何注释作为值对象的属性,以便 API 平台为 swagger 文档生成其字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 03:24