Skip to content

[GO] If a path argument is named 'path', generated API method will throw a build time error #3566

Description

@ivucica
Description

Function arguments are named after path parameters in the API spec. If a path parameter is named path, this generates a build time error as Go does not like reusing the name to 'create' a 'new' variable as long as there is a preexisting variable with the same name in the scope.

Swagger-codegen version

7a245e3

Swagger declaration file content or url

Full file available at https://bitbucket.org/api/swagger.json (it may change, but that's out of my control)

Also https://bitbucket.org/ivucica/calendarify/src/f1297e3411fcb52e830bd02dd0108e85eed39488/vendor/bitbucket.org/api/swagger.json

Command line used for generation

See https://bitbucket.org/ivucica/calendarify/src/f1297e3411fcb52e830bd02dd0108e85eed39488/vendor/bitbucket.org/api/BUILD

genrule(
    name = "rest_client_go_gen",
    srcs = [":swagger.json"],
    outs = SWAGGER_GENERATED,
    tools = [
        "//vendor:swagger-codegen-cli",
        "//vendor:swagger-codegen-cli_deploy.jar",
    ],
    cmd = "\n".join([
        "mkdir -p \"$$(dirname $(location :configuration.go))\"",
        "$(location //vendor:swagger-codegen-cli) --singlejar generate " +
        " -i $(location :swagger.json)" +
        " -l go" +
        " -o \"$$(dirname $(location :configuration.go))\"",
    ]),
)

Versions of dependencies: https://bitbucket.org/ivucica/calendarify/src/f1297e3411fcb52e830bd02dd0108e85eed39488/WORKSPACE

Versions of swagger projects: https://bitbucket.org/ivucica/calendarify/src/f1297e3411fcb52e830bd02dd0108e85eed39488/vendor/github.com/swagger-api/

  • swagger-codegen [7a245e3]
  • swagger-core [c7c9a4c641dc]
  • swagger-parser [0e2fac8d6e46]
  • swagger-ui [44149467753f]
Steps to reproduce

Bitbucket's API contains the following definition (transformed from JSON into YAML by the Swagger editor):

paths:
...
  '/repositories/{username}/{repo_slug}/issues/{issue_id}/attachments/{path}':
    delete:
      ...
    parameters:
      - required: true
        type: string
        name: username
        in: path
      - required: true
        type: string
        name: path
        in: path
      - required: true
        type: string
        name: issue_id
        in: path
      - required: true
        type: string
        name: repo_slug
        in: path
    ...

Generated code looks like:

func (a IssuetrackerApi) RepositoriesUsernameRepoSlugIssuesIssueIdAttachmentsPathDelete(username string, path string, issueId string, repoSlug string) (*APIResponse, error) {                                                                                         

        var httpMethod = "Delete"                                                                                                   
        // create path and map variables                                                                                            
        path := a.Configuration.BasePath + "/repositories/{username}/{repo_slug}/issues/{issue_id}/attachments/{path}"              
...

(note the 'path' is a preexisting variable, used as an argument.)

This results in a build error in Go such as:

badc0de.net/pkg/calendarify/bazel-out/local-fastbuild/genfiles/vendor/bitbucket.org/api/issuetracker_api.go:342: no new variables on left side of :=

Related issues

I am unaware of any.

Suggest a Fix

If a method argument would be named path, change it to path_arg, or some other value that would not be used in the generated code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions