Swagger 3.0 reDoc Discriminator JSON

时间:2018-10-16 20:00:37

标签: swagger documentation swagger-3.0

我目前正在编写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)下看到鉴别结果here的示例

1 个答案:

答案 0 :(得分:0)

经过进一步的挖掘,我能够找出问题所在……大部分是我的结构。 在index.json文件上,我更新了我的components部分,以指向包含如下模式的components文件夹:

"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"
          }
        }
      }
    },