Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Using Swagger Codegen

Once you have generated your swagger.json file, you can use it to generate your server stubs or client SDKs using Swagger codegen.

Here is an example that will generate PHP code.

docker run --rm \
  -v ${PWD}:/local \
  swaggerapi/swagger-codegen-cli-v3:3.0.5 generate \
  -i http://192.168.16.21/spec \
  -l "php" \
  -o /local/out/code

Alternative - Redoc

Alternatively, you can pass the swagger.json to Redoc for a different look:

docker run -d \
  --restart=always \
  -p 80:80 \
  -v $(pwd)/swagger.json:/usr/share/nginx/html/swagger.json \
  -e SPEC_URL=swagger.json \
  redocly/redoc

Offline Documentation

If you want to generate files you can send someone, rather than having to host a website, then you can use the Redoc CLI tool.

First, install the CLI tool through NPM:

sudo npm i -g redoc-cli

Then you can generate the documentation with:

redoc-cli bundle -o docs.html swagger.json

References

Last updated: 17th September 2020
First published: 14th March 2019