Skip to content

[GO] Bad GO code generating access_token in query under securityDefinitions #6339

@sun7927

Description

@sun7927
Description

spec:

post:
  tags:
    - Authentication
  description: Log in to the server
  operationId: login
  security:
    - ApiTokenQueryParam: []
  consumes:
    - application/json
  parameters:
    - name: api-token
      in: header
      description: the token to log in
      required: true
      type: string

securityDefinitions:
  ApiTokenQueryParam:
    type: apiKey
    name: api-token
    in: query

It will generate the following GO code,

func (a AuthenticationApi) Login(apiToken string) (*LoginResponse, *APIResponse, error) {
    ...
    localVarQueryParams := url.Values{}
    // authentication '(ApiTokenQueryParam)' required
    // set key with prefix in query string
    localVarQueryParams["api-token"] =  a.Configuration.GetAPIKeyWithPrefix("api-token")

It has an error:

cannot use a.Configuration.GetAPIKeyWithPrefix("api-token") (type string) as type []string in assignment

The correct code should be:

localVarQueryParams.Add("api-token",  a.Configuration.GetAPIKeyWithPrefix("api-token"))
Swagger-codegen version

2.2.3

Swagger declaration file content or url
Command line used for generation

java -jar swagger-codegen-cli-2.2.3.jar generate -i $(SWAGGER_SPEC) -o $(CLIENT_GO) -l go -c $(SWAGGER_CONFIG) --git-user-id=$(GIT_USER) --git-repo-id=$(GIT_REPO) -v

Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions