You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 04:42:38