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

如何在OpenAPI 3中使用$ref创建响应并添加额外属性?

实现目标响应的OpenAPI语法

你需要把响应的schema从直接定义数组,改成包含code、message、data三个属性的对象,其中data字段对应原有的患者数组。修改后的YAML代码如下:

description: Success get all patients
content:
  application/json:
    schema:
      type: object
      required:
        - code
        - message
        - data
      properties:
        code:
          type: integer
          example: 200
        message:
          type: string
          example: "Success get all patients data"
        data:
          type: array
          items:
            $ref: '#/components/schemas/Patient'  # 替换为你实际定义的患者组件引用,原代码里的InventoryItem应为笔误

关键说明:

  • 将schema的type从array改为object,匹配你想要的外层JSON对象结构
  • 通过required字段声明三个必填属性,确保响应结构的完整性
  • data字段保留原有的数组类型,内部依然复用你已创建的Patient组件定义
  • 添加example字段可以为每个属性提供示例值,让接口文档更直观

内容的提问来源于stack exchange,提问作者Mikeul

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 15:20:54