swagger - Swagger 3.0 reDoc 鉴别器 JSON
问题描述
我目前正在编写 swagger 3.0 文档并使用 reDoc 为其呈现漂亮的 UI。我的文档中有一些场景,基于以前的属性枚举,我想显示不同的模式对象属性。可悲的是,我无法弄清楚如何在我的文档中正确地将它们连接在一起。到目前为止,我有以下测试端点:
{
"post": {
"operationId" : "test",
"summary": "test",
"description": "test",
"tags": [ "test" ],
"consumes": "application/json",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "./schemas/test1.json"
},
{
"$ref": "./schemas/test2.json"
}
],
"discriminator": {
"propertyName": "pet_type",
"mapping": {
"click": "./schemas/test1.json",
"open": "./schemas/test2.json"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
}
test1.json 看起来像这样:
{
"Cat": {
"type": "object",
"properties": {
"pet_type": {
"type": "string"
},
"hunts": {
"type": "boolean"
},
"age": {
"type": "integer"
}
},
"discriminator": {
"propertyName": "pet_type"
}
}
}
像这样的 test2.json:
{
"Dog": {
"type": "object",
"properties": {
"pet_type": {
"type": "string"
},
"bark": {
"type": "boolean"
},
"breed": {
"type": "string",
"enum": [
"Dingo",
"Husky",
"Retriever",
"Shepherd"
]
}
},
"discriminator": {
"propertyName": "pet_type"
}
}
}
期望的结果是基于枚举(reDoc 示例中的下拉菜单)在两个“测试”json 之间切换。为了得到这个结果,我缺少什么?您可以在特征部分(第一个 gif)下看到判别器结果的示例
解决方案
经过更多的挖掘,我能够找出问题所在……我的大部分结构。在我的 index.json 文件中,我更新了组件部分以指向包含架构的组件文件夹,如下所示:
"components": {
"$ref": "./components/test.json"
},
test.json 如下所示:
{
"schemas": {
"Refinance": {
"description": "A representation of a cat",
"allOf": [
{
"$ref": "#/schemas/Pet"
},
{
"type": "object",
"properties": {
"huntingSkill": {
"type": "string",
"description": "The measured skill for hunting",
"default": "lazy",
"enum": [
"clueless",
"lazy",
"adventurous",
"aggressive"
]
}
},
"required": [
"huntingSkill"
]
}
]
},
"Purchase": {
"description": "A representation of a dog",
"allOf": [
{
"$ref": "#/schemas/Pet"
},
{
"type": "object",
"properties": {
"packSize": {
"type": "integer",
"format": "int32",
"description": "The size of the pack the dog is from",
"default": 1,
"minimum": 1
},
"foobar": {
"type": "string",
"description": "some ol bullshit"
}
},
"required": [
"packSize"
]
}
]
},
"Pet": {
"type": "object",
"discriminator": {
"propertyName": "petType"
},
"properties": {
"petType": {
"description": "Type of a pet",
"type": "string"
}
},
"xml": {
"name": "Pet"
}
}
}
}
最后,端点的架构被引用如下:
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "../../index.json#/components/schemas/Pet"
}
}
}
},
推荐阅读
- docker - 从运行在 kubernetes 集群中的 docker 容器生成堆转储
- python - 在 pandas read_sql 中指定数据类型的最佳方法是什么?
- javascript - Vue:动态“v-slot = { value }”导致div被隐藏
- c++ - 为什么这个 C 函数计算 sqrt 不能用于小数?
- swift - 不可见时向上移动文本字段
- javascript - JQuery 选择器的 Angular 等价物
- django - 如何使 Django 管理面板中的字段仅在模型创建时才可写,而在其他时候只能读取?
- javascript - 将新文件添加到输入多个文件中
- javascript - 打字稿允许变量污染全局范围
- python - 想要将 python datetime.datetime(2021, 7, 28, 9, 39, 5, 601831) 转换为“2021-07-28 09:39:601831+00:00”