我正在尝试使用 Swagger 记录 REST API。来自我们 API 的简化 JSON 响应如下所示:
{
"data": {
"type": "person"
"id": "1"
"attributes": {
"name": "Joe"
"age": 32
...
}
"links": {
...
}
}
}
或者
{
"data": {
"type": "job"
"id": "22"
"attributes": {
"name": "Manager"
"location": "Somewhere"
...
}
"links": {
...
}
}
}
他们对成功 GET 的 Swagger 定义可能如下所示:
'200':
description: OK.
schema:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
attributes:
$ref: '#/definitions/person'
或者
'200':
description: OK.
schema:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
attributes:
$ref: '#/definitions/job'
在我们的 Swagger 文件中可能有很多这样的重复。是否可以定义这些响应以共享公共部分?即我不想输入或复制/粘贴这部分数十次:
'200':
description: OK.
schema:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
我看不出使用鉴别器字段或使用 $ref 将如何工作。