oapi-codegen
oapi-codegen copied to clipboard
Different package for server and types
Suggestion to allow different packages per generator
oapi-codegen -generate types,server api/openapi.yaml -o types=internal/api/models/models.go -o server=internal/api/server/server.go
This would benefit me as well.
You can achieve that using the import-mapping flag. We've done it like this in our Makefile
OAPI_SCHEMA_FILE := openapi/api.yaml
OAPI_COMMON_SCHEMA_FILE := openapi/types.yaml
OAPI_GENERATED_DIR := pkg/openapi/generated
OAPI_CODEGEN := ~/go/bin/oapi-codegen
oapi: ## build oapi
mkdir -pv $(OAPI_GENERATED_DIR)/{types,public}
$(OAPI_CODEGEN) -generate 'types,skip-prune,spec' \
-package 'types' $(OAPI_COMMON_SCHEMA_FILE) > $(OAPI_GENERATED_DIR)/types/types.gen.go
$(OAPI_CODEGEN) -generate 'types,server,spec' -import-mapping=./types.yaml:github.com/org/repo/v2/pkg/openapi/generated/types\
-package 'public' $(OAPI_SCHEMA_FILE) > $(OAPI_GENERATED_DIR)/public/api.gen.go
This is now possible. An example is provided in one of the original PRs for this: https://github.com/deepmap/oapi-codegen/pull/389#issuecomment-1243080859
the example uses an alias of . but when I try to use anything but the dot alias it doesn't work (it doesn't even put the import in the generated code anymore) Perhaps it doesn't support anything else yet.