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] Support for response status code ranges #10075

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3883,9 +3883,9 @@ public CodegenOperation fromOperation(String path,
}
}
op.responses.sort((a, b) -> {
int aDefault = "0".equals(a.code) ? 1 : 0;
int bDefault = "0".equals(b.code) ? 1 : 0;
return aDefault - bDefault;
int aScore = a.isWildcard() ? 2 : a.isRange() ? 1 : 0;
int bScore = b.isWildcard() ? 2 : b.isRange() ? 1 : 0;
return Integer.compare(aScore, bScore);
});

if (methodResponse != null) {
Expand Down
13 changes: 13 additions & 0 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,22 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{#dataType}}
{{^is1xx}}
{{^is2xx}}
{{#range}}
{{#is3xx}}
if localVarHTTPResponse.StatusCode >= 300 && localVarHTTPResponse.StatusCode < 400 {
{{/is3xx}}
{{#is4xx}}
if localVarHTTPResponse.StatusCode >= 400 && localVarHTTPResponse.StatusCode < 500 {
{{/is4xx}}
{{#is5xx}}
if localVarHTTPResponse.StatusCode >= 500
{{/is5xx}}
{{/range}}
{{^range}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Shall we remove the following before and after the {{#range}} ... {{/range}} check?

		{{^is1xx}}
		{{^is2xx}}

...
		{{/is2xx}}
		{{/is1xx}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing those checks causes unreachable code to be generated due to the if localVarHTTPResponse.StatusCode >= 300 { a few lines up. We're only interested in decoding "unsuccessful" responses at this point and those negative checks are the only way to do this AFAIK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

{{^wildcard}}
if localVarHTTPResponse.StatusCode == {{{code}}} {
{{/wildcard}}
{{/range}}
var v {{{dataType}}}
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ paths:
properties:
string:
$ref: '#/components/schemas/Foo'
4XX:
description: client error
content:
application/json:
schema:
type: object
properties:
string:
$ref: '#/components/schemas/Foo'
404:
description: not found
content:
application/json:
schema:
type: object
properties:
string:
$ref: '#/components/schemas/Foo'
/pet:
servers:
- url: 'http://petstore.swagger.io/v2'
Expand Down
12 changes: 12 additions & 0 deletions samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ paths:
schema:
$ref: '#/components/schemas/inline_response_default'
description: response
"4XX":
content:
application/json:
schema:
$ref: '#/components/schemas/inline_response_default'
description: client error
"404":
content:
application/json:
schema:
$ref: '#/components/schemas/inline_response_default'
description: not found
/pet:
post:
operationId: addPet
Expand Down
20 changes: 20 additions & 0 deletions samples/openapi3/client/petstore/go/go-petstore/api_default.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.