Swagger OpenAPI使用带有模式的对象而不是数组

798qvoo8  于 2023-08-05  发布在  其他
关注(0)|答案(2)|浏览(106)

我正在使用L5 SwaggerDarkOnLine来生成使用OpenApi原理图的Swagger文档。
要使用模式,我可以

@OA\Property(property="certification", type="array", @OA\Items(ref="#/components/schemas/Certification"))

字符串
它工作得很好,

"certification": [
    {
      "certification_id": 0,
      "name": "string"
    }
  ],


。但它会建立一个内含多个对象的数组区块,并在其中加上方括号。
我怎么用同样的工作却失去了阵列。比如说

@OA\Property(property="certification", type="object", @OA\Items(ref="#/components/schemas/Certification")),


以便去掉方括号并只显示类似对象。

"certification": {
      "certification_id": 0,
      "name": "string"
 }

1qczuiv0

1qczuiv01#

您可以:

@OA\Property(
  property="certification", 
  ref="#/components/schemas/Certification"
)

字符串
@OA\Items注解仅在要指定数组中的属性时使用(请参见数据类型:数组)。
在本例中,您只想描述一个对象,因此只需在属性中引用该对象的模式并删除@OA\Items

8nuwlpux

8nuwlpux2#

您可以像这样定义子属性:

@OA\Schema(
   schema="MySchema",
   type="object",
   @OA\Property(property="MyObject",type="object",
      @OA\Property(property="sub_prop1",type="string",example="active"),
      @OA\Property(property="value",type="integer",example="3"),
   ),
)

字符串
或通过使用

@OA\Schema(
   schema="MySchema",
   type="object",
   $ref: '#/components/schemas/MyObject'
)

相关问题