我正在使用OpenAPI 3.0为我正在构建的服务定义API。我遇到了在其他组件中重用模式组件的问题。例如,我有一个Note对象,它包含创建便笺的人的Profile对象。这可以通过使用Profile关键字引用$ref对象来实现预期的效果。问题是当显示示例时,配置文件中没有任何数据,如果我将参考文件放在下面的示例中,它包括Profile的实际Profile块,而不仅仅是Profile组件的示例数据。
我想知道是否有一种方法可以在其他组件中重用组件,也可以重用这些组件上的示例?
Ex:
FullNote:
allOf:
- $ref: '#/components/schemas/BaseNote'
- type: object
title: A single note response
required:
- id
- dateCreated
- profile
properties:
id:
type: integer
format: int32
dateCreated:
type: integer
format: int64
profile:
type: object
$ref: '#/components/schemas/Profile'
example:
id: 123456789
dateCreated: 1509048083045
profile:
$ref: '#/components/schemas/Profile'发布于 2017-12-04 19:09:06
example关键字(不要与exampleS混淆)不支持$ref。整个示例需要内联指定:
FullNote:
allOf:
- $ref: '#/components/schemas/BaseNote'
- type: object
...
example:
id: 123456789
dateCreated: 1509048083045
influencer:
prop1: value1 # <----
prop2: value2或者,您也可以使用属性级别的示例-在本例中,像Swagger这样的工具将从属性示例构建模式示例。
FullNote:
allOf:
- $ref: '#/components/schemas/BaseNote'
- type: object
...
properties:
id:
type: integer
format: int32
example: 123456789 # <----
dateCreated:
type: integer
format: int64
example: 1509048083045 # <----
profile:
# This property will use examples from the Profile schema
$ref: '#/components/schemas/Profile'
Profile:
type: object
properties:
prop1:
type: string
example: value1 # <----https://stackoverflow.com/questions/47639926
复制相似问题