protoc-gen-openapi is a plugin for the Google protocol buffer compiler to generate
openAPI V3 spec for any given input protobuf. It runs as a protoc-gen- binary that the
protobuf compiler infers from the openapi_out flag.
protoc-gen-openapi is written in Go, so ensure that is installed on your system. You
can follow the instructions on the golang website or
on Debian or Ubuntu, you can install it from the package manager:
sudo apt-get install -y golangTo build, first ensure you have the protocol compiler (protoc):
go get github.com/golang/protobuf/protoTo build, run the following command from this project directory:
go buildThen ensure the resulting protoc-gen-openapi binary is in your PATH. A recommended location
is $HOME/bin:
cp protoc-gen-openapi $HOME/binSince the following is often in your $HOME/.bashrc file:
export PATH=$HOME/bin:$PATHTIP
The -I option in protoc is useful when you need to specify proto paths for imports.
Then to generate the OpenAPI spec of the protobuf defined by file.proto, run
protoc --openapi_out=output_directory input_directory/file.protoWith that input, the output will be written to
output_directory/file.json
Other supported options are:
per_file- when set to
true, the output is per proto file instead of per package.
- when set to
single_file- when set to
true, the output is a single file of all the input protos specified.
- when set to
use_ref- when set to
true, the output uses the$reffield in OpenAPI spec to reference other schemas.
- when set to
yaml- when set to
true, the output is in yaml file.
- when set to