openapi-gen-build (0.1.4)
Installation
[registries.forgejo]
index = "sparse+ " # Sparse index
# index = " " # Git
[net]
git-fetch-with-cli = truecargo add openapi-gen-build@0.1.4 --registry forgejoAbout this package
openapi-gen-build
Build-time Rust client generation from OpenAPI specs via openapi-generator-cli.
Usage
Add as a build dependency in Cargo.toml:
[build-dependencies]
openapi-gen-build = { path = "crates/openapi-gen-build" }
Then in build.rs:
use openapi_gen_build::Config;
fn main() {
Config::new(std::env::var("OUT_DIR").unwrap().into())
.spec("specs/api.yaml", "my_api")
.generate()
.emit_rerun_if_changed();
}
For generators that emit additional modules, such as rust-axum, configure the
generator and namespace explicitly:
use openapi_gen_build::Config;
fn main() {
Config::new(std::env::var("OUT_DIR").unwrap())
.spec("specs/api.yaml", "contract")
.namespace("crate::generated")
.generator("rust-axum")
.generate()
.expect("OpenAPI generation failed")
.emit_rerun_if_changed();
}
The generated contract module is discovered from the generator output. Every
top-level Rust module is declared automatically, including nested modules with
their own mod.rs files. For example, an Axum output containing apis.rs,
models.rs, types.rs, header.rs, and server/mod.rs produces:
generated/
mod.rs
contract/
mod.rs
apis.rs
models.rs
types.rs
header.rs
server/
mod.rs
No consumer-side mod.rs or filesystem writes are required. Generated
crate:: paths are rewritten to the configured namespace, including grouped
imports such as use crate::{apis, models};.
Generated modules are placed under crate::generated::<name> by default. Include
them from your library:
mod generated;
// use generated::my_api::apis::...;
Configuration
| Method | Default | Description |
|---|---|---|
new(output_dir) |
— | Output directory (OUT_DIR) |
.spec(input, name) |
— | Register an OpenAPI spec |
.discover_specs(dir) |
— | Auto-discover *.json/*.yaml/*.yml in a directory |
.namespace(ns) |
"crate::generated" |
Namespace prefix for generated modules |
.generator(g) |
"rust" |
Generator language |
.additional_property(k, v) |
— | Single --additional-properties entry |
.additional_properties([...]) |
— | Bulk-set all additional properties |
.allow_clippy(bool) |
true |
Prepend #![allow(clippy::all)] to generated .rs files |
.generate() |
— | Run code generation |
.emit_rerun_if_changed() |
— | Emit cargo:rerun-if-changed= instructions |
Clippy
By default, #![allow(clippy::all)] is added to every generated file so that
cargo clippy does not fail on generated code. Pass .allow_clippy(false) to
opt out.
Namespace rewriting
Generated code references itself via crate::apis:: and crate::models::.
When the module tree is nested, these paths are rewritten to use the configured
namespace prefix so imports resolve correctly.
License
MIT