openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open superboum opened this issue 2 years ago • 2 comments

Bug Report Checklist

  • [X] Have you provided a full/minimal spec to reproduce the issue?
  • [X] Have you validated the input using an OpenAPI validator (example)?
  • [X] Have you tested with the latest master to confirm the issue still exists?
  • [X] Have you searched for related issues/PRs?
  • [X] 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.

superboum avatar Nov 28 '23 14:11 superboum

Another case, where generated helper functions are problematic is when there are two fields, one named with prefix has together with the name of the other field. So for example previousPage and hasPreviousPage. In that case (at least if previousPage is not a required field), a helper method is generated for optional previousPage field:

// HasPrevPage returns a boolean if a field has been set.
func (o *PaginatedDto) HasPreviousPage() bool {
	if o != nil && !IsNil(o.PrevPage) {
		return true
	}

	return false
}

The code is not compiling because generated dto struct has field and method with same name:

Type '*PaginatedDto' has both field and method named 'HasPreviousPage'

As nice, as it is, to have all the convenient helper functions, I think it would be better / more robust, if only one getter and one setter would be generated for each field. If the getter would return value and ok bool, functionality wouldn't suffer, I think.

nilskuhn avatar Apr 22 '24 17:04 nilskuhn

thanks for reporting the issue.

looks like we need an option to skip these helper methods, right?

please use name mappings option to workaround these issues for the time being, e.g.

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g go -i /tmp/aa.yaml -o /tmp/gogo2 --name-mappings barOk=barAlright

ref: https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#name-mapping

wing328 avatar Apr 23 '24 02:04 wing328