Skip to content

Commit cbca86b

Browse files
mcac0006wing328
authored andcommitted
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)
1 parent 2c342cc commit cbca86b

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

docs/generators/spring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ sidebar_label: spring
6161
|useOptional|Use Optional container for optional parameters| |false|
6262
|hateoas|Use Spring HATEOAS library to allow adding HATEOAS links| |false|
6363
|returnSuccessCode|Generated server returns 2xx code| |false|
64+
|unhandledException|Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).|false|
6465
|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|

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class SpringCodegen extends AbstractJavaCodegen
6666
public static final String API_FIRST = "apiFirst";
6767
public static final String HATEOAS = "hateoas";
6868
public static final String RETURN_SUCCESS_CODE = "returnSuccessCode";
69+
public static final String UNHANDLED_EXCEPTION_HANDLING = "unhandledException";
6970

7071
protected String title = "OpenAPI Spring";
7172
protected String configPackage = "org.openapitools.configuration";
@@ -88,6 +89,7 @@ public class SpringCodegen extends AbstractJavaCodegen
8889
protected boolean virtualService = false;
8990
protected boolean hateoas = false;
9091
protected boolean returnSuccessCode = false;
92+
protected boolean unhandledException = false;
9193

9294
public SpringCodegen() {
9395
super();
@@ -130,6 +132,7 @@ public SpringCodegen() {
130132
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,"Use Optional container for optional parameters", useOptional));
131133
cliOptions.add(CliOption.newBoolean(HATEOAS, "Use Spring HATEOAS library to allow adding HATEOAS links", hateoas));
132134
cliOptions.add(CliOption.newBoolean(RETURN_SUCCESS_CODE, "Generated server returns 2xx code", returnSuccessCode));
135+
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));
133136

134137
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application using the SpringFox integration.");
135138
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@@ -286,6 +289,12 @@ public void processOpts() {
286289
this.setReturnSuccessCode(Boolean.valueOf(additionalProperties.get(RETURN_SUCCESS_CODE).toString()));
287290
}
288291

292+
if (additionalProperties.containsKey(UNHANDLED_EXCEPTION_HANDLING)) {
293+
this.setUnhandledException(Boolean.valueOf(additionalProperties.get(UNHANDLED_EXCEPTION_HANDLING).toString()));
294+
} else {
295+
additionalProperties.put(UNHANDLED_EXCEPTION_HANDLING, this.isUnhandledException());
296+
}
297+
289298
typeMapping.put("file", "Resource");
290299
importMapping.put("Resource", "org.springframework.core.io.Resource");
291300

@@ -687,6 +696,10 @@ public String getConfigPackage() {
687696
return this.configPackage;
688697
}
689698

699+
public boolean isUnhandledException() {
700+
return unhandledException;
701+
}
702+
690703
public void setBasePackage(String basePackage) {
691704
this.basePackage = basePackage;
692705
}
@@ -737,6 +750,10 @@ public void setReturnSuccessCode(boolean returnSuccessCode) {
737750
this.returnSuccessCode = returnSuccessCode;
738751
}
739752

753+
public void setUnhandledException(boolean unhandledException) {
754+
this.unhandledException = unhandledException;
755+
}
756+
740757
@Override
741758
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
742759
super.postProcessModelProperty(model, property);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public interface {{classname}} {
109109
produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}}
110110
consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}}{{/singleContentTypes}}
111111
method = RequestMethod.{{httpMethod}})
112-
{{#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}} {
112+
{{#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}} {
113113
{{#delegate-method}}
114114
return {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}});
115115
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void testInitialConfigValues() throws Exception {
6767
Assert.assertEquals(codegen.getConfigPackage(), "org.openapitools.configuration");
6868
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.CONFIG_PACKAGE), "org.openapitools.configuration");
6969
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.SERVER_PORT), "8082");
70+
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.UNHANDLED_EXCEPTION_HANDLING), false);
7071
}
7172

7273
@Test
@@ -78,6 +79,7 @@ public void testSettersForConfigValues() throws Exception {
7879
codegen.setInvokerPackage("xx.yyyyyyyy.invoker");
7980
codegen.setBasePackage("xx.yyyyyyyy.base");
8081
codegen.setConfigPackage("xx.yyyyyyyy.config");
82+
codegen.setUnhandledException(true);
8183
codegen.processOpts();
8284

8385
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
@@ -92,6 +94,8 @@ public void testSettersForConfigValues() throws Exception {
9294
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.BASE_PACKAGE), "xx.yyyyyyyy.base");
9395
Assert.assertEquals(codegen.getConfigPackage(), "xx.yyyyyyyy.config");
9496
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.CONFIG_PACKAGE), "xx.yyyyyyyy.config");
97+
Assert.assertEquals(codegen.isUnhandledException(), true);
98+
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.UNHANDLED_EXCEPTION_HANDLING), true);
9599
}
96100

97101
@Test

0 commit comments

Comments
 (0)