response
|--metadata attribute 1 (string/int/object)
|--metadata attribute 2 (string/int/object)
|--body (object)
|--body attribute 1 (string/int/object)
|--body attribute 2 (string/int/object)
我已经尝试使用以下JSON在Swagger中定义了这个关系:
{
...
"definitions": {
"response": {
"allOf": [
{
"$ref": "#/definitions/response_header"
},
{
"properties": {
"body": {
"description": "The body of the response (not metadata)",
"schema": {
"$ref": "#/definitions/response_body"
}
}
}
}
]
},
"response_header": {
"type": "object",
"required": [
"result"
],
"properties": {
"result": {
"type": "string",
"description": "value of 'success', for a successful response, or 'error' if there is an error",
"enum": [
"error",
"success"
]
},
"message": {
"type": "string",
"description": "A suitable error message if something went wrong."
}
}
},
"response_body": {
"type": "object"
}
}
}
我尝试通过创建继承自body/header的不同body/header类来创建不同的响应,然后创建由相关header/body类组成的子响应类(在源代码底部显示)来创建不同的响应。但是,我确定这要么是做事情的错误方式,要么是我的实现有误。我一直无法找到swagger 2.0规范中继承的示例(如下所示),但已找到一个组合的示例。
我相当确信这个"鉴别器"起着很大的作用,但不确定我需要做什么。
问题
是否有人能向我展示如何在swagger 2.0(JSON)中实现组合+继承,最好通过“修复”下面的示例代码。如果我可以指定一个ErrorResponse类,它继承自response,其中header中的“result”属性始终设置为“error”,那就太好了。
{
"swagger": "2.0",
"info": {
"title": "Test API",
"description": "Request data from the system.",
"version": "1.0.0"
},
"host": "xxx.xxx.com",
"schemes": [
"https"
],
"basePath": "/",
"produces": [
"application/json"
],
"paths": {
"/request_filename": {
"post": {
"summary": "Request Filename",
"description": "Generates an appropriate filename for a given data request.",
"responses": {
"200": {
"description": "A JSON response with the generated filename",
"schema": {
"$ref": "#/definitions/filename_response"
}
}
}
}
}
},
"definitions": {
"response": {
"allOf": [
{
"$ref": "#/definitions/response_header"
},
{
"properties": {
"body": {
"description": "The body of the response (not metadata)",
"schema": {
"$ref": "#/definitions/response_body"
}
}
}
}
]
},
"response_header": {
"type": "object",
"required": [
"result"
],
"properties": {
"result": {
"type": "string",
"description": "value of 'success', for a successful response, or 'error' if there is an error",
"enum": [
"error",
"success"
]
},
"message": {
"type": "string",
"description": "A suitable error message if something went wrong."
}
}
},
"response_body": {
"type": "object"
},
"filename_response": {
"extends": "response",
"allOf": [
{
"$ref": "#definitions/response_header"
},
{
"properties": {
"body": {
"schema": {
"$ref": "#definitions/filename_response_body"
}
}
}
}
]
},
"filename_response_body": {
"extends": "#/definitions/response_body",
"properties": {
"filename": {
"type": "string",
"description": "The automatically generated filename"
}
}
}
}
}
图示更新
为了更好地阐明我的想法,我创建了下面这个非常基本的图示,旨在表明所有响应都是“响应”对象的实例,这些对象是通过(组合)使用任何响应头和响应体对象构建的。响应头和响应体对象可以扩展并插入到任何响应对象中,在文件名响应的情况下会使用基础响应体类的filename_response_body子对象。无论是错误响应还是成功响应都使用“响应”对象。