skip to content
Alvin Lucillo

OpenAPI Codegen

/ 1 min read

In the prior entries, openapitools/openapi-generator-cli is used to generate Go files, but the generated files are not idiomatic in Go. For example, the handler is not error first (it returns abstracted response with error), and it introduces existing/default router and controller. Moreover, we need something like RegisterHandlers that allows use to plug in existing router. One of the solutions out there is oapi-codegen. As a bonus, it only generates one Go file by default, unlike the former tool that produces basically a ready made program.

To get started, install it as a project-managed tool: go get -tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest