OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.36k stars 6.46k forks source link

[BUG] java-micronaut-server + abstractcontroller: void methods lead to compilation error in the generated code #14288

Open staizen-rcomblen opened 1 year ago

staizen-rcomblen commented 1 year ago

Bug Report Checklist

Description

java-micronaut-server with reactive=false and generateControllerAsAbstract=true leads to invalid Java code for void operations.

Generated code is:

    @Get(uri="/list")
    @Produces(value = {})
    @Secured({SecurityRule.IS_ANONYMOUS})
    public void listGetApi() {
        return listGet();
    }

Expected output is:

    @Get(uri="/list")
    @Produces(value = {})
    @Secured({SecurityRule.IS_ANONYMOUS})
    public void listGetApi() {
        listGet();
    }

you cannot return something from a void method. Java compiler complains.

openapi-generator version

6.2.1

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Sample API
  description: A sample API to illustrate OpenAPI concepts
paths:
  /list:
    get:
      description: Returns a list of stuff
      responses:
        '200':
          description: Successful response
Generation Details
npx @openapitools/openapi-generator-cli generate -i test.yaml -g java-micronaut-server --additional-properties=generateControllerAsAbstract=true,reactive=false -o $PWD/generated
Steps to reproduce
 ~/sandbox/openapi-micronaut-bug$ cat test.yaml 

openapi: 3.0.0
info:
  version: 1.0.0
  title: Sample API
  description: A sample API to illustrate OpenAPI concepts
paths:
  /list:
    get:
      description: Returns a list of stuff
      responses:
        '200':
          description: Successful response
 ~/sandbox/openapi-micronaut-bug$ cat run.sh 
#!/bin/bash
cd `dirname $0`
rm -rf generated
npx @openapitools/openapi-generator-cli generate -i test.yaml -g java-micronaut-server --additional-properties=generateControllerAsAbstract=true,reactive=false -o $PWD/generated
 ~/sandbox/openapi-micronaut-bug$ ./run.sh 
[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - Output directory (/home/rcomblen/sandbox/openapi-micronaut-bug/generated) does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: java-micronaut-server (server)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'java-micronaut-server' is considered beta.
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Environment variable JAVA_POST_PROCESS_FILE not defined so the Java code may not be properly formatted. To define it, try 'export JAVA_POST_PROCESS_FILE="/usr/local/bin/clang-format -i"' (Linux/Mac)
[main] INFO  o.o.c.languages.AbstractJavaCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation null
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] WARN  o.o.codegen.DefaultCodegen - Empty operationId found for path: get /list. Renamed to auto-generated operationId: listGet
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/src/main/java/org/openapitools/api/AbstractDefaultController.java
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/src/test/java/org/openapitools/api/AbstractDefaultControllerTest.java
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/src/main/java/org/openapitools/controller/DefaultController.java
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/docs/controllers/AbstractDefaultController.md
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/src/main/resources/application.yml
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/src/main/resources/logback.xml
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/build.gradle
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/settings.gradle
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/gradle.properties
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/gradlew
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/gradlew.bat
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/gradle/wrapper/gradle-wrapper.properties
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/gradle/wrapper/gradle-wrapper.jar
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/pom.xml
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/mvnw
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/mvnw.bat
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/.mvn/wrapper/MavenWrapperDownloader.java
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/.mvn/wrapper/maven-wrapper.jar
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/.mvn/wrapper/maven-wrapper.properties
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/.gitignore
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/README.md
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/src/main/java/org/openapitools/Application.java
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/.openapi-generator-ignore
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/.openapi-generator/VERSION
[main] INFO  o.o.codegen.TemplateManager - writing file /home/rcomblen/sandbox/openapi-micronaut-bug/generated/.openapi-generator/FILES
################################################################################
# Thanks for using OpenAPI Generator.                                          #
# Please consider donation to help us maintain this project 🙏                 #
# https://opencollective.com/openapi_generator/donate                          #
################################################################################
 ~/sandbox/openapi-micronaut-bug$ cat generated/src/main/java/org/openapitools/api/AbstractDefaultController.java | grep -C 10 "return list"
    @Operation(
        operationId = "listGet",
        responses = {
            @ApiResponse(responseCode = "200", description = "Successful response")
        }
    )
    @Get(uri="/list")
    @Produces(value = {})
    @Secured({SecurityRule.IS_ANONYMOUS})
    public void listGetApi() {
        return listGet();
    }

    /**
     *
     * This method will be delegated to when the controller gets a request
     */
    public void listGet() {
            // TODO implement listGet();
        throw new HttpStatusException(HttpStatus.NOT_IMPLEMENTED, null);
Related issues/PRs

/

Suggest a fix

Mustache template should specifically handle this void methods case.

rodrigoma3 commented 1 year ago

Using Java Spring the return void is not primitive and this forces you to implement a return null. There is issue #12341 for using primitive void and not having return.

altro3 commented 3 weeks ago

generateControllerAsAbstract property doesn't support anymore, now always create interface

Just use official micronaut generator for java and kotlin by micronaut-opeanpi gradle or maven plugin from this repo: https://github.com/micronaut-projects/micronaut-openapi

Look to this guide: https://guides.micronaut.io/latest/micronaut-openapi-generator-server.html

Also, please describe problems and suggestions here: https://github.com/micronaut-projects/micronaut-openapi/issues