【发布时间】:2015-03-07 21:11:24
【问题描述】:
在我的“简化”API 中,所有响应都派生自(继承)基础“响应”类。响应类组成 由一个充满元数据的标头和包含用户请求的核心数据的主体组成。响应(在 JSON 中)的布局使得所有元数据都在第一个“层”上,并且正文是一个称为“正文”的单个属性
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 specification(如下所示)中找到继承示例,但找到了composition 的示例。
我很确定这个“鉴别器”有很大的作用,但不确定我需要做什么。
问题
有人可以告诉我应该如何在 swagger 2.0 (JSON) 中实现组合+继承,最好是通过“修复”下面的示例代码。如果我可以指定一个继承自响应的 ErrorResponse 类,其中标头中的“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"
}
}
}
}
}
图表更新
为了尝试阐明我想要什么,我创建了下面的非常基本的图表,旨在显示所有响应都是由(组合)使用 response_header 和 response_body 对象的任意组合构建的“响应”对象的实例化. response_header 和 response_body 对象可以扩展并插入到任何响应对象中,这是在使用基本 response_body 类的 filename_response_body 子类的 filename_response 的情况下完成的。错误响应和成功响应都使用“响应”对象。
【问题讨论】:
-
这里有一个作曲样本,但太糟糕了,不值得分享。我会研究你的规格应该是什么样子。请记住,用户界面目前不支持它,但在对 2.0 的完全支持可用时会支持。
-
在我深入研究之前,还有一件事——你在寻找组合还是继承?作文基本上是在说
I have the properties of X and my own properties.。继承表明关系X is my parent. I have its properties and my own.。如果您想说某组模型适用于正在使用的父级,则继承很有用。 -
我更希望通过这个示例一次性演示继承和组合的使用。显然,我意识到一个人可以轻松地单独使用其中任何一个,但在这种情况下,所有响应都是基本“响应”类的子级。响应类由另外两个对象“组成”,即标题和正文。
-
我可能还不清楚。继承是组合的延伸。如果有继承,就有组合。如果有组合,则不一定有继承。此外,在您的示例中,“响应”模型未在任何地方使用。我应该忽略它,只展示它的外观吗?
-
啊,没有意识到继承和组合之间的关系。所以使用继承来显示两者。关于未使用的响应模型,应与请求响应的 filename_response 子项中的“扩展”一起使用。
标签: swagger