From 25988296d65627924ef4aa970573404c8bb4dae2 Mon Sep 17 00:00:00 2001 From: altro3 Date: Sun, 22 Sep 2024 15:40:16 +0700 Subject: [PATCH] Added support useJakartaEe flag --- .../generator/AbstractMicronautJavaCodegen.java | 9 ++++++--- .../generator/AbstractMicronautKotlinCodegen.java | 10 +++++++--- .../generator/MicronautCodeGeneratorEntryPoint.java | 11 +++++++++++ .../MicronautCodeGeneratorOptionsBuilder.java | 9 +++++++++ .../client/auth/AuthorizationBinder.mustache | 2 +- .../client/test/api_test.groovy.mustache | 2 +- .../java-micronaut/client/test/api_test.mustache | 2 +- .../common/test/model_test.groovy.mustache | 2 +- .../server/test/controller_test.groovy.mustache | 2 +- .../server/test/controller_test.mustache | 2 +- .../client/auth/AuthorizationBinder.mustache | 2 +- .../server/test/controller_test.mustache | 2 +- 12 files changed, 41 insertions(+), 14 deletions(-) diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java index 28803a499f..8c9c20734b 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java @@ -496,6 +496,12 @@ public void processOpts() { } writePropertyBack(OPT_GENERATE_OPERATION_ONLY_FOR_FIRST_TAG, generateOperationOnlyForFirstTag); + if (additionalProperties.containsKey(USE_JAKARTA_EE)) { + setUseJakartaEe(convertPropertyToBoolean(USE_JAKARTA_EE)); + } + writePropertyBack(USE_JAKARTA_EE, useJakartaEe); + writePropertyBack(JAVAX_PACKAGE, useJakartaEe ? "jakarta" : "javax"); + maybeSetTestTool(); writePropertyBack(OPT_TEST, testTool); if (testTool.equals(OPT_TEST_JUNIT)) { @@ -524,9 +530,6 @@ public void processOpts() { supportingFiles.add(new SupportingFile("common/configuration/application.yml.mustache", resourceFolder, "application.yml").doNotOverwrite()); supportingFiles.add(new SupportingFile("common/configuration/logback.xml.mustache", resourceFolder, "logback.xml").doNotOverwrite()); - // Use jakarta instead of javax - additionalProperties.put("javaxPackage", "jakarta"); - // Use the default java time switch (dateLibrary) { case OPT_DATE_LIBRARY_OFFSET_DATETIME -> { diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java index ec5312da13..f6a0226624 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java @@ -217,6 +217,7 @@ protected AbstractMicronautKotlinCodegen() { // CHECKSTYLE:OFF // Set all the fields + useJakartaEe = true; useBeanValidation = true; visitable = false; hideGenerationTimestamp = false; @@ -557,6 +558,12 @@ public void processOpts() { } writePropertyBack(OPT_GENERATE_OPERATION_ONLY_FOR_FIRST_TAG, generateOperationOnlyForFirstTag); + if (additionalProperties.containsKey(USE_JAKARTA_EE)) { + setUseJakartaEe(convertPropertyToBoolean(USE_JAKARTA_EE)); + } + writePropertyBack(USE_JAKARTA_EE, useJakartaEe); + writePropertyBack(JAVAX_PACKAGE, useJakartaEe ? "jakarta" : "javax"); + maybeSetTestTool(); writePropertyBack(OPT_TEST, testTool); if (testTool.equals(OPT_TEST_JUNIT)) { @@ -583,9 +590,6 @@ public void processOpts() { supportingFiles.add(new SupportingFile("common/configuration/application.yml.mustache", resourcesFolder, "application.yml").doNotOverwrite()); supportingFiles.add(new SupportingFile("common/configuration/logback.xml.mustache", resourcesFolder, "logback.xml").doNotOverwrite()); - // Use jakarta instead of javax - additionalProperties.put("javaxPackage", "jakarta"); - // Use the default java time switch (dateLibrary) { case OPT_DATE_LIBRARY_OFFSET_DATETIME -> { diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java index a1f3113fd3..b3a5443553 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java @@ -187,6 +187,7 @@ private void configureOptions() { javaCodeGen.setImplicitHeadersRegex(options.implicitHeadersRegex); } + javaCodeGen.setUseJakartaEe(options.useJakartaEe); javaCodeGen.setUseOneOfInterfaces(options.useOneOfInterfaces); javaCodeGen.setReactive(options.reactive); javaCodeGen.setGenerateHttpResponseAlways(options.generateHttpResponseAlways); @@ -257,6 +258,7 @@ private void configureOptions() { if (options.implicitHeadersRegex != null && !options.implicitHeadersRegex.isBlank()) { kotlinCodeGen.setImplicitHeadersRegex(options.implicitHeadersRegex); } + kotlinCodeGen.setUseJakartaEe(options.useJakartaEe); kotlinCodeGen.setUseOneOfInterfaces(options.useOneOfInterfaces); kotlinCodeGen.setReactive(options.reactive); kotlinCodeGen.setGenerateHttpResponseAlways(options.generateHttpResponseAlways); @@ -505,6 +507,7 @@ private static class DefaultOptionsBuilder implements MicronautCodeGeneratorOpti private String apiPackage; private String artifactId; + private boolean useJakartaEe = true; private boolean beanValidation = true; private boolean useEnumCaseInsensitive; private String invokerPackage; @@ -738,6 +741,12 @@ public MicronautCodeGeneratorOptionsBuilder withUseOneOfInterfaces(boolean useOn return this; } + @Override + public MicronautCodeGeneratorOptionsBuilder withUseJakartaEe(boolean useJakartaEe) { + this.useJakartaEe = useJakartaEe; + return this; + } + private Options build() { return new Options( lang, @@ -745,6 +754,7 @@ private Options build() { modelPackage, invokerPackage, artifactId, + useJakartaEe, parameterMappings, responseBodyMappings, schemaMapping, @@ -802,6 +812,7 @@ private record Options( String modelPackage, String invokerPackage, String artifactId, + boolean useJakartaEe, List parameterMappings, List responseBodyMappings, Map schemaMapping, diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java index 2532a7a287..9a123eecec 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java @@ -289,6 +289,15 @@ public interface MicronautCodeGeneratorOptionsBuilder { */ MicronautCodeGeneratorOptionsBuilder withUseOneOfInterfaces(boolean useOneOfInterfaces); + /** + * Flag to indicate whether to use the "jakarta" or "javax" package. + * + * @param useJakartaEe if true, then use the "jakarta" package, otherwise - "javax". + * + * @return this builder + */ + MicronautCodeGeneratorOptionsBuilder withUseJakartaEe(boolean useJakartaEe); + /** * The possible date-time formatting configurations. */ diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/AuthorizationBinder.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/AuthorizationBinder.mustache index f7bae38a86..1cac872f92 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/AuthorizationBinder.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/AuthorizationBinder.mustache @@ -10,7 +10,7 @@ import io.micronaut.core.util.StringUtils; import io.micronaut.http.MutableHttpRequest; import io.micronaut.http.client.bind.AnnotatedClientRequestBinder; import io.micronaut.http.client.bind.ClientRequestUriContext; -import jakarta.inject.Singleton; +import {{javaxPackage}}.inject.Singleton; import java.util.ArrayList; import java.util.List; {{#generatedAnnotation}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.groovy.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.groovy.mustache index b1ff42e96d..2014c0fbf9 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.groovy.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.groovy.mustache @@ -4,7 +4,7 @@ package {{package}} {{/imports}} import io.micronaut.test.extensions.spock.annotation.MicronautTest import spock.lang.Specification -import jakarta.inject.Inject +import {{javaxPackage}}.inject.Inject import spock.lang.Ignore {{#wrapInHttpResponse}} import io.micronaut.http.HttpResponse diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.mustache index 2ab1a9f787..586ab3769f 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.mustache @@ -6,7 +6,7 @@ import io.micronaut.test.extensions.junit5.annotation.MicronautTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; -import jakarta.inject.Inject; +import {{javaxPackage}}.inject.Inject; {{#wrapInHttpResponse}} import io.micronaut.http.HttpResponse; {{/wrapInHttpResponse}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/common/test/model_test.groovy.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/common/test/model_test.groovy.mustache index f45e6c11e9..a050de9b81 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/common/test/model_test.groovy.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/common/test/model_test.groovy.mustache @@ -4,7 +4,7 @@ package {{package}} {{/imports}} import io.micronaut.test.extensions.spock.annotation.MicronautTest import spock.lang.Specification -import jakarta.inject.Inject +import {{javaxPackage}}.inject.Inject /** * Model tests for {{classname}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.groovy.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.groovy.mustache index d8a72f960f..a019363fe2 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.groovy.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.groovy.mustache @@ -16,7 +16,7 @@ import io.micronaut.http.uri.UriTemplate import io.micronaut.http.cookie.Cookie import io.micronaut.http.client.multipart.MultipartBody import io.micronaut.core.type.Argument -import jakarta.inject.Inject +import {{javaxPackage}}.inject.Inject import spock.lang.Specification import spock.lang.Ignore {{#reactive}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.mustache index 84e1c76912..e7718c8c75 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.mustache @@ -19,7 +19,7 @@ import io.micronaut.core.type.GenericArgument; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Assertions; -import jakarta.inject.Inject; +import {{javaxPackage}}.inject.Inject; {{#reactive}} import reactor.core.publisher.Mono; import reactor.core.publisher.Flux; diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/auth/AuthorizationBinder.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/auth/AuthorizationBinder.mustache index 03e66a2f14..70ffa0abdb 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/auth/AuthorizationBinder.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/auth/AuthorizationBinder.mustache @@ -8,7 +8,7 @@ import io.micronaut.core.naming.NameUtils import io.micronaut.http.MutableHttpRequest import io.micronaut.http.client.bind.AnnotatedClientRequestBinder import io.micronaut.http.client.bind.ClientRequestUriContext -import jakarta.inject.Singleton +import {{javaxPackage}}.inject.Singleton {{#generatedAnnotation}} import {{javaxPackage}}.annotation.Generated {{/generatedAnnotation}} diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/server/test/controller_test.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/server/test/controller_test.mustache index d83bb0e6a2..7d22a14f4e 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/server/test/controller_test.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/server/test/controller_test.mustache @@ -19,7 +19,7 @@ import io.micronaut.core.type.Argument import org.junit.jupiter.api.Test import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Assertions -import jakarta.inject.Inject +import {{javaxPackage}}.inject.Inject {{#reactive}} import reactor.core.publisher.Flux {{/reactive}}