Oapi-codegen: Difference between revisions
Jump to navigation
Jump to search
(→types) |
|||
Line 49: | Line 49: | ||
==Type Generation== | ==Type Generation== | ||
See: {{Internal|OpenAPI_Specification_Schemas#Overview|OpenAPI Specification Schemas}} | See: {{Internal|OpenAPI_Specification_Schemas#Overview|OpenAPI Specification Schemas}} | ||
==Code Generation for OpenAPI Specification Path/Operation Combinations== | |||
{{Internal|OpenAPI_Specification_Path#Server_Code_Generation_for_Path/Operation_Combinations|OpenAPI Specification Path | Server Code Generation for Path/Operation Combinations}} | |||
=Options= | =Options= |
Revision as of 20:19, 26 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
make 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
See:
Code Generation for OpenAPI Specification Path/Operation Combinations
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
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
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) {...}