swagger-api / swagger-api/swagger-codegen

[GO] optional array parameterToString question

Open
#12,124 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

i have set the optional query array params Ids like: []string{"a1","b2"}.

i expect the query string : Ids=a1&Ids=b2 , but got Ids=a1b2.

Swagger-codegen version
swagger-codegen version
3.0.42
Swagger declaration file content or url
    - name: Ids
      in: query
      description: id array
      required: false
      style: form
      explode: true
      schema:
        type: array
        items:
          type: string
Command line used for generation

swagger-codegen generate -i ./api/swagger.yaml -l go -o ./

Steps to reproduce

type DefaultPostOpts struct {
	Ids         optional.Interface
}

func (a *DefaultApiService) ApiPost(ctx context.Context, localVarOptionals *DefaultPostOpts) (Response, *http.Response, error) { 
        ....

	if localVarOptionals != nil && localVarOptionals.Ids.IsSet() {
		localVarQueryParams.Add("Ids", parameterToString(localVarOptionals.Ids.Value(), "multi"))
	}
       ....
}

client.DefaultApi.ApiPost(ctx, &DefaultPostOpts{
	Ids: optional.NewInterface([]string{"a1","b2"}),
})
Related issues/PRs
Suggest a fix/enhancement


if localVarOptionals != nil && localVarOptionals.Ids.IsSet() {
               appendQueryParams(localVarQueryParams,localVarOptionals.Ids.Value(),"Ids")
}

func appendQueryParams(queryParams url.Values, obj interface{}, key string) {
	value := reflect.ValueOf(obj)
	kind := value.Kind()
	switch kind {
	case reflect.Slice, reflect.Array:
		for i := 0; i < value.Len(); i++ {
			element := value.Index(i)
			queryParams.Add(key, fmt.Sprintf("%v", element.Interface()))
		}
	case reflect.Map:
		for _, mapKey := range value.MapKeys() {
			value := value.MapIndex(mapKey)
			queryParams.Add(key, fmt.Sprintf("%v", value.Interface()))
		}
	default:
		queryParams.Add(key, fmt.Sprintf("%v", obj))
	}
}

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 with the generated Go client's parameterToString call and the localVarQueryParams construction shown in the reproduction, using the provided Swagger YAML and generation command as inputs. Trace how optional.Interface containing []string is serialized. Done means the generated request represents the two values as repeated Ids query parameters, with regression coverage if the relevant test location can be found.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.