Repository: OpenAPITools/openapi-generator

Issue #13124: [REQ] [Java] Support of JakartaEE 9
Created: 2022-08-07T14:50:26Z
Context
Since Eclipse Foundation is in charge of JakartaEE specifications, it's time to switch to JakartaEE 9 for openapi generator.
The first step was to switch to JakartaEE 8 (see the issue #10504). It's done now.
The next step is to upgrade to JakartaEE 9 which means to upgrade to new versions of jakarta's dependencies.
But this upgrade is a bit more difficult than the previous because the main goal of JakartaEE 9 is that the javax.* packages to jakarta.* packages have been renamed by Eclipse.
I think that it will be necessary to create a new property (named jakartaeeVersion=9 for example) to allow the user to generate the source code with the actual versions of jakartaEE 8 or the last versions of jakartaEE 9.
Describe the solution you'd like
We need to upgrade the following dependencies in the Maven/Gradle/Scala mustache template files. These template files are located in openapi-generator/modules/openapi-generator/src/main/resources.
jakarta.annotation:jakarta.annotation-api:1.3.5 => 2.0.0
jakarta.validation:jakarta.validation-api:2.0.2 => 3.0.0
jakarta.ws.rs:jakarta.ws.rs-api:2.1.6 => 3.0.0
jakarta.json.bind:jakarta.json.bind-api:1.0.2 => 2.0.0
jakarta.json:jakarta.json-api:1.1.6 => 2.0.0
jakarta.xml.bind:jakarta.xml.bind-api:2.3.3 => 3.0.0
jakarta.el:jakarta.el-api:3.0.3 => 4.0.0
jakarta.servlet:jakarta.servlet-api:4.0.4 => 5.0.0
jakarta.activation:jakarta.activation-api:1.2.2 => 2.0.0
The upgrade of jakarta versions will force us to replace all javax.* import by jakarta.* in the mustache templates.
Then, we need a new property named jakartaeeVersion to keep the generation source code as is (with the package javax.* ) or to use the new jakartaEE 9 classes in the generated source code.
By default, if the property didn't set, we consider that we are still on jakartaEE 8 (javax.*).
Beware, some samples are dependents of Spring Boot. For them, it will be necessary to upgrade the version of spring boot parent to 3 (release in September 2022). It is not part of the scope of this issue.
How to validate the request
We need to create an unit test with the property jakartaeeVersion. The versions of depedencies should be equal to the versions above.
And of course, we need to create a new java sample with the new property.
Additional context
Versions of JakartaEE 9 : https://projects.eclipse.org/releases/jakarta-ee-9. Or a map here https://jakarta.ee/release/9/
Explanations of JakartaEE 9 release plan : https://eclipse-ee4j.github.io/jakartaee-platform/jakartaee9/JakartaEE9ReleasePlan

Duplicate issue #14100: [BUG][JAVA][SPRING] missing JsonValue import if allOf used with discriminator
Created: 2022-11-23T09:37:58Z
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
Missing import means that code does not compile...
openapi-generator version
6.3.0-snapshot, 6.2.1
OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Test
  description: 'Test'
  version: v2.0

tags:
  - name: Test

paths:
  /test:
    get:
      tags:
        - Test
      description: Test
      operationId: test
      responses:
        '204':
          description: ''
    
components:
  schemas:
    SimulationEvent:
      required:
        - eventTime
      type: object
      properties:
        eventTime:
          type: integer
          format: int64
      discriminator:
        propertyName: type
    TransactionEvent:
      type: object
      properties:
        transactionType:
          type: string
          enum:
            - purchase
            - withdraw
            - refund
            - load
      allOf:
        - $ref: '#/components/schemas/SimulationEvent'
    
Generation Details
docker run --rm --workdir /github/workspace -v $(pwd):/github/workspace openapitools/openapi-generator-cli:v6.2.1 generate -g spring -o /github/workspace/spring-client -i /github/workspace/contract.yml -p groupId=co.test,basePackage=co.test.v2.server.config,basePackage=co.test.v2.server,apiPackage=co.test.v2.server,invokerPackage=co.test.v2.server.invoker,modelPackage=co.test.v2.server.models,library=spring-boot,artifactId=test-v2-server,documentationProvider=none,useSwaggerUI=false,interfaceOnly=true,snapshotVersion=true

Steps to reproduce

Save the contract above into file contract.yml
Run the docker command above
open spring-client/src/main/java/co/test/v2/server/models/TransactionEvent.java
Verify that @JsonValue is used in code but it is not imported

Related issues/PRs
There was similar issue before #11323 And I can confirm that this is similar nature issue because hasEnums is not set during generation of that class...
Suggest a fix
I dont have suggestion for a fix, but tmp workaround is to add this code on top of pojo.mustache:
{{#jackson}}
{{^hasEnums}}
import com.fasterxml.jackson.annotation.JsonValue;
{{/hasEnums}}
{{/jackson}}

As you can see I am adding import only when the hasEnums is false... Because otherwise we are risking that we will have same import twice...
Important debugging note:
If you remove discriminator from the contract above, the generation is working fine...

Closing comment (2023-02-28T08:16:29Z): At the moment we are using a workaround similar to #13124 (comment)
In gradle:
tasks.register('addJsonValueImport') {
    doLast {
        ant.replace(dir: 'build', includes:'**/<some file>.java',
                token:'import com.fasterxml.jackson.annotation.JsonTypeName;',
                value:'import com.fasterxml.jackson.annotation.JsonTypeName;\nimport com.fasterxml.jackson.annotation.JsonValue;\n')
    }
}

tasks.named('compileJava').configure {
    dependsOn unpack, generateModel, addJsonValueImport
}