Command line
oapixconstgen is a single command with four flags. It reads one spec, writes one Go file, and exits. All of it lives in main.go.
oapixconstgen \
-spec=api/openapi.yaml \
-out=internal/api \
-file=constants.gen.go \
-pkg=api
Only -spec is required. Installed as a project tool, the same flags follow go tool:
go tool oapixconstgen -spec=api/openapi.yaml -out=internal/api
Flags
| Flag | Default | Description |
|---|---|---|
-spec | (required) | Path to the OpenAPI spec file. |
-out | . | Directory the generated file is written to. It must already exist; the tool does not create it. |
-file | constants.gen.go | Name of the generated file inside -out. |
-pkg | api | Package name written in the generated file’s package clause. |
Flags use the Go flag package syntax, so -spec=api.yaml, -spec api.yaml and --spec=api.yaml all work. -h prints the flag list.
What a run does
- Reads the file at
-specand parses it as YAML. JSON is valid YAML, so a JSON spec works too. - Takes the top-level
x-constantsmap and ignores everything else in the document. The rest of the spec is not validated. See The x-constants block for the format. - Builds one
constblock, sorted by the names as written in the spec, and runs it throughgo/format. - Writes it to
-out/-file, replacing any file already there. The file is created with mode0600, readable and writable by its owner only.
If the spec has no x-constants, or none of its type keys are supported, the tool writes nothing, leaves any existing output file alone, and exits 0.
Exit codes
| Code | When |
|---|---|
0 | The file was written, or there were no constants to write. |
1 | A flag could not be parsed (including -h), -spec is missing, the spec cannot be read or parsed, the generated code is not valid Go syntax, or the output file cannot be written. |
Log output
Every message goes to stderr through log/slog in its text format. stdout stays empty.
| Level | Message | Meaning |
|---|---|---|
INFO | constants generated | Success. The path field holds the file that was written. |
INFO | no x-constants found in spec, skipping generation | Nothing to write. Exit code 0. |
ERROR | spec path is required | -spec was not given. |
ERROR | failed to read spec file | The spec path does not exist or cannot be read. |
ERROR | failed to parse spec file | The file is not valid YAML, or x-constants is not a map of maps. |
ERROR | failed to generate code | The generated source does not parse as Go. The usual causes are an invalid -pkg, a constant name that is not a Go identifier, or a value that does not fit its type key. The error field holds the parser message. |
ERROR | failed to write output file | The -out directory does not exist or is not writable. |
A successful run looks like this:
time=2026-01-28T10:00:00.000Z level=INFO msg="constants generated" path=internal/api/constants.gen.go
Use with go generate
Put a directive in a Go file of the package that should hold the constants. go generate runs it from that package’s directory, so -spec is relative to it:
//go:generate oapixconstgen -spec=../../api/openapi.yaml -out=. -pkg=api
As a project tool:
//go:generate go tool oapixconstgen -spec=../../api/openapi.yaml -out=. -pkg=api
Then regenerate everything:
go generate ./...
The generated file starts with // Code generated by oapixconstgen. DO NOT EDIT., the marker Go tooling and linters use to recognize generated code. Edit the spec and run the generator again rather than changing the file by hand.