OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][JAX-RS] Interface does not generate additional model type annotations

Open
#15,066 0 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)?
    I assume OpenAPI Generator's own samples are valid.
  • 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

Interfaces cannot be generated with additional model type annotations. This is required to more easily mock them in within the Quarkus framework.

openapi-generator version

6.4.0

OpenAPI declaration file content or url
swagger: "2.0"
info:
  version: 1.0.0
  title: Swagger Petstore
consumes: [application/json]
produces: [application/json]
paths:
  /pets:
    get:
      responses:
        "200":
          description: pet response
          schema:
            type: string
        default:
          description: unexpected error
          schema:
            $ref: string

Generation Details

Clone this repo and step inside it: openapi-generator--BUG-JAVA-Interface-does-not-generate-additional-model-type-annotations-example
Java 17 is required.
Run ./gradlew clean openApiGenerateJaxrs and the generated file is created in this path: build/generated-openapi/src/gen/java/com/example/api/resource/PetsApi.java

Here is the actual output (to the left) and the expected output (to the right):

package com.example.api.resource;                               package com.example.api.resource;


import javax.ws.rs.*;                                           import javax.ws.rs.*;
import javax.ws.rs.core.Response;                               import javax.ws.rs.core.Response;



import java.io.InputStream;                                     import java.io.InputStream;
import java.util.Map;                                           import java.util.Map;
import java.util.List;                                          import java.util.List;
import javax.validation.constraints.*;                          import javax.validation.constraints.*;
import javax.validation.Valid;                                  import javax.validation.Valid;

@Path("/pets")                                                  @Path("/pets")
                                                              > @javax.enterprise.context.ApplicationScoped
@javax.annotation.Generated(value = "org.openapitools.codegen   @javax.annotation.Generated(value = "org.openapitools.codegen
public interface PetsApi {                                      public interface PetsApi {

    @GET                                                            @GET
    @Produces({ "application/json" })                               @Produces({ "application/json" })
    String petsGet();                                               String petsGet();
}                                                               }

The bug appears to come from the fact that the configOption additionalModelTypeAnnotation is ignored when running together with the configOption interfaceOnly set to "true".

Steps to reproduce

Set your equivalent openApiGenerate options (if you are running maven or gradle) to these:
Set the configvalue additionalModelTypeAnnotation to anything and set the configOption interfaceOnly to "true".

Now your openApiGenerate should look as following (for example in build.gradle):

task openApiGenerateJaxrs(type: GenerateTask) {
    generatorName = "jaxrs-spec"
    inputSpec = "$projectDir/contract.yaml"
    outputDir = "$buildDir/generated-openapi"
    apiPackage = "com.example.api.resource"
    invokerPackage = "com.example.api.invoker"
    modelPackage = "com.example.api.model"
    configOptions = [
        additionalModelTypeAnnotation: "@javax.enterprise.context.ApplicationScoped",
        library: "quarkus",
        interfaceOnly: "true",
        useSwaggerAnnotations: "false"
    ]
    globalProperties = [
        apiTests: "false",
        modelTests: "false"
    ]
}

Running ./gradlew clean openApiGenerateJaxrs will not produce interfaces with desired Annotation.

Suggest a fix

I am suspecting it's one of the mustache files which are missing the double variable, but I cannot guess which one {{>generatedAnnotation}{{>additionalModelTypeAnnotations}} .

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

Start with the JAX-RS generator templates and the handling of the additionalModelTypeAnnotation and interfaceOnly options. Run ./gradlew clean openApiGenerateJaxrs using the provided reproduction and inspect build/generated-openapi/src/gen/java/com/example/api/resource/PetsApi.java. Done means the generated interface includes @javax.enterprise.context.ApplicationScoped when both options are enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.