我正试图创造一个swagger json并试图检查它的有效性 http://editor.swagger.io
验证json后,上述编辑器出现以下错误:
Schema error should NOT have additional properties
additionalProperty: components
Jump to line 0
如果由于某种原因,我无法在根级别定义一个名为components的元素,我将要使用某种json模式,对于requestBody的模式执行$ ref的正确方法是什么正如我打算做的API操作,如下面的示例所示。另外,你看到我的招摇json有什么问题吗?
我对swagger2.0的招摇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"
}
}
}
}
}
}
答案 0 :(得分:3)
您正在混合使用OpenAPI 3.0和2.0语法。 components
关键字用于OpenAPI 3.0。在OpenAPI / Swagger 2.0中,可重用模式位于definitions
:
"definitions": {
"myEndPoint": {
...
}
}
确保同时将$ref
更改为
"$ref": "#/definitions/myEndPoint"