Skip to content

[BUG][Golang][Client] type property does not need to be modified as struct attribute name. #3411

Open
@grokify

Description

@grokify

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When a schema property name is "type" or any other reserved word, the generator will see this as a reserved word and convert this to a struct with the attribute "Type_", however this is not necessary as Go handles Type and other attributes in a struct fine.

Actual Output

type MyObject struct {
	Type_              optional.String
}

Expected Output

type MyObject struct {
	Type              optional.String
}

A working example can be seen here:

The warning on generation is:

[main] WARN o.o.c.languages.AbstractGoCodegen - type (reserved word) cannot be used as parameter name. Renamed to type_

Removing the trailing _ still results in a working SDK.

Here is the full set of reserved words in AbstractGoCodegen.java:

        setReservedWordsLowerCase(
                Arrays.asList(
                        // data type
                        "string", "bool", "uint", "uint8", "uint16", "uint32", "uint64",
                        "int", "int8", "int16", "int32", "int64", "float32", "float64",
                        "complex64", "complex128", "rune", "byte", "uintptr",

                        "break", "default", "func", "interface", "select",
                        "case", "defer", "go", "map", "struct",
                        "chan", "else", "goto", "package", "switch",
                        "const", "fallthrough", "if", "range", "type",
                        "continue", "for", "import", "return", "var", "error", "nil")
                // Added "error" as it's used so frequently that it may as well be a keyword
        );

So far, everyone of these compiles without being escaped. Here's an example with every reserve word implemented:

openapi-generator version

4.1.0-SNAPSHOT

OpenAPI declaration file content or url
{
  "openapi":"3.0.0",
  "info":{
    "description":"",
    "version":"1.0",
    "title":"Engage Digital API",
    "termsOfService":"https://developer.ringcentral.com"
  },
  "paths":{
    "/foobar":{
      "get":{
        "operationId":"getFooBar",
        "responses":{
          "200":{
            "description":"Success",
            "content":{
              "application/json":{
                "schema":{
                  "$ref":"#/components/schemas/MyObject"
                }
              }
            }
          }
        }
      }
    }
  },
  "components":{
    "schemas":{
      "MyObject":{
        "properties":{
          "type":{
            "type":"string"
          }
        }
      }
    }
  }
}
Command line used for generation

java -jar openapi-generator-cli.jar generate -i openapi-spec_v3.0.0.json -g go -o engagedigital --package-name engagedigital

Steps to reproduce

Create SDK with above and examine api_*.go file.

Related issues/PRs

None found.#3480

Suggest a fix

Do not use reserved word when implementing struct properties.

Manually removing the trailing _ results in a better interface and still working SDK.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions