我正在为Web服务定义公共模式,我想在规范的组件/模式部分中导入它们。 我想创建一个在多个服务中通用的规范数据模型,以避免在每个服务定义中重新定义类似的对象。
有办法做到这一点吗? 是否有与XSD对其导入标记的作用相似的机制?
答案 0 :(得分:1)
如果Pet.yaml不是普通对象,但具有多个类似这样的对象,那么对于从Google登陆到这里的人来说,要进一步了解Helen的答案:
components:
schemas:
pet:
type: object
properties:
(...)
您可以这样引用它:
$ref: './common/Pet.yaml#/components/schemas/pet'
答案 1 :(得分:0)
您可以使用绝对或相对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。