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

generate types and server in different directories

Open satyamsi opened this issue 3 years ago • 2 comments

Discussed in https://github.com/deepmap/oapi-codegen/discussions/551

Originally posted by betorvs April 15, 2022 Hi all,

Thanks for this project. It's amazing!

If I want to generate code in a different directories like, types on model directory and server in controller directory. How can I generate server code with all types in model directory?

oapi-codegen -generate types ~/go/pkg/mod/github.com/deepmap/oapi-codegen\@v1.9.1/examples/petstore-expanded/petstore-expanded.yaml > model/PetstoreExpanded/model_pets.go

And if I run server option:

oapi-codegen -generate server ~/go/pkg/mod/github.com/deepmap/oapi-codegen\@v1.9.1/examples/petstore-expanded/petstore-expanded.yaml > controller/PetstoreExpanded/controller_pets.go

But in controller/PetstoreExpanded/controller_pets.go It cannot find any types from model/PetstoreExpanded/model_pets.go.

Is there any easy way to achieve it? I don't want to edit controller/PetstoreExpanded/view_pets.go.

satyamsi avatar Oct 28 '22 02:10 satyamsi

For production quality code, it's important to organize the code well. Currently, this only supports single combined output leading to duplicates.

Would you folks be open to getting a PR for this issue ?

satyamsi avatar Oct 28 '22 02:10 satyamsi

One way to achieve this is to separate your types and your paths in your swagger specs. petstore-types.yaml could contain your types and petstore-paths.yaml your paths.

For this example we'll assume that your go project lives at github.com/satyamsi/petstore, your types will be in github.com/satyamsi/petstore/types, your server in github.com/satyamsi/petstore/server and that you use he following folder structure:

petstore/:
  oapi-codegen-types.yaml
  oapi-codegen-paths.yaml
  types/:
    types.gen.go
  server/:
    server.gen.go
  specs/:
    types/:
      petstore-types.yaml
    paths/:
      petstore-paths.yaml

Then you'll need to use two different config :

oapi-codegen-types.yaml

package: types
generate:
  models:true

output-options:
  # This option is important otherwise your types will not be generated since they don't have any paths
  skip-prune: true

oapi-codegen-server.yaml

package: server
generate:
  echo-server: true

import-mapping:
  # Given your folder structure, in your `petstore-paths.yaml` you'll be referrencing your types using 
  # $ref : '../types/petstore-types.yaml#' see https://swagger.io/docs/specification/using-ref/ for more information.
  # Therefore you'll need to map your specs to your go package name like this 
  ../types/petstore-types.yaml: github.com/satyamsi/petstore/types

Now all you need to do is execute the following commands:

cd petstore
oapi-codegen -config oapi-codegen-types.yaml specs/types/petstore-types.yaml > types/types.gen.go
oapi-codegen -config oapi-codegen-server.yaml specs/paths/petstore-paths.yaml > server/server.gen.go

And that will do the trick !

riftsin avatar Nov 17 '22 07:11 riftsin