Openapi-generator: [BUG][SPRING] delegatePattern + unhandledException configuration Exception not throws

Created on 30 Oct 2019  路  1Comment  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [X] Have you provided a full/minimal spec to reproduce the issue?
  • [X] Have you validated the input using an OpenAPI validator (example)?
  • [X] What's the version of OpenAPI Generator used?
  • [X] Have you search for related issues/PRs?
  • [X] What's the actual output vs expected output?
  • [X] [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

https://github.com/OpenAPITools/openapi-generator/blob/66be33a37b030fced0169b8df392c0d7817b13a1/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache#L118

    {{#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}} {
Bug

Most helpful comment

4327

>All comments

4327

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TiFu picture TiFu  路  70Comments

wing328 picture wing328  路  25Comments

seunlanlege picture seunlanlege  路  23Comments

jphorx picture jphorx  路  29Comments

MartinDelille picture MartinDelille  路  35Comments