Skip to content

jaqx0r/rules_oapi_codegen

Repository files navigation

rules_oapi_codegen

Bazel rules for generating API service handlers from OpenAPI yaml with oapi-codegen.

Rules

oapi_codegen

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. bazel build //:generate_api_handler
  2. Inspect the output in bazel-bin/api.go

  3. Create an import-only file:

    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 bazel-contrib/bazel-gazelle#1801

About

Bazel rules for generating API service handlers with `oapi-codegen`.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •