首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swagger3.0 reDoc鉴别器JSON

Swagger3.0 reDoc鉴别器JSON
EN

Stack Overflow用户
提问于 2018-10-16 20:00:36
回答 1查看 642关注 0票数 0

我目前正在编写swagger3.0文档,并使用reDoc将其呈现为很好的UI。我的文档中有几种场景,其中基于以前的属性枚举,我希望显示不同的模式对象属性。可悲的是,我无法在我的文档中找到如何正确地将这些连接在一起。到目前为止,我有以下测试端点:

代码语言:javascript
复制
{
  "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如下所示:

代码语言:javascript
复制
{
  "Cat": {
    "type": "object",
    "properties": {
      "pet_type": {
        "type": "string"
      },
      "hunts": {
        "type": "boolean"
      },
      "age": {
        "type": "integer"
      }
    },
    "discriminator": {
      "propertyName": "pet_type"
    }
  }
}

测试2.json是这样的:

代码语言:javascript
复制
{
  "Dog": {
    "type": "object",
    "properties": {
      "pet_type": {
        "type": "string"
      },
      "bark": {
        "type": "boolean"
      },
      "breed": {
        "type": "string",
        "enum": [
          "Dingo",
          "Husky",
          "Retriever",
          "Shepherd"
        ]
      }
    },
    "discriminator": {
      "propertyName": "pet_type"
    }
  }
}

期望的输出将是基于枚举(在reDoc示例中看到的下拉列表)在两个"test“jsons之间切换。我错过了什么才能得到这个结果?您可以在特性部分(第一个gif)下面看到判别器结果这里的示例。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-17 19:47:53

经过更多的调查,我终于找到了这个问题.我的结构大部分都是。在我的index.json文件中,我更新了components部分,指向包含模式的components文件夹:

代码语言:javascript
复制
"components": {
   "$ref": "./components/test.json"
},

test.json看起来如下所示:

代码语言:javascript
复制
{
  "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"
      }
    }
  }
}

最后,引用端点的架构如下:

代码语言:javascript
复制
"requestBody": {
      "required": true,
      "content": {
        "application/json": {
          "schema": {
            "$ref": "../../index.json#/components/schemas/Pet"
          }
        }
      }
    },
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52843204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档