swagger-api / swagger-api/swagger-codegen

[Go] [Golang] [Client] Can not handle 2 different successful payloads

Open
#9,109 0 comments 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

Generated golang client for endpoint which has several successful responses with different response payloads fails to handle all of them. It chooses one model and try to Unmarshal all payloads into it.

Swagger-codegen version

2.3.1

Swagger declaration file content or url
swagger: '2.0'
info:
  description: Example
  title: Example
  version: '1.0.0'
consumes:
  - application/json
produces:
  - application/json
paths:
  /rest:
    post:
      summary: "rest API"
      produces:
      - "application/json"
      responses:
        201:
          description: "tododo"
          schema:
            $ref: "#/definitions/foo"
        202:
          description: "dototo"
          schema:
            $ref: "#/definitions/bar"
definitions:
  foo:
    type: object
    properties:
      id:
        type: number
  bar:
    type: object
    properties:
      number:
        type: string
Generated code

look, it even doesn't use Bar model

/* DefaultApiService rest API
 * @param ctx context.Context for authentication, logging, tracing, etc.
 @return Foo*/
func (a *DefaultApiService) RestPost(ctx context.Context) (Foo,  *http.Response, error) {
	var (
		localVarHttpMethod = strings.ToUpper("Post")
		localVarPostBody interface{}
		localVarFileName string
		localVarFileBytes []byte
	 	successPayload  Foo
	)

	// create path and map variables
	localVarPath := a.client.cfg.BasePath + "/rest"

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}


	// to determine the Content-Type header
	localVarHttpContentTypes := []string{ "application/json",  }

	// set Content-Type header
	localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
	if localVarHttpContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHttpContentType
	}

	// to determine the Accept header
	localVarHttpHeaderAccepts := []string{
		"application/json",
		}

	// set Accept header
	localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
	if localVarHttpHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
	}
	r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
	if err != nil {
		return successPayload, nil, err
	}

	localVarHttpResponse, err := a.client.callAPI(r)
	if err != nil || localVarHttpResponse == nil {
		return successPayload, localVarHttpResponse, err
	}
	defer localVarHttpResponse.Body.Close()
	if localVarHttpResponse.StatusCode >= 300 {
		bodyBytes, _ := ioutil.ReadAll(localVarHttpResponse.Body)
		return successPayload, localVarHttpResponse, reportError("Status: %v, Body: %s", localVarHttpResponse.Status, bodyBytes)
	}

	if err = json.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil {
		return successPayload, localVarHttpResponse, err
	}


	return successPayload, localVarHttpResponse, err
}

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 Swagger declaration for POST /rest and the generated DefaultApiService.RestPost entry point, which currently returns and decodes Foo while the 202 response uses Bar. Reproduce the generated client with the supplied Foo and Bar definitions, then verify that both successful response payloads are handled according to their status codes.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, 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.