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)下看到判别器结果示例

4

1 回答 1

0

经过更多的挖掘,我能够找出问题所在……我的大部分结构。在我的 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"
          }
        }
      }
    },
于 2018-10-17T19:47:53.507 回答