【问题标题】:Swagger 3.0 reDoc Discriminator JSONSwagger 3.0 reDoc 鉴别器 JSON
【发布时间】:2018-10-16 20:00:36
【问题描述】:

我目前正在编写 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 之间切换。为了得到这个结果,我缺少什么? 特征部分下可以看到判别器结果here的示例(第一个gif)

【问题讨论】:

    标签: swagger documentation swagger-3.0


    【解决方案1】:

    经过更多的挖掘,我能够找出问题所在……我的大部分结构。 在我的 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"
              }
            }
          }
        },
    

    【讨论】:

      猜你喜欢
      • 2022-12-22
      • 2017-02-02
      • 1970-01-01
      • 2015-02-21
      • 1970-01-01
      • 2019-08-23
      • 2022-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多