Swagger 3.0.0 代码生成失败 java.lang.RuntimeException:缺少 swagger 输入或配置

pcj*_*pcj 5 swagger openapi swagger-codegen

我正在使用 swagger 指定我的 API 我使用的是 2.0 现在有新版本 3.0.0 根据我使用离线 swagger 编辑器指定 3.0.0 规范的文档。准备好后,我下载了 json 文件,我将使用该文件生成 spring 服务器代码。我下载了swagger-codegen

使用构建它mvn clean package然后我执行了以下命令:

java -jar <PARENT_DIR>/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i <PARENT_DIR>/ServerCode/swagger.json -l spring -o <PARENT_DIR>/ServerCode
Run Code Online (Sandbox Code Playgroud)

上面的命令给了我以下错误:

[main] INFO io.swagger.parser.Swagger20Parser - reading from swagger.json
[main] INFO io.swagger.parser.Swagger20Parser - reading from swagger.json
[main] INFO io.swagger.codegen.ignore.CodegenIgnoreProcessor - No .swagger-codegen-ignore file found.
Exception in thread "main" java.lang.RuntimeException: missing swagger input or config!
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:723)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:285)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)
Run Code Online (Sandbox Code Playgroud)

更新 :

我的 swagger.json 文件如下(这是我的试验项目,因此我在这里粘贴了我的 api 结构):

{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "User Example",
    "license": {
      "name": "MIT"
    }
  },
  "servers": [
    {
      "url": "http://www.example.com//v1"
    }
  ],
  "paths": {
    "/user": {
      "post": {
        "summary": "API to create a new User",
        "operationId": "createUser",
        "tags": [
          "user"
        ],
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "description": "User data to be created",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/User"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Newly created User data",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/User"
                }
              }
            }
          },
          "404": {
            "description": "Unexpected Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AuthError"
                }
              }
            }
          },
          "500": {
            "description": "Unexpected Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/InternalServerError"
                }
              }
            }
          },
          "default": {
            "description": "Unexpected Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnexpectedError"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "User": {
        "required": [
          "id",
          "fname",
          "email"
        ],
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
          "fname": {
            "type": "string"
          },
          "lname": {
            "type": "string"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "phone": {
            "type": "string"
          }
        }
      },
      "UnexpectedError": {
        "properties": {
          "message": {
            "type": "string",
            "example": "Something went wrong"
          }
        }
      },
      "AuthError": {
        "properties": {
          "message": {
            "type": "string",
            "example": "Authorization failed"
          }
        }
      },
      "InternalServerError": {
        "properties": {
          "message": {
            "type": "string",
            "example": "There is server side error while processing your request"
          }
        }
      }
    },
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试在3.0.0分支上构建 code-gen-cli也给出了同样的错误。

Kri*_*hna 7

我知道现在回答这个问题真的很晚了,但我通过一个简单的改变解决了这个问题。

在你的 json 文件中

改变

{
  "openapi": "3.0.0",
  "info": {
Run Code Online (Sandbox Code Playgroud)

对此

{
  "swagger": "3.0.0",
  "info": {
Run Code Online (Sandbox Code Playgroud)

它将得到解决。


Thi*_*iru 3

正如另一个节点在问题评论中所建议的那样,从 3.0.0 分支再次构建成功生成:

$ git checkout v3.0.0
$ mvn clean package
Run Code Online (Sandbox Code Playgroud)