Search code examples
openapiswagger-codegen

swagger-codegen-cli convert v3 openapi.json to static html


I have an openapi.json file (generated at build time), which is v3 format. Can I use swagger-codegen-cli to generate a static website for the swagger docs?

The goal is to generate both the apidoc.json and html at build time.

Example CLI command?

The following command errors saying I don't have a swagger input:

java -jar ./lib/swagger-codegen-cli-2.2.1.jar generate -i src/main/resources/static/apidoc/openapi.json -l html2 -o target/generated-sources/swagger
[main] INFO io.swagger.parser.Swagger20Parser - reading from src/main/resources/static/apidoc/openapi.json
[main] INFO io.swagger.parser.Swagger20Parser - reading from src/main/resources/static/apidoc/openapi.json
Exception in thread "main" java.lang.RuntimeException: missing swagger input or config!
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:132)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:223)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:36)

The openapi.json file does not start with "swagger". Do I need to convert it? Here's the opening of it:

{"openapi":"3.0.1","info":{"title"

Solution

  • Swagger Codegen v. 2 does not support OpenAPI 3.0. You need to use Codegen version 3.x.

    You can download the latest 3.x CLI JAR from Maven Central:
    https://mvnrepository.com/artifact/io.swagger.codegen.v3/swagger-codegen-cli