This project is looking for maintainers. Please refer to the announcement for more information.
This repo was created to integrate the OpenAPI code generation CLI with Bazel.
If you find this project useful, please kindly consider supporting this project financially via https://opencollective.com/openapi_generator 🙏
As of v0.1.8, this only supports Bazel Modules. The setup is much simpler.
- Add the following code to your
MODULE.bazelfile at the root of your repository
bazel_dep(name = "openapi_tools_generator_bazel", version = "0.2.1")
openapi_gen = use_extension("@openapi_tools_generator_bazel//:extension.bzl", "openapi_gen")
openapi_gen.client(
sha256 = "f18d771e98f2c5bb169d1d1961de4f94866d2901abc1e16177dd7e9299834721",
version = "6.5.0",
)
use_repo(openapi_gen, "openapi_tools_generator_bazel_cli")
# Or, to use the default client (currently 7.2.0), remove the parameters to client().
openapi_gen.client()
- Create a BUILD.bazel file next to the .yaml file you wish to generate code from.
The below example generates a go library within a generated directory named
petstore_go
load("@openapi_tools_generator_bazel//:defs.bzl", "openapi_generator")
openapi_generator(
name = "petstore_go",
generator = "go",
spec = "petstore.yaml",
)
- [Optional] Copy the output files to your codebase
openapi_generator(
name = "petstore_proto",
generator = "protobuf-schema",
spec = "petstore.yaml",
)
load("@openapi_tools_generator_bazel//:defs.bzl", "copy_files")
# Run this as
# bazel build :copy_files_out 2>&1 | grep -o 'bazel-bin/[^ ]*' | xargs -r bash
# bazel run :copy_files_out is blocked from writing to one's codebase
copy_files(
name = "copy_files_out",
srcs = ":petstore_proto",
regex_map = {
".*\\.proto": "protos/petstore"
}
)