Generate Go client and server boilerplate from OpenAPI 3 specifications
Generate Go client and server boilerplate from OpenAPI 3 specifications
oapi-codegenoapi-codegen is a command-line tool and library to convert OpenAPI specifications to Go code, be it server-side implementations, API clients, or simply HTTP models.
Using oapi-codegen allows you to reduce the boilerplate required to create or integrate with services based on OpenAPI 3.0 and OpenAPI 3.1, and instead focus on writing your business logic, and working on the real value-add for your organisation.
With oapi-codegen, there are a few Key Design Decisions we've made, including:
oapi-codegen is one part of a wider ecosystem, which can be found described in further detail in the oapi-codegen organisation on GitHub.
⚠️ This README may be for the latest development version, which may contain unreleased changes. Please ensure you're looking at the README for the latest release version.
additionalProperties)
As announced in May 2024,
we have moved the project from the Deepmap organization to our own organization, and you will need to update your
import paths to pull updates past this point. You need to do a recursive search/replace from
github.com/deepmap/oapi-codegen/v2 to github.com/oapi-codegen/oapi-codegen/v2.
[!IMPORTANT]
oapi-codegenmoved to its new home with the version tagv2.3.0.
If you are using v2.2.0 or below, please install like so:
# for the binary install
go install github.com/deepmap/oapi-codegen/v2/cmd/[email protected]If you are using v2.3.0 or above, please install like so, using the new module import path:
# for the binary install
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latestoapi-codegen requires Go 1.25+ to build and install. Note that the generated code has its own, lower requirements — see Supported Servers.
It is recommended to use go tool support for managing the dependency of oapi-codegen alongside your core application.
To do this, you run go get -tool:
$ go get -tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest
# this will then modify your `go.mod`From there, each invocation of oapi-codegen would be used like so:
//go:generate go tool oapi-codegen -config cfg.yaml ../../api.yamlAlternatively, you can install it as a binary with:
$ go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest
$ oapi-codegen -versionWhich then means you can invoke it like so:
//go:generate oapi-codegen --config=config.yaml ../../api.yamlWhile the project does not (yet) have a defined release cadence, there may be cases where you want to pull in yet-unreleased changes to your codebase.
Therefore, you may want to pin your dependency on oapi-codegen to a given commit hash, rather than a tag.
This is officially recommended for consumers of oapi-codegen, who want features/bug fixes that haven't yet been released.
We aim to keep the default branch ready-to-release so you should be able to safely pin.
To do so, you can run:
# pin to the latest version on the default branch
$ go get github.com/oapi-codegen/oapi-codegen/v2@main
# alternatively, to a commit hash i.e. https://github.com/oapi-codegen/oapi-codegen/commit/71e916c59688a6379b5774dfe5904ec222b9a537
$ go get github.com/oapi-codegen/oapi-codegen/v2@71e916c59688a6379b5774dfe5904ec222b9a537This will then make a change such as:
diff --git go.mod go.mod
index 44f29a4..436a780 100644
--- go.mod
+++ go.mod
@@ -2,21 +2,20 @@
-require github.com/oapi-codegen/oapi-codegen/v2 v2.1.0
+require github.com/oapi-codegen/oapi-codegen/v2 v2.1.1-0.20240331212514-80f0b978ef16oapi-codegen is configured using a YAML configuration file, to simplify the number of flags that users need to remember, and to make reading the go:generate command less daunting.
While some command line options are supported, they should be considered deprecated,
we just maintain them for backward compatibility, but we're not extending
them.
For full details of what is supported, check out the oapi-codegen configuration documentation.
We also have a JSON Schema that can be used by IDEs/editors with the Language Server Protocol (LSP) to perform intelligent suggestions, i.e.:
# yaml-language-server: $schema=https://raw.githubusercontent.com/oapi-codegen/oapi-codegen/v2.8.0/configuration-schema.json
package: api
# ...Note that it's recommended to pin to a specific version of the configuration schema, so it matches the version of oapi-codegen you're using. For instance, if you're using Renovate, you can have Renovate automagically update this version for you.
Although we strive to retain backwards compatibility - as a project that's using a stable API per SemVer - there are sometimes opportunities we must take to fix a bug that could cause a breaking change for people relying upon the behaviour.
In this case, we will expose a compatibility option to restore old behaviour.
That all being said, we operate in a little bit of a grey area, since we are a code generator, we support template overrides, and our code can be imported. So this is how we will try to maintain compatibility:
pkg/ directory is importable. In retrospect, this was a mistake in our original design. Importing anything other than the Generate function and its related Configuration is considered unstable (and at this point, hasn't (yet) been documented). In the future, we will clean up our import surface.Also note that - if a security vulnerability is found - we will opt for breaking all users if need be, if it is the most secure response.
Unless explicitly called out above, your usage of oapi-codegen may be using unstable features, which may make it harder to upgrade over time.
At a high level, oapi-codegen supports:
OAS3.1.0 const naming is long and different than other types
Dependency Dashboard
OpenAPI 3.1: schemas with const but no type lose type information
Nested nullable unions silently discard JSON payloads
Unexported fields with anyOf in request parameters
Support for OpenAPI 3.2?
Committed swaggerSpec blobs depend on the Go toolchain; pin GOTOOLCHAIN for make generate
`generate-types-for-anonymous-schemas` generates invalid Go identifiers for numeric response keys
Investigate Go 1.27's UUID package
Generate a slice of "valid" values for an enum