Is there a way to configure openapi-generator to use jakarta package during generation
Asked Answered
R

3

63

I've recently upgraded my project to use spring-boot 3.0.0. So I don't have javax.* modules in the project anymore. But the Open API generator keeps trying to import javax modules. Especially, it uses javax.annotation.Generated for the @Generated annotation which is not present in the project anymore. Is there a way to reconfigure it somehow?

Remind answered 27/11, 2022 at 20:17 Comment(2)
The generator documentation page (openapi-generator.tech/docs/generators) is always the first goto in these cases. OpenAPI tech is well adapted and chances are, the community has already hit the issue and delivered the fix – Erythrite
@SANJAYGAUTAM see sashok_bg's answer https://mcmap.net/q/319409/-is-there-a-way-to-configure-openapi-generator-to-use-jakarta-package-during-generation - it works with <configOptions> <useJakartaEe>true</useJakartaEe> </configOptions> since some version. I am using it and I can confirm that it works. In my opinion this should be the accepted answer. The useSpringBoot3 answer with the most votes works only for server-side generator spring. – Alliterative
P
42

You should follow the documentation whenever possible.

The property that you need is either "useSpringBoot3" or "useJakartaEe"

  1. Go to https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin

  2. At the end of the table you see "configHelp" property which will give you configs for the current generator "spring" in my case

  3. Rerun "mvn clean install" - this will give you a list of available "configOptions".

  4. Read the list and found a property

    useJakartaEe: whether to use Jakarta EE namespace instead of javax (Default: false)

My final pom:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.4.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <configHelp>false</configHelp>
                <configOptions>
                    <useJakartaEe>true</useJakartaEe>
                </configOptions>
                <inputSpec>
                    ${project.basedir}/src/main/resources/api.openapi.yaml
                </inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>some.package</apiPackage>
                <modelPackage>some.package.model</modelPackage>
            </configuration>
        </execution>
    </executions>
</plugin>

Cheers

Picturesque answered 15/3, 2023 at 10:26 Comment(4)
using the setting <useJakartaEe>true</useJakartaEe> gives me the error package jakarta.annotation does not exist. What am I doing wrong? Here is the pom snippet: pastebin.com/KGvwFauv – Jordanson
@ArthurEirich do you have the correct Jakarta dependency versions? I had a similar error, it turned out I forgot to include jakarta.annotation-api in a version which actually uses the new jakarta.annotation package name 🀦 – Lemmy
I had all the needed dependencies. My problem was the option <globalProperties<skipFormModel>false</skipFormModel></globalProperties> not being set. After I scrolled trhough the log I saw a warning that a model class wasnt generated. Googled the log message for it and found a github bug – Jordanson
@DonHo github.com/OpenAPITools/openapi-generator/issues/10415 and github.com/OpenAPITools/openapi-generator/issues/12036 are those bugs where I found out about the option – Jordanson
A
48

Yes, you can use useSpringBoot3: "true" in your configoptions of the generator. Example in gradle:

        configOptions = [
            useSpringBoot3: "true"
        ]
Arguseyed answered 28/11, 2022 at 13:53 Comment(4)
For some weird reason, this option works only for the SPRING generator openapi-generator.tech/docs/generators/spring, which generates the server. – Alliterative
The top option only works for the Spring generator. If you are generating code for the client like Java generator, this option is not available. Currently I don't see any option to allow Jakarta migration. – Harte
Its still an open issue : github.com/OpenAPITools/openapi-generator/issues/13124 – Harte
It seems that setting useJakartaEe: 'true' should work in the java generator since openapi-generator 6.3.0, but apparently some configurations are not working well, and thus the issue is still open. It works well for me using the native library! – Evincive
P
42

You should follow the documentation whenever possible.

The property that you need is either "useSpringBoot3" or "useJakartaEe"

  1. Go to https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin

  2. At the end of the table you see "configHelp" property which will give you configs for the current generator "spring" in my case

  3. Rerun "mvn clean install" - this will give you a list of available "configOptions".

  4. Read the list and found a property

    useJakartaEe: whether to use Jakarta EE namespace instead of javax (Default: false)

My final pom:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.4.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <configHelp>false</configHelp>
                <configOptions>
                    <useJakartaEe>true</useJakartaEe>
                </configOptions>
                <inputSpec>
                    ${project.basedir}/src/main/resources/api.openapi.yaml
                </inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>some.package</apiPackage>
                <modelPackage>some.package.model</modelPackage>
            </configuration>
        </execution>
    </executions>
</plugin>

Cheers

Picturesque answered 15/3, 2023 at 10:26 Comment(4)
using the setting <useJakartaEe>true</useJakartaEe> gives me the error package jakarta.annotation does not exist. What am I doing wrong? Here is the pom snippet: pastebin.com/KGvwFauv – Jordanson
@ArthurEirich do you have the correct Jakarta dependency versions? I had a similar error, it turned out I forgot to include jakarta.annotation-api in a version which actually uses the new jakarta.annotation package name 🀦 – Lemmy
I had all the needed dependencies. My problem was the option <globalProperties<skipFormModel>false</skipFormModel></globalProperties> not being set. After I scrolled trhough the log I saw a warning that a model class wasnt generated. Googled the log message for it and found a github bug – Jordanson
@DonHo github.com/OpenAPITools/openapi-generator/issues/10415 and github.com/OpenAPITools/openapi-generator/issues/12036 are those bugs where I found out about the option – Jordanson
C
2

I use openapi-generator generate command and adding options that

--additional-properties=useSpringBoot3=true

also worked. Here is the reference: https://openapi-generator.tech/docs/generators/spring/

Corky answered 26/7, 2023 at 6:30 Comment(1)
This is virtually the same, and has the same issues as Shaki's answer from 28.11.2022: it only works for the server-side Spring generator. If you are generating code for the client-side Java generator, you need to use useJakartaEe=true as in the sashok_bg's now-accepted answer. – Alliterative

© 2022 - 2024 β€” McMap. All rights reserved.