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

Support shared "types" package

Open kwtj43 opened this issue 3 years ago • 6 comments

Sorry if this feature (or work around) is already available, I didn't find anything in the documentation.

I'd like to be able to generate the types in one package and then use that package for client/server (that also reside in different packages)...

oapi-codegen -package types -generate types my.yaml > types/types.gen.go oapi-codegen -package client -generate client -type-package types my.yaml > client/client.gen.go oapi-codegen -package server -generate server -type-package types my.yaml > server/server.gen.go

The generate client/server code would use the types in the "types" package.

kwtj43 avatar Mar 30 '22 16:03 kwtj43

That's a must have feature. In other case typical cyclic deps arose:

  • server pkg contains implementation that imports tools pkg;
  • tools pkg has to import server in order to process arbitrary types. Separate types package could break that vicious circle.

tuxofil avatar Apr 23 '22 13:04 tuxofil

https://github.com/deepmap/oapi-codegen/pull/389

tuxofil avatar Apr 23 '22 13:04 tuxofil

I've adapted #389 to the latest version of oapi-codegen in #558

tuxofil avatar Apr 24 '22 10:04 tuxofil

The difference from origin patch is you have to specify full Golang package name like:

oapi-codegen -package types -generate types my.yaml > types/types.gen.go
oapi-codegen -package client -generate client -type-package a.com/b/c/types my.yaml > client/client.gen.go
oapi-codegen -package server -generate server -type-package a.com/b/c/types my.yaml > server/server.gen.go

tuxofil avatar Apr 24 '22 10:04 tuxofil

@tuxofil I also ran into this issue - thanks for your PR, I used your fork of this package and was able to generate code into a separate types package.

I ran into an issue when generating an API client - the types in the client package didn't properly reference my types package (a.com/b/c/types in your example above). I've created a new PR based on yours with an additional commit to fix this. Just a heads up in case you run into a similar issue.

chrnorm avatar Apr 25 '22 16:04 chrnorm

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

pitmullerIngka avatar Nov 23 '22 10:11 pitmullerIngka

Is there a way to provide additional-imports via cli?

pcriv avatar May 18 '23 08:05 pcriv