如何用swagger-php属性定义返回对象数组的响应?
修改OpenAPI响应为返回对象数组
直接调整代码里的两个关键部分,就能实现返回对象数组的效果:
#[OA.Response( response: 201, description: "Get the list", content: new OA.JsonContent( type: 'array', items: new OA.Items( properties: [ new OA.Property( property: "property name 1", type: "string", example: "value 1" ), new OA.Property( property: "property name 2", type: "string", example: "value 2" ) ], type: 'object' ) ) )]
修改说明:
- 将
JsonContent中的type: 'object'改为type: 'array',明确响应为数组类型 - 新增
items字段,通过OA.Items定义数组内每个元素的结构——也就是你原本写的单个对象的属性配置,保持内部type: 'object'不变
修改完成后,接口文档会正确标识该响应返回的是包含指定结构对象的数组。
内容的提问来源于stack exchange,提问作者Kees Klomp
相关产品推荐
相关产品推荐

