Add unhandledException to 'spring' generator, which declares all operation methods to throw a generic exception, allowing implementations to unhandle checked exceptions. This is useful whenever it is unnecessary to handle specific exceptions and, more importantly, allow generic constructs (eg. Spring @ControllerAdvice) to tackle unhandled exceptions from a single point (for instance, tackle all unhandled exceptions as an HTTP500 while excluding the stacktrace in the response body). (#2482)

This commit is contained in:
Matthew Cachia 2019-07-04 14:19:40 +02:00 committed by William Cheng
parent 2c342cc2b4
commit cbca86b43a
4 changed files with 23 additions and 1 deletions

View File

@ -61,4 +61,5 @@ sidebar_label: spring
|useOptional|Use Optional container for optional parameters| |false|
|hateoas|Use Spring HATEOAS library to allow adding HATEOAS links| |false|
|returnSuccessCode|Generated server returns 2xx code| |false|
|unhandledException|Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).|false|
|library|library template (sub-template)|<dl><dt>**spring-boot**</dt><dd>Spring-boot Server application using the SpringFox integration.</dd><dt>**spring-mvc**</dt><dd>Spring-MVC Server application using the SpringFox integration.</dd><dt>**spring-cloud**</dt><dd>Spring-Cloud-Feign client with Spring-Boot auto-configured settings.</dd><dl>|spring-boot|

View File

@ -66,6 +66,7 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String API_FIRST = "apiFirst";
public static final String HATEOAS = "hateoas";
public static final String RETURN_SUCCESS_CODE = "returnSuccessCode";
public static final String UNHANDLED_EXCEPTION_HANDLING = "unhandledException";
protected String title = "OpenAPI Spring";
protected String configPackage = "org.openapitools.configuration";
@ -88,6 +89,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean virtualService = false;
protected boolean hateoas = false;
protected boolean returnSuccessCode = false;
protected boolean unhandledException = false;
public SpringCodegen() {
super();
@ -130,6 +132,7 @@ public class SpringCodegen extends AbstractJavaCodegen
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,"Use Optional container for optional parameters", useOptional));
cliOptions.add(CliOption.newBoolean(HATEOAS, "Use Spring HATEOAS library to allow adding HATEOAS links", hateoas));
cliOptions.add(CliOption.newBoolean(RETURN_SUCCESS_CODE, "Generated server returns 2xx code", returnSuccessCode));
cliOptions.add(CliOption.newBoolean(UNHANDLED_EXCEPTION_HANDLING, "Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).", unhandledException));
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application using the SpringFox integration.");
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@ -286,6 +289,12 @@ public class SpringCodegen extends AbstractJavaCodegen
this.setReturnSuccessCode(Boolean.valueOf(additionalProperties.get(RETURN_SUCCESS_CODE).toString()));
}
if (additionalProperties.containsKey(UNHANDLED_EXCEPTION_HANDLING)) {
this.setUnhandledException(Boolean.valueOf(additionalProperties.get(UNHANDLED_EXCEPTION_HANDLING).toString()));
} else {
additionalProperties.put(UNHANDLED_EXCEPTION_HANDLING, this.isUnhandledException());
}
typeMapping.put("file", "Resource");
importMapping.put("Resource", "org.springframework.core.io.Resource");
@ -687,6 +696,10 @@ public class SpringCodegen extends AbstractJavaCodegen
return this.configPackage;
}
public boolean isUnhandledException() {
return unhandledException;
}
public void setBasePackage(String basePackage) {
this.basePackage = basePackage;
}
@ -737,6 +750,10 @@ public class SpringCodegen extends AbstractJavaCodegen
this.returnSuccessCode = returnSuccessCode;
}
public void setUnhandledException(boolean unhandledException) {
this.unhandledException = unhandledException;
}
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);

View File

@ -109,7 +109,7 @@ public interface {{classname}} {
produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}}
consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}}{{/singleContentTypes}}
method = RequestMethod.{{httpMethod}})
{{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{^hasMore}}{{#reactive}}, {{/reactive}}{{/hasMore}}{{/allParams}}{{#reactive}}ServerWebExchange exchange{{/reactive}}){{^jdk8}};{{/jdk8}}{{#jdk8}} {
{{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{^hasMore}}{{#reactive}}, {{/reactive}}{{/hasMore}}{{/allParams}}{{#reactive}}ServerWebExchange exchange{{/reactive}}){{^jdk8}};{{/jdk8}}{{#jdk8}}{{#unhandledException}} throws Exception{{/unhandledException}} {
{{#delegate-method}}
return {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}});
}

View File

@ -67,6 +67,7 @@ public class SpringCodegenTest {
Assert.assertEquals(codegen.getConfigPackage(), "org.openapitools.configuration");
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.CONFIG_PACKAGE), "org.openapitools.configuration");
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.SERVER_PORT), "8082");
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.UNHANDLED_EXCEPTION_HANDLING), false);
}
@Test
@ -78,6 +79,7 @@ public class SpringCodegenTest {
codegen.setInvokerPackage("xx.yyyyyyyy.invoker");
codegen.setBasePackage("xx.yyyyyyyy.base");
codegen.setConfigPackage("xx.yyyyyyyy.config");
codegen.setUnhandledException(true);
codegen.processOpts();
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
@ -92,6 +94,8 @@ public class SpringCodegenTest {
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.BASE_PACKAGE), "xx.yyyyyyyy.base");
Assert.assertEquals(codegen.getConfigPackage(), "xx.yyyyyyyy.config");
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.CONFIG_PACKAGE), "xx.yyyyyyyy.config");
Assert.assertEquals(codegen.isUnhandledException(), true);
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.UNHANDLED_EXCEPTION_HANDLING), true);
}
@Test