Skip to content

Commit 9c66d33

Browse files
authored
[go] Support for response status code ranges (OpenAPITools#10075)
1 parent badfdcc commit 9c66d33

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3906,9 +3906,9 @@ public CodegenOperation fromOperation(String path,
39063906
}
39073907
}
39083908
op.responses.sort((a, b) -> {
3909-
int aDefault = "0".equals(a.code) ? 1 : 0;
3910-
int bDefault = "0".equals(b.code) ? 1 : 0;
3911-
return aDefault - bDefault;
3909+
int aScore = a.isWildcard() ? 2 : a.isRange() ? 1 : 0;
3910+
int bScore = b.isWildcard() ? 2 : b.isRange() ? 1 : 0;
3911+
return Integer.compare(aScore, bScore);
39123912
});
39133913

39143914
if (methodResponse != null) {

modules/openapi-generator/src/main/resources/go/api.mustache

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,22 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
356356
{{#dataType}}
357357
{{^is1xx}}
358358
{{^is2xx}}
359+
{{#range}}
360+
{{#is3xx}}
361+
if localVarHTTPResponse.StatusCode >= 300 && localVarHTTPResponse.StatusCode < 400 {
362+
{{/is3xx}}
363+
{{#is4xx}}
364+
if localVarHTTPResponse.StatusCode >= 400 && localVarHTTPResponse.StatusCode < 500 {
365+
{{/is4xx}}
366+
{{#is5xx}}
367+
if localVarHTTPResponse.StatusCode >= 500
368+
{{/is5xx}}
369+
{{/range}}
370+
{{^range}}
359371
{{^wildcard}}
360372
if localVarHTTPResponse.StatusCode == {{{code}}} {
361373
{{/wildcard}}
374+
{{/range}}
362375
var v {{{dataType}}}
363376
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
364377
if err != nil {

modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ paths:
2929
properties:
3030
string:
3131
$ref: '#/components/schemas/Foo'
32+
4XX:
33+
description: client error
34+
content:
35+
application/json:
36+
schema:
37+
type: object
38+
properties:
39+
string:
40+
$ref: '#/components/schemas/Foo'
41+
404:
42+
description: not found
43+
content:
44+
application/json:
45+
schema:
46+
type: object
47+
properties:
48+
string:
49+
$ref: '#/components/schemas/Foo'
3250
/pet:
3351
servers:
3452
- url: 'http://petstore.swagger.io/v2'

samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ paths:
5050
schema:
5151
$ref: '#/components/schemas/inline_response_default'
5252
description: response
53+
"4XX":
54+
content:
55+
application/json:
56+
schema:
57+
$ref: '#/components/schemas/inline_response_default'
58+
description: client error
59+
"404":
60+
content:
61+
application/json:
62+
schema:
63+
$ref: '#/components/schemas/inline_response_default'
64+
description: not found
5365
/pet:
5466
post:
5567
operationId: addPet

samples/openapi3/client/petstore/go/go-petstore/api_default.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)