OpenAPITools / OpenAPITools/openapi-generator

[BUG][GO] Bug generating `Get<name>Ok` method when using `$ref`

Open
#11,848 1 comment 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

Get<name>Ok() (<type>, bool) methods are generated incorrectly when using $ref. See example below.

https://github.com/OpenAPITools/openapi-generator/blob/v5.3.1/modules/openapi-generator/src/main/resources/go/model_simple.mustache#L124-L138

https://github.com/OpenAPITools/openapi-generator/blame/4a36be70025e9c0d3ff61731618b7fc2d942c4b6/modules/openapi-generator/src/main/resources/go/model_simple.mustache#L124-L138

openapi-generator version

v5.4.0 (works in e.g. v5.3.1)

OpenAPI declaration file content or url
{
  "openapi": "3.0.3",
  "info": {
    "title": "Bug",
    "version": "1.0"
  },
  "paths": {},
  "components": {
    "schemas": {
      "message": {
        "properties": {
          "header": {
            "$ref": "#/components/schemas/header",
            "description": "Some description",
            "type": "object"
          }
        },
        "required": [
          "header"
        ],
        "title": "Message",
        "type": "object"
      },
      "header": {
        "default": {},
        "description": "Some description",
        "properties": {
          "destination": {
            "default": "",
            "description": "Some description",
            "title": "destination",
            "type": "string"
          },
          "source": {
            "default": "",
            "description": "Some description",
            "title": "source",
            "type": "string"
          }
        },
        "required": [
          "source",
          "destination"
        ],
        "title": "header",
        "type": "object"
      }
    }
  }
}
Generation Details
$ cd /tmp; ls local # put provided example schema in /tmp/local
bug.json
$ for version in v5.3.1 v5.4.0 latest; do
  docker run --rm -v "/tmp/local:/local" openapitools/openapi-generator-cli:$version generate \
            -i /local/bug.json \
            -g go \
            -o /local/$version \
            --package-name bug \
            --global-property models,modelDocs=false \
            --additional-properties=disallowAdditionalPropertiesIfNotPresent=false \
            --skip-validate-spec
done
$ ls local
bug.json latest   v5.3.1   v5.4.0
$ cd local
$ go mod init bug
$ go build ./...
# bug/latest
latest/model_message.go:57:3: cannot use nil as type Header in return argument
# bug/v5.4.0
v5.4.0/model_message.go:57:3: cannot use nil as type Header in return argument
Steps to reproduce
for version in v5.3.1 v5.4.0 latest; do
  docker run --rm -v "/tmp/local:/local" openapitools/openapi-generator-cli:$version generate \
            -i /local/bug.json \
            -g go \
            -o /local/$version \
            --package-name bug \
            --global-property models,modelDocs=false \
            --additional-properties=disallowAdditionalPropertiesIfNotPresent=false \
            --skip-validate-spec
done
--- local/v5.3.1/model_message.go       2022-03-10 09:21:32.000000000 +0100
+++ local/v5.4.0/model_message.go       2022-03-10 09:21:35.000000000 +0100
@@ -52,11 +52,11 @@
 
 // GetHeaderOk returns a tuple with the Header field value
 // and a boolean to check if the value has been set.
-func (o *Message) GetHeaderOk() (*Header, bool) {
+func (o *Message) GetHeaderOk() (Header, bool) {
        if o == nil  {
                return nil, false
        }
-       return &o.Header, true
+       return o.Header, true
 }
 
 // SetHeader sets field value
--- local/v5.3.1/model_message.go       2022-03-10 09:21:32.000000000 +0100
+++ local/latest/model_message.go       2022-03-10 09:21:37.000000000 +0100
@@ -52,11 +52,11 @@
 
 // GetHeaderOk returns a tuple with the Header field value
 // and a boolean to check if the value has been set.
-func (o *Message) GetHeaderOk() (*Header, bool) {
-       if o == nil  {
+func (o *Message) GetHeaderOk() (Header, bool) {
+       if o == nil {
                return nil, false
        }
-       return &o.Header, true
+       return o.Header, true
 }
 
 // SetHeader sets field value
Related issues/PRs

Seems to be introduced in #8491.

Suggest a fix

Perhaps?

diff --git a/modules/openapi-generator/src/main/resources/go/model_simple.mustache b/modules/openapi-generator/src/main/resources/go/model_simple.mustache
index 7a9b2565444..f0c685a0e1f 100644
--- a/modules/openapi-generator/src/main/resources/go/model_simple.mustache
+++ b/modules/openapi-generator/src/main/resources/go/model_simple.mustache
@@ -123,7 +123,13 @@ func (o *{{classname}}) Get{{name}}() {{vendorExtensions.x-go-base-type}} {
 {{/deprecated}}
 func (o *{{classname}}) Get{{name}}Ok() ({{^isArray}}{{^isFreeFormObject}}*{{/isFreeFormObject}}{{/isArray}}{{vendorExtensions.x-go-base-type}}, bool) {
        if o == nil{{#isNullable}}{{#vendorExtensions.x-golang-is-container}} || o.{{name}} == nil{{/vendorExtensions.x-golang-is-container}}{{/isNullable}} {
+{{#isNullable}}
                return nil, false
+{{/isNullable}}
+{{^isNullable}}
+               var ret {{^isArray}}{{^isFreeFormObject}}*{{/isFreeFormObject}}{{/isArray}}{{vendorExtensions.x-go-base-type}}
+               return ret, false
+{{/isNullable}}
        }
 {{#isNullable}}
 {{#vendorExtensions.x-golang-is-container}}

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 modules/openapi-generator/src/main/resources/go/model_simple.mustache and reproduce the issue using the provided OpenAPI schema and Go generation command. Compare the generated GetHeaderOk method with v5.3.1 and verify that the generated v5.4.0 code builds successfully without returning nil for a non-pointer Header value.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.