OpenAPITools / OpenAPITools/openapi-generator

[BUG][GO][GO-SERVER] setting import mappings does not work in go and go-server generators

Open
#8,767 4 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I'm trying to use the go and go-server generators to generate services that use our existing models. According to this using the following flag should be enough to load a custom package:

--import-mappings Pet=test.com/mypackage

But the generated code for both, go and go-server is not working. The problems are different so I'm not sure if this should be splitted into two issues.

Go generator is including the import at the top, but it is not using the package when referencing the model:

import (
	...
	"test.com/mypackage"
)

...
func (r ApiPutPetRequest) Execute() (Pet, *_nethttp.Response, error) {
	return r.ApiService.PutPetExecute(r)
}

So it fails to compile: undefined: Pet. The code should be:

func (r ApiPutPetRequest) Execute() (mypackage.Pet, *_nethttp.Response, error) {
	return r.ApiService.PutPetExecute(r)
}

I've tried adding --type-mappings Pet=mypackage.Pet to try to manually set the package, but it as no effect in the output.

Go Server generator on the other hand is completely ignoring the --type-mappings flag,

import (
	...
        // our import should be here
)

...

// PutPet - Save a new Pet
func (c *PetApiController) PutPet(w http.ResponseWriter, r *http.Request) { 
	pet := &Pet{}  // this should be mypackage.Pet
	...
}

I'm using v5.0.1 of the generator.

OpenAPI declaration file content or url

https://github.com/jagobagascon/openapi-go-test/blob/master/spec.yaml

Generation Details

Check example repo

Steps to reproduce

I created a example repo where I can reproduce the issue: https://github.com/jagobagascon/openapi-go-test

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 by reproducing both the go and go-server generators with the example repository's spec.yaml and the reported import-mappings and type-mappings flags. Trace how each generator renders imported model references and imports. Done means generated code for both generators uses mypackage.Pet and compiles without undefined model references.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.