Oapi-codegen: Difference between revisions
Line 74: | Line 74: | ||
===<tt>types</tt>=== | ===<tt>types</tt>=== | ||
See [[#Type_Generation|Type Generation]] above. | See [[#Type_Generation|Type Generation]] above. | ||
===<tt>spec</tt>=== | |||
===spec=== | |||
Generates this: | Generates this: | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
Line 92: | Line 91: | ||
===<tt>server</tt>=== | ===<tt>server</tt>=== | ||
See [[#Server_Code_Generation|Server Code Generation]] above. | See [[#Server_Code_Generation|Server Code Generation]] above. | ||
===<tt>chi-server</tt>=== | |||
===chi-server=== | |||
Generates server code for the chi HTTP server. The code is compatible with <code>[[Go_Package_net/http#Registering_Handlers_Generated_by_oapi-codegen_from_an_OpenAPI_Specification|net/http]]</code> server: | Generates server code for the chi HTTP server. The code is compatible with <code>[[Go_Package_net/http#Registering_Handlers_Generated_by_oapi-codegen_from_an_OpenAPI_Specification|net/http]]</code> server: | ||
<syntaxhighlight lang='bash'> | <syntaxhighlight lang='bash'> | ||
Line 99: | Line 97: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
For an example of how to set up a <code>net/http</code> server with the generated code, see: {{Internal|Go_Package_net/http#Registering_Handlers_Generated_by_oapi-codegen_from_an_OpenAPI_Specification|net/http Server with oapi-codegen Code}} | For an example of how to set up a <code>net/http</code> server with the generated code, see: {{Internal|Go_Package_net/http#Registering_Handlers_Generated_by_oapi-codegen_from_an_OpenAPI_Specification|net/http Server with oapi-codegen Code}} | ||
===<tt>client</tt>=== | |||
===client=== | See [[#Client_Code_Generation|Client Code Generation]] above. |
Revision as of 02:18, 27 January 2024
External
Internal
Overview
Installation
Get the latest version from https://github.com/deepmap/oapi-codegen/tags
Then:
go install github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@v2.1.0
The installation will place the package under $GOPATH/pkg/mod/cache/download/github.com/deepmap
and $GOPATH/pkg/mod/github.com/deepmap
, will compile the binary and place it under ~/go/bin
.
Generate Code
Generate client and server code:
oapi-codegen -package petstore ./petstore-expanded.yaml > ./internal/petstore/petstore.gen.go
go mod tidy
Makefile Support
.PHONY: generate_oapi_artifacts
generate_oapi_artifacts: internal/petstore/spec.gen.go internal/petstore/types.gen.go internal/petstore/server.gen.go internal/petstore/client.gen.go
internal/petstore/spec.gen.go: ./petstore.yaml
oapi-codegen -generate spec -package petstore $< > $@
internal/petstore/types.gen.go: ./petstore.yaml
oapi-codegen -generate types -package petstore $< > $@
internal/petstore/server.gen.go: ./petstore.yaml
oapi-codegen -generate server -package petstore $< > $@
internal/petstore/client.gen.go: ./petstore.yaml
oapi-codegen -generate client -package petstore $< > $@
Type Generation
Generate the types declared in the /components/schemas
section of the OpenAPI specification.
oapi-codegen -generate types -package petstore ./petstore-expanded.yaml > ./internal/petstore/types.gen.go
Also see:
Server Code Generation
By default, oapi-codegen
generates server code for the echo HTTP server.
oapi-codegen -generate server -package petstore ./petstore.yaml > ./internal/petstore/server.gen.go
For an example of how to set up an echo
server with the generated code, see:
For OpenAPI specification examples and their corresponding generated server code see:
Client Code Generation
TODO: https://github.com/deepmap/oapi-codegen#generated-client-boilerplate
Options
-package
The package name for the generated code.
-generate
Comma-separated list of code to generate; valid options: "types", "client", "chi-server", "server", "gin", "gorilla", "spec", "skip-fmt", "skip-prune", "fiber", "iris". The default is "types,client,server,spec".
types
See Type Generation above.
spec
Generates this:
var swaggerSpec = []string{...}
func decodeSpec() ([]byte, error) {...}
var rawSpec = decodeSpecCached()
func decodeSpecCached() func() ([]byte, error) {...}
func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) {...}
func GetSwagger() (swagger *openapi3.T, err error) {...}
server
See Server Code Generation above.
chi-server
Generates server code for the chi HTTP server. The code is compatible with net/http
server:
oapi-codegen -generate chi-server -package petstore ./petstore.yaml > ./internal/petstore/server.gen.go
For an example of how to set up a net/http
server with the generated code, see:
client
See Client Code Generation above.