Swagger 编辑器总是在 json 上出错

Swagger editor always gives error on json

对我来说,没有一个 json 字符串可以与 swagger 编辑器一起使用。

这是我从网页上截取的一个例子:

{
"swaggerVersion": "2.0",
"basePath": "http://localhost:8000/greetings",
"apis": [
{
  "path": "/hello/{subject}",
  "operations": [
    {
      "method": "GET",
      "summary": "Greet our subject with hello!",
      "type": "string",
      "nickname": "helloSubject",
      "parameters": [
        {
          "name": "subject",
          "description": "The subject to be greeted.",
          "required": true,
          "type": "string",
          "paramType": "path"
        }
      ]
    }
  ]
}
],
"models": {}
}

所以当前的错误是:

✖ YAML Syntax Error
Missed comma between flow collection entries at line 2, column 14: "swagger: "2.0", ^

我不知道你从哪里获取基本代码,但语法似乎完全错误(至少对于 swagger 2),我建议你看一下 the official specification.

关于你粘贴的代码,我重构了它,试试这个:

{
    "swagger": "2.0",
    "host": "localhost:8000",
    "basePath": "/greetings",
    "info": {
        "title": "Some title",
        "version": "0.0.1"
    },
    "paths": {
        "/hello/{subject}": {
            "get": {
                "summary": "Greet our subject with hello!",
                "parameters": [{
                    "name": "subject",
                    "description": "The subject to be greeted.",
                    "required": true,
                    "type": "string",
                    "in": "path"
                }],
                "responses": {
                    "default": {
                        "description": "Some description",
                        "schema": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
}

即使 OP 的语法错误,这也是为正在寻找答案的其他人提供的,他们的语法是正确的

如果您从浏览器 获取 JSON 数据并且您有一个浏览器扩展可以很好地格式化和显示 JSON 数据,它可能在 Swagger 编辑器中粘贴时造成问题(由于格式化)。

如果是这种情况,请尝试禁用扩展程序或进入隐身模式。