OpenAPITools / OpenAPITools/openapi-generator

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

Open
#14,288 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the supplied OpenAPI document and generation command, then inspect generated/src/main/java/org/openapitools/api/AbstractDefaultController.java. Find the java-micronaut-server Mustache template that produces the controller delegation and update its void-operation handling; done means the generated Java compiles without returning from a void method.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.