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

@@ -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);