Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] generated API code swallows decoding errors #10434

Open
kevinawoo opened this issue Aug 18, 2020 · 0 comments
Open

[Go] generated API code swallows decoding errors #10434

kevinawoo opened this issue Aug 18, 2020 · 0 comments

Comments

@kevinawoo
Copy link
Contributor

kevinawoo commented Aug 18, 2020

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

kevinawoo added a commit to armory/spin that referenced this issue Aug 18, 2020
This also removes "*malformed" test cases since 2.14.4 doesn't return parsing errors

swagger-api/swagger-codegen#10434 reports the issue.
hopefully swagger-api/swagger-codegen#10429 be merged will add that ability back in.
@kevinawoo kevinawoo changed the title [Go] generated API code hides decoding errors [Go] generated API code swallows decoding errors Aug 18, 2020
mergify bot pushed a commit to spinnaker/spin that referenced this issue Aug 18, 2020
* chore(gateapi): update gateapi from 5c625da5 using swagger 2.4.14

* feat(contribution): add instructions on how to generate gateapi using 2.4.1

* refact(gateapi): swagger 2.4.14 query params are typed now

* tests(gateapi): gateapi generated code expects valid JSON and headers

This also removes "*malformed" test cases since 2.14.4 doesn't return parsing errors

swagger-api/swagger-codegen#10434 reports the issue.
hopefully swagger-api/swagger-codegen#10429 be merged will add that ability back in.

* refactor(imports): fixed import styling

* chore(gateapi): use swagger-cli 2.4.15
gsapkal pushed a commit to gsapkal/spin that referenced this issue Sep 10, 2020
…#292)

* chore(gateapi): update gateapi from 5c625da5 using swagger 2.4.14

* feat(contribution): add instructions on how to generate gateapi using 2.4.1

* refact(gateapi): swagger 2.4.14 query params are typed now

* tests(gateapi): gateapi generated code expects valid JSON and headers

This also removes "*malformed" test cases since 2.14.4 doesn't return parsing errors

swagger-api/swagger-codegen#10434 reports the issue.
hopefully swagger-api/swagger-codegen#10429 be merged will add that ability back in.

* refactor(imports): fixed import styling

* chore(gateapi): use swagger-cli 2.4.15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant