【问题标题】:Swagger Schema error should NOT have additional propertiesSwagger Schema 错误不应具有其他属性
【发布时间】:2017-11-14 19:32:47
【问题描述】:

我正在尝试创建 swagger json 并尝试检查它的有效性 http://editor.swagger.io

在验证 json 时,上述编辑器给出以下错误:

Schema error should NOT have additional properties
additionalProperty: components
Jump to line 0

如果由于某种原因我无法在根级别定义一个名为 components 的元素,我将在其中拥有某种 json 架构,那么在 API 操作的 requestBody 架构上执行 $ref 的正确方法是什么正如我打算在下面的示例中看到的那样。另外,你觉得我的 swagger json 有什么问题吗?

我的 swagger2.0 的 swagger json 看起来像这样。

{
    "swagger": "2.0",
    "info": {
        "version": "1.0",
        "title": "My swagger API",
        "contact": {
            "name": "myName",
            "email": "abc@gmail.com"
        }
    },
    "host": "localhost:1234",
    "basePath": "/",
    "tags": [{
        "name": "someTagName",
        "description": "this is a try"
    }],
    "components":{"schemas": {"myEndPoint": ""}},
    "paths": {
        "/myEndPoint": {
            "post": {
                "tags": ["some-tag"],
                "summary": "myEndPoint endpoint will give you something",
                "description": "some description will go here",
                "operationId": "getMyEndPoint",
                "consumes": ["application/json"],
                "produces": ["application/json"],
                "parameters": [{
                    "in": "body",
                    "name": "somePayload",
                    "description": "somePayload is what this is",
                    "required": true,
                    "schema": {
                        "$ref": "#components/schemas/myEndPoint"
                    }
                },
                {
                    "in": "header",
                    "name": "Authorization",
                    "description": "auth token goes here",
                    "required": true,
                    "type": "string"
                }],
                "responses": {
                    "200": {
                        "description": "Success",
                        "schema": {
                            "type": "string"
                        }
                    },
                    "400": {
                        "description": "Bad Request"
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: swagger-2.0 swagger-editor


    【解决方案1】:

    您混淆了 OpenAPI 3.0 和 2.0 语法。 components 关键字用于 OpenAPI 3.0。在 OpenAPI/Swagger 2.0 中,可重用模式位于 definitions 下:

    "definitions": {
      "myEndPoint": {
        ...
      }
    }
    

    确保还将$ref 更改为

    "$ref": "#/definitions/myEndPoint"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-24
      • 2018-01-14
      • 2021-04-01
      • 2020-12-10
      • 2021-03-30
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多