Swagger Schema 错误不应具有其他属性 [英] Swagger Schema error should NOT have additional properties
问题描述
我正在尝试创建 swagger json 并尝试检查它的有效性http://editor.swagger.io
I am trying to create swagger json and trying to check it's validity in http://editor.swagger.io
验证json后,上面提到的编辑器给出以下错误:
Upon validating the json, the above mentioned editor gives the following error:
Schema error should NOT have additional properties
additionalProperty: components
Jump to line 0
如果由于某种原因我无法在根级别定义名为 components 的元素,我将在其中拥有某种 json 架构,那么在 API 操作的 requestBody 架构上执行 $ref 的正确方法是什么正如我打算在下面的示例中看到的那样.另外,你看到我的 swagger json 有什么问题吗?
If for some reason I can't define an element named components at root level where i am going to have some sort of json schema, what is the right way to do a $ref on the schema for requestBody for an API operation as I intend to do as seen in my example below. Also, do you see anything wrong with my swagger json ?
我的 swagger2.0 的 swagger json 看起来像这样.
My swagger json for swagger2.0 look like this.
{
"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"
}
}
}
}
}
}
推荐答案
您正在混淆 OpenAPI 3.0 和 2.0 语法.components
关键字用于 OpenAPI 3.0.在 OpenAPI/Swagger 2.0 中,可重用模式存在于 definitions
下:
You are mixing up OpenAPI 3.0 and 2.0 syntax. The components
keyword is used in OpenAPI 3.0. In OpenAPI/Swagger 2.0, reusable schemas live under definitions
:
"definitions": {
"myEndPoint": {
...
}
}
确保也将 $ref
更改为
"$ref": "#/definitions/myEndPoint"
这篇关于Swagger Schema 错误不应具有其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!