OpenAPITools / OpenAPITools/openapi-generator

[BUG][go] Generated Client Bug: type has both field and method named xxx

Open
#9,397 0 comments 1 reaction 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

Generating a Go API client produces code that does not compile. The problem arises if a model YYY contains a field called XXX and a field called HasXXX:

"schema": {
  "type": "object",
  "properties": {
    "operations": {
      "type": "array"
    },
    "hasOperations": {
      "type": "boolean"
    }
  }
}

The compiler error is type YYY has both field and method named HasXXX.

Example URL to generate such a problem: https://api.sellsation.com/swagger/docs/v1

The example link generates a wrong model, for example model_json_patch_document_patch_contact_model.go:

type JsonPatchDocumentPatchContactModel struct {
	Operations *[]JsonPatchOperation `json:"operations,omitempty"`
	HasOperations *bool `json:"hasOperations,omitempty"`
	AdditionalProperties map[string]interface{}
}

....

// HasOperations returns a boolean if a field has been set.   <-------------- NAME CLASH!
func (o *JsonPatchDocumentPatchContactModel) HasOperations() bool {
	if o != nil && o.Operations != nil {
		return true
	}

	return false
}

// HasHasOperations returns a boolean if a field has been set.
func (o *JsonPatchDocumentPatchContactModel) HasHasOperations() bool {
	if o != nil && o.HasOperations != nil {
		return true
	}

	return false
}
openapi-generator version

Tested with version 5.1.0 and the latest docker image (digest 35bd4a32c0f1).

OpenAPI declaration file content or url

https://api.sellsation.com/swagger/docs/v1

Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i https://api.sellsation.com/swagger/docs/v1 \
    --additional-properties=packageName=sellsationclient,packageVersion=$api_main_version,generateInterfaces=true,disallowAdditionalPropertiesIfNotPresent=false \
    -g go \
    -o /local/out/go
Steps to reproduce

Simple generate the API client using the sample call above. All model_json_patch*.go files contain bad Go code.

Related issues/PRs
Suggest a fix

Validate if generated method name clashes with a field name. If so, add a pre/suffix to the method name. Rerun the check until there are no more name clashes.

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

Run the documented Docker generation command against the linked Swagger specification and inspect the generated model_json_patch*.go files, especially model_json_patch_document_patch_contact_model.go. Compare the generated fields and Has... methods, then verify that the generated Go client compiles without field-and-method name clashes.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.