OpenAPITools / OpenAPITools/openapi-generator

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

Open
#3,411 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Go 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)?
  • 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.https://github.com/OpenAPITools/openapi-generator/issues/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.

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 in AbstractGoCodegen.java and reproduce the issue with the supplied OpenAPI declaration and generation command. Inspect the generated api_*.go file for the MyObject field named type. Done means the generated field uses Type rather than Type_ and the resulting SDK remains valid.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.