OpenAPITools / OpenAPITools/openapi-generator

[BUG][Go] Generator creates 2 functions with the same name

Open
#17,208 2 comments 0 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When I try to load a Go library generated from an OpenAPI spec containing 2 fields, one named bar and the other one barOk, I get the following error:

... method FooGet200Response.GetBarOk already declared at ...

I expect to load the library without an error, and more specifically, I expect that OpenAPI does not generate two functions with the same name in the same file / module / namespace.

openapi-generator version
$ docker run --rm  openapitools/openapi-generator-cli version
7.2.0-SNAPSHOT

I don't think it's a regression, I detected the bug after we extended our API with this 2 fields.
In other words, I can't say if the bug has always been present or has been added after some changes.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Golang Generator Bug with Ok ending properties
  version: 0.0.1
paths:
  /foo:
    get:
      summary: Returns a list of users.
      description: Optional extended description in CommonMark or HTML.
      responses:
        '200':    # status code
          description: A JSON array of user names
          content:
            application/json:
              schema: 
                type: object
                properties: 
                  bar:
                    type: string
                  barOk:
                    type: string
Generation Details

How I generate the Golang library:

docker run \
  --rm \
  -v /tmp/bug.yml:/bug.yml \
  -v /tmp/output:/tmp/out \
  -ti  \
  openapitools/openapi-generator-cli:latest \
    generate \
    -g go \
    -i /bug.yml \
    -o /tmp/out

Then when I try to build it:

go build

I get the following error:

$ go build
# github.com/GIT_USER_ID/GIT_REPO_ID
./model__foo_get_200_response.go:76:29: method FooGet200Response.GetBarOk already declared at ./model__foo_get_200_response.go:54:29

Function at line 54:

// GetBarOk returns a tuple with the Bar field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *FooGet200Response) GetBarOk() (*string, bool) {
	if o == nil || IsNil(o.Bar) {
		return nil, false
	}
	return o.Bar, true
}

Function at line 76:

// GetBarOk returns the BarOk field value if set, zero value otherwise.
func (o *FooGet200Response) GetBarOk() string {
	if o == nil || IsNil(o.BarOk) {
		var ret string
		return ret
	}
	return *o.BarOk
}
Steps to reproduce

Check the section above.

Related issues/PRs

I have not found any related issue

Suggest a fix

I don't know how to fix this bug now. I don't have any workaround yet except manually patching the generated code.

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 minimal OpenAPI declaration in the issue and generate the Go client using the documented Docker command. Inspect model__foo_get_200_response.go and run go build to reproduce the duplicate GetBarOk methods; done means the generated package builds without duplicate function declarations.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.