Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #3411
Closed
Open
Issue created Jul 22, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

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

Created by: 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:

  • https://play.golang.org/p/LPjbFfb9o6B

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:

  • https://play.golang.org/p/52b-LjWvx2l
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.

Assignee
Assign to
Time tracking