swagger-api / swagger-api/swagger-codegen

[Go] generated API code swallows decoding errors

Open
#10,434 0 comments 1 reaction 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

If the API returns a 200, but the JSON payload is malformed, then the code Go code generated hides the decoding errors. It's more useful to surface these errors to the developer to handle.

Generated template currently with 2.4.15:

if localVarHttpResponse.StatusCode < 300 {
	// If we succeed, return the data, otherwise pass on to decode error.
	err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
	if err == nil { 
		return localVarReturnValue, localVarHttpResponse, err
	}
}

Proposed change:

if localVarHttpResponse.StatusCode < 300 {
	// If we succeed, return the data, otherwise pass on to decode error.
	err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
	return localVarReturnValue, localVarHttpResponse, err
}
Swagger-codegen version

2.4.15.
Technically, it's not a regression since 2.3.x returned generic interface{}s. That version required you to do your own decoding.

Swagger declaration file content or url

not needed, it's with all Go generated code

Command line used for generation

swagger-cli generate

Steps to reproduce

You can generate any code.

Related issues/PRs

I opened https://github.com/swagger-api/swagger-codegen/pull/10429

Suggest a fix/enhancement

https://github.com/swagger-api/swagger-codegen/pull/10429

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 Go code-generation template that emits the shown decode block and compare the proposed change in PR 10429. Confirm how malformed JSON from a successful response is handled, then ensure the generated client returns that decoding error; the issue does not name a file or test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.