oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

multiple openapi specs

Open tempcke opened this issue 7 months ago • 1 comments

I have a service which has a public api expressed in openapi format which I use oapi-codegen to generate the server and types for and it works great, however i want to add some admin endpoints and have those generated also without modifying the public openapi spec as I give it out to partners.

So after reading the docs I created an admin.yml file and renamed my original openapi.yml file to obs.yml (obs is the name of the service spec and so is the public api)

and I updated the Makefile target like this

genRest: ## generate api/rest/openapi/*.gen.go
	rm api/rest/openapi/*.gen.go
	oapi-codegen -generate types       -o api/rest/openapi/obs-types.gen.go   -package openapi api/rest/openapi/obs.yml
	oapi-codegen -generate echo-server -o api/rest/openapi/obs-svr.gen.go     -package openapi api/rest/openapi/obs.yml
	oapi-codegen -generate types       -o api/rest/openapi/admin-types.gen.go -package openapi -import-mapping obs.yml:- api/rest/openapi/admin.yml
	oapi-codegen -generate echo-server -o api/rest/openapi/admin-svr.gen.go   -package openapi -import-mapping obs.yml:- api/rest/openapi/admin.yml
	ls -l api/rest/openapi/*.gen.go

because admin.yml uses some of the public models it contains references to the components in obs.yml so I figured out how to use import mapping and this works, it generates the code and the types without duplicating the types etc.

The following are declared twice, once in admin-svr.gen.go and once in obs-svr.gen.go

type ServerInterface
type ServerInterfaceWrapper
type EchoRouter
func RegisterHandlers
func RegisterHandlersWithBaseURL

For this to work I'd obviously need one RegisterHandlers func per service

Though I'd rather not it occurs to me that I could move these into different directories/packages and pass the same instance of echo into each Register func .. but because admin is a superset and references some types in obs.yml the references and input mapping becomes ugly.

Am I missing something in the docs and going about this wrong?

tempcke avatar Jun 13 '25 19:06 tempcke

In a similar situation. One possible solution is to combine the schemas using jq. For now, I'm using separate packages though.

rehandaphedar avatar Aug 29 '25 20:08 rehandaphedar