在我的“简化” API中,所有响应都是从基本“响应”类派生(继承)的。响应类由填充有元数据的标头和包含用户请求的核心数据的正文组成。布置响应(以JSON格式),使得所有元数据都位于第一个“层”上,并且body是这样一个称为“ body”的单个属性
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来定义这种关系:
{
...
"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规范(如下所示)中找到继承的示例,但是找到了composition的示例。
我可以肯定的是,这个“判别器”有很大的作用,但是不确定我需要做什么。
题
有人可以告诉我如何在swagger 2.0(JSON)中实现构图和继承,最好是通过“固定”下面的示例代码来实现。如果我可以指定一个从响应继承的ErrorResponse类,那也很好,其中标题中的“结果”属性始终设置为“错误”。
{
"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"
}
}
}
}
}
图表更新
为了尝试阐明我想要的内容,我在下面创建了一个非常基本的图,该图旨在显示所有响应都是由(组合)使用response_header和response_body对象的任意组合构建的“ response”对象的实例。可以扩展response_header和response_body对象并将其插入到任何响应对象中,这在使用基本response_body类的filename_response_body子项的filename_response的情况下完成。错误响应和成功响应都使用“响应”对象。