swagger-api / swagger-api/swagger-codegen

[Go] swagger-codegen generates code with wrong imports for packages using new Go modules (go.mod file)

Open
#8,798 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Issue is a blocker https://github.com/grpc-ecosystem/grpc-gateway/pull/770 - swagger-codegen generates Go files with imports not corresponding to the module value in the go.mod file.

swagger-codegen should take into account the module declaration in go.mod file if that file is available and use that as the package basename in the import statements in the generated code.
In particular, for the resty package, it should put as import "gopkg.in/resty.v1" and not "github.com/go-resty/resty" as currently.

Swagger-codegen version

grpc-ecosystem/grpc-gateway uses 2.2.2 for now (there's issue to upgrade https://github.com/grpc-ecosystem/grpc-gateway/issues/254); it is not yet know if the issue remains with the latest / current version of swagger-codegen.

Swagger declaration file content or url

https://github.com/grpc-ecosystem/grpc-gateway/blob/master/examples/proto/examplepb/a_bit_of_everything.swagger.json

Command line used for generation

https://github.com/grpc-ecosystem/grpc-gateway/blob/master/Makefile#L161-L162
$(SWAGGER_CODEGEN) generate -i $(ECHO_EXAMPLE_SPEC) \ -l go -o examples/clients/echo --additional-properties packageName=echo

Steps to reproduce

TODO

Related issues/PRs

https://github.com/grpc-ecosystem/grpc-gateway/pull/770

Suggest a fix/enhancement

consider go.mod file for the imported packages if available and use the module value from it as the import package basename.

/cc @johanbrandhorst

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generation command in Makefile and the examplepb/a_bit_of_everything.swagger.json declaration, then reproduce the imports produced for the Go client. Inspect how Go package imports are derived and how an available go.mod module value could affect them. Done means generated imports use the declared module basename, including the resty example, without breaking generation.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.