https://github.com/jaqx0r/rules_oapi_codegen
Bazel rules for generating API service handlers with `oapi-codegen`.
https://github.com/jaqx0r/rules_oapi_codegen
bazel oapi-codegen openapi rules yaml
Last synced: 5 months ago
JSON representation
Bazel rules for generating API service handlers with `oapi-codegen`.
- Host: GitHub
- URL: https://github.com/jaqx0r/rules_oapi_codegen
- Owner: jaqx0r
- License: apache-2.0
- Created: 2025-08-22T05:34:49.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-08-22T07:17:59.000Z (5 months ago)
- Last Synced: 2025-08-22T07:28:12.768Z (5 months ago)
- Topics: bazel, oapi-codegen, openapi, rules, yaml
- Language: Starlark
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rules_oapi_codegen
Bazel rules for generating API service handlers from OpenAPI yaml with [`oapi-codegen`](https://github.com/oapi-codegen/oapi-codegen).
## Rules
### `oapi_codegen`
```starlark
load("@rules_oapi_codegen//:defs.bzl", "oapi_codegen")
oapi_codegen(
name = "generate_api_handler",
src = "api.yaml",
generate = "gin", # your server framework of choice
out = "api.go",
strict = True, # default is True
models = True, # default is True
)
```
`oapi_codegen` invokes the `oapi-codegen` binary on your source openapi yaml description, generating a service handler for that specification into the output file.
The generated file is not scanned for imports by `bazel run @rules_go//go -- mod tidy` nor `bazel run //:gazelle` so you will have to manually copy the imports from your generated file once:
1. ```shell
bazel build //:generate_api_handler
```
2. Inspect the output in `bazel-bin/api.go`
3. Create an import-only file:
```go
package api
import (
_ "github.com/gin/gin-gonic"
... etc
)
```
4. Add the import-only source file to your BUILD rule and run `mod tidy` and `gazelle`.
The bug this workaround addresses is tracked in https://github.com/bazel-contrib/bazel-gazelle/issues/1801