7

I am specifying my APIs using swagger I was using 2.0 now there is new version 3.0.0 according to the documentation I have specified 3.0.0 specification using offline swagger editor. Once it was ready I downloaded json file using which I am going to generate spring server code. I downloaded the swagger-codegen

Built it using mvn clean package then I executed following command :

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

Above command gives following error to me :

[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)

Update :

My swagger.json file as follows ( This is my trial project hence I have pasted my api structure here ):

{
  "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"
      }
    }
  }
}

I have tried building the code-gen-cli on 3.0.0 branch also which is giving same error.

Prashant
  • 4,474
  • 8
  • 34
  • 82
  • Did you use the [3.0.0-SNAPSHOT](https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen-cli/3.0.0-SNAPSHOT/) version? Can you post your swagger.json file? – Helen Nov 15 '17 at 08:27
  • @Helen please have look at the updated questions the 3.0.0-SNAPSHOT didn't worked for me giving same error. Thanks for response. – Prashant Nov 15 '17 at 08:55
  • You may want to try to build from https://github.com/swagger-api/swagger-codegen/tree/3.0.0_enhancements, which is still work-in-progress. – William Cheng Nov 15 '17 at 16:44
  • @WilliamCheng thanks for response 3.0.3_enhancement also do not work saw [github issue](https://github.com/swagger-api/swagger-codegen/issues/4669) it seems work in progress – Prashant Nov 16 '17 at 09:04
  • 2
    I tried exactly the same thing and first got exactly the same error building `swagger-codegen-cli` from the `master` branch. But then, building again from the `3.0.0` branch, the generation was successful. – anothernode May 23 '18 at 14:55
  • Hi I had the same error. However, this was only when I used a certain version of my input swagger.json file. I suspect my file had some invalid contents that choked swagger, but the error message was a bit cryptic to understand what went wrong. I did try the various debug flags (e.g. `-DdebugModels=true`) as documented at https://github.com/swagger-api/swagger-codegen/wiki/FAQ but none of them print more useful info. Thanks! – leeyuiwah Dec 14 '18 at 15:56

2 Answers2

7

I know this is really late to answer this but I solved it with one simple change.

in your json file

change

{
  "openapi": "3.0.0",
  "info": {

to this

{
  "swagger": "3.0.0",
  "info": {

and it will be resolved.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
3

As suggesed by anothernode in the question comments building again from 3.0.0 branch generates successfully:

$ git checkout v3.0.0
$ mvn clean package
bitfox
  • 2,281
  • 1
  • 18
  • 17
Thiru
  • 1,380
  • 13
  • 21