swagger-api / swagger-api/swagger-codegen

[Go] [Golang] [Client] No chance to obtain error payload

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

No opportunity to get error payload without crutches
Error response models are generated, but not used
related to #9109

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"
        400:
          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
moreover, in case of error localVarHttpResponse.Body is returned read and closed, so there isn't even an opportunity to get error payload by yourself, except parsing returned error, what is a disgusting crutch

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 generated DefaultApiService.RestPost entry point and the error-handling path around localVarHttpResponse.Body; review the related issue #9109 for context. Done should let callers obtain the declared 400 Bar error payload without relying on a formatted error string, with the response body handled safely.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.