如何在OpenAPI响应中引用Customer Schema的customer_id字段?
可以引用Customer Schema的customer_id属性
当然可以,在OpenAPI 3.1中,你可以通过JSON Schema的$ref语法直接引用已有Schema里的具体属性。具体做法是使用$ref指向Customer schema中customer_id属性的路径:#/components/schemas/Customer/properties/customer_id。
修改后的接口定义如下:
paths: /customer/all: get: tags: - customer summary: Retrieve list of customer IDs description: Retrieve the IDs of all existing customers operationId: getCustomersById responses: '200': description: successful operation content: application/json: schema: type: array items: $ref: '#/components/schemas/Customer/properties/customer_id'
这么做的好处是:
- 避免重复定义
customer_id的类型、格式、描述等信息 - 当你后续修改
Customerschema中customer_id的定义时,这个响应的schema会自动同步更新,保证一致性
内容的提问来源于stack exchange,提问作者Saurabh
相关产品推荐
相关产品推荐

