Do not check status code for default response (#3322)

* Do not check status code for default response

* Updated generated code/docs

Because Circle CI said

> Please run 'bin/utils/ensure-up-to-date' locally and commit
> changes (UNCOMMITTED CHANGES ERROR)
This commit is contained in:
Thiago Arrais
2019-10-14 14:29:46 -03:00
committed by William Cheng
parent 2cab048d41
commit cb2bf4d2bf
56 changed files with 377 additions and 219 deletions

View File

@@ -2665,6 +2665,11 @@ public class DefaultCodegen implements CodegenConfig {
op.isResponseFile = Boolean.TRUE;
}
}
op.responses.sort((a, b) -> {
int aDefault = "0".equals(a.code) ? 1 : 0;
int bDefault = "0".equals(b.code) ? 1 : 0;
return aDefault - bDefault;
});
op.responses.get(op.responses.size() - 1).hasMore = false;
if (methodResponse != null) {

View File

@@ -319,7 +319,9 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams
}
{{#responses}}
{{#dataType}}
{{^wildcard}}
if localVarHTTPResponse.StatusCode == {{{code}}} {
{{/wildcard}}
var v {{{dataType}}}
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
@@ -327,8 +329,13 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
}
newErr.model = v
{{#hasMore}}
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
{{/hasMore}}
{{^wildcard}}
}
{{/wildcard}}
{{/dataType}}
{{/responses}}
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr

View File

@@ -317,7 +317,9 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams
}
{{#responses}}
{{#dataType}}
{{^wildcard}}
if localVarHTTPResponse.StatusCode == {{{code}}} {
{{/wildcard}}
var v {{{dataType}}}
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
@@ -325,8 +327,12 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#hasParams
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
}
newErr.model = v
{{#hasMore}}
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
{{/hasMore}}
{{^wildcard}}
}
{{/wildcard}}
{{/dataType}}
{{/responses}}
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr

View File

@@ -595,6 +595,25 @@ public class DefaultCodegenTest {
Assert.assertEquals(co2.path, "/some/path");
}
@Test
public void testDefaultResponseShouldBeLast() {
OpenAPI openAPI = TestUtils.createOpenAPI();
Operation myOperation = new Operation().operationId("myOperation").responses(
new ApiResponses()
.addApiResponse(
"default", new ApiResponse().description("Default"))
.addApiResponse(
"422", new ApiResponse().description("Error"))
);
openAPI.path("/here", new PathItem().get(myOperation));
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
CodegenOperation co = codegen.fromOperation("/here", "get", myOperation, null);
Assert.assertEquals(co.responses.get(0).message, "Error");
Assert.assertEquals(co.responses.get(1).message, "Default");
}
@Test
public void testResponseWithNoSchemaInHeaders() {
OpenAPI openAPI = TestUtils.createOpenAPI();