[BUG][SPRING] delegatePattern + unhandledException configuration Exception not throws
Created by: Thodin3
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
When using delegatePattern + unhandledException configuration :
<configOptions>
<delegatePattern>true</delegatePattern>
<unhandledException>true</unhandledException>
</configOptions>
In the interface, we have :
default ResponseEntity<Elements> _getElements(@RequestHeader(value="If-Modified-Since", required=false) OffsetDateTime ifModifiedSince) throws Exception {
return getElements(ifModifiedSince);
}
// Override this method
default ResponseEntity<Shipments> getElements(OffsetDateTime ifModifiedSince) {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
ApiUtil.setExampleResponse(...);
break;
}
}
});
return new ResponseEntity<>(HttpStatus.valueOf(200));
}
And we need to throws Exception also in the delegate method otherwise, it's not possible to implement it with the unhandled Exception (signature changed).
Would it possible to have:
default ResponseEntity<Elements> _getElements(@RequestHeader(value="If-Modified-Since", required=false) OffsetDateTime ifModifiedSince) throws Exception {
return getElements(ifModifiedSince);
}
// Override this method
default ResponseEntity<Shipments> getElements(OffsetDateTime ifModifiedSince) throws Exception {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
ApiUtil.setExampleResponse(...);
break;
}
}
});
return new ResponseEntity<>(HttpStatus.valueOf(200));
}
openapi-generator version
4.1.3 and it's not a regression
OpenAPI declaration file content or url
https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/samples/yaml/pet.yml
Command line used for generation
<configuration>
...
<configOptions>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
<interfaceOnly>true</interfaceOnly>
<delegatePattern>true</delegatePattern>
<unhandledException>true</unhandledException>
<library>spring-mvc</library>
</configOptions>
<library>spring-mvc</library>
</configuration>
Steps to reproduce
1/ run with any swagger openAPI or not 2/ configure delegatePattern + unhandledException 3/ generate code for JAVA Spring 4/ have a look to the interface and the throws Exception in delegate methods
Related issues/PRs
#4327
Suggest a fix
{{#jdk8-default-interface}}default {{/jdk8-default-interface}} {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isListContainer}}Mono{{/isListContainer}}{{#isListContainer}}Flux{{/isListContainer}}<{{{baseType}}}>{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}MultipartFile{{/isFile}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}ServerWebExchange exchange{{/reactive}}){{#unhandledException}} throws Exception{{/unhandledException}} {