Skip to content

[Go] generated API code swallows decoding errors #10434

Open
@kevinawoo

Description

@kevinawoo
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 #10429

Suggest a fix/enhancement

#10429

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions