【发布时间】:2018-05-04 16:35:29
【问题描述】:
我正在为 Web 服务定义通用模式,并且我想将它们导入规范的组件/模式部分。 我想创建一个跨多个服务通用的规范数据模型,以避免在每个服务定义中重新定义相似的对象。
有没有办法做到这一点? 是否有类似于 XSD 对其导入标签所做的机制?
【问题讨论】:
标签: rest web-services xsd swagger openapi
我正在为 Web 服务定义通用模式,并且我想将它们导入规范的组件/模式部分。 我想创建一个跨多个服务通用的规范数据模型,以避免在每个服务定义中重新定义相似的对象。
有没有办法做到这一点? 是否有类似于 XSD 对其导入标签所做的机制?
【问题讨论】:
标签: rest web-services xsd swagger openapi
如果 Pet.yaml 不是一个普通对象而是有几个像这样的对象,则扩展 Helen 对那些从 Google 登陆这里的人的回答:
components:
schemas:
pet:
type: object
properties:
(...)
你可以这样引用它:
$ref: './common/Pet.yaml#/components/schemas/pet'
【讨论】:
您可以直接使用绝对或相对 URL $ref 外部 OpenAPI 架构对象:
responses:
'200':
description: OK
schema:
$ref: './common/Pet.yaml'
# or
# $ref: 'https://api.example.com/schemas/Pet.yaml'
Pet.yaml 包含的地方,例如:
type: object
properties:
id:
type: integer
readOnly: true
petType:
type: string
name:
type: string
required:
- id
- petType
- name
更多信息请参见Using $ref。
【讨论】: