OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.62k stars 6.29k forks source link

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

Open superboum opened 7 months ago

superboum commented 7 months ago

Bug Report Checklist

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.

nilskuhn commented 2 months ago

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.

wing328 commented 2 months ago

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