From 0fda0dbffa19f508881dfc9bcfae1bbe44e93d35 Mon Sep 17 00:00:00 2001 From: altro3 Date: Sun, 20 Aug 2023 17:04:42 +0700 Subject: [PATCH] OpenAPI generator 6.1.0 new properties --- gradle/libs.versions.toml | 4 +- .../openapi/DefaultOpenApiExtension.java | 43 ++++- .../gradle/openapi/OpenApiClientSpec.java | 1 + .../gradle/openapi/OpenApiExtension.java | 1 + .../gradle/openapi/OpenApiServerSpec.java | 3 + .../micronaut/gradle/openapi/OpenApiSpec.java | 9 + .../tasks/AbstractOpenApiGenerator.java | 16 ++ .../tasks/AbstractOpenApiWorkAction.java | 80 +++++---- .../tasks/OpenApiClientWorkAction.java | 57 ++++-- .../openapi/tasks/OpenApiServerGenerator.java | 7 + .../tasks/OpenApiServerWorkAction.java | 37 +++- .../AbstractOpenApiGeneratorSpec.groovy | 1 + .../gradle/OpenApiClientGeneratorSpec.groovy | 160 ++++++++++++++++- .../gradle/OpenApiServerGeneratorSpec.groovy | 170 +++++++++++++++++- 14 files changed, 517 insertions(+), 72 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index aec02478d..d87e09479 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,10 +9,10 @@ spock = "2.1-groovy-3.0" oraclelinux = "9" alpine="3.16" graalvmPlugin = "0.9.28" -micronaut-platform = "4.1.2" # This is the platform version, used in our tests +micronaut-platform = "4.2.0" # This is the platform version, used in our tests micronaut-aot = "2.1.1" micronaut-testresources = "2.2.0" -micronaut-openapi = "5.1.1" +micronaut-openapi = "6.1.0" log4j2 = { require = "2.17.1", reject = ["]0, 2.17["] } jetbrains-annotations = "24.0.1" tomlj = "1.1.0" diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/DefaultOpenApiExtension.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/DefaultOpenApiExtension.java index 3e61b46a4..e1a686579 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/DefaultOpenApiExtension.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/DefaultOpenApiExtension.java @@ -15,10 +15,19 @@ */ package io.micronaut.gradle.openapi; +import java.io.File; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Consumer; + +import javax.inject.Inject; + import io.micronaut.gradle.PluginsHelper; import io.micronaut.gradle.openapi.tasks.AbstractOpenApiGenerator; import io.micronaut.gradle.openapi.tasks.OpenApiClientGenerator; import io.micronaut.gradle.openapi.tasks.OpenApiServerGenerator; + import org.gradle.api.Action; import org.gradle.api.GradleException; import org.gradle.api.Project; @@ -29,16 +38,10 @@ import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; -import javax.inject.Inject; -import java.io.File; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.function.Consumer; - import static org.codehaus.groovy.runtime.StringGroovyMethods.capitalize; public abstract class DefaultOpenApiExtension implements OpenApiExtension { + public static final String OPENAPI_GROUP = "Micronaut OpenAPI"; // We use a String here because the type is not available at runtime because of classpath isolation private static final String DEFAULT_SERIALIZATION_FRAMEWORK = "MICRONAUT_SERDE_JACKSON"; @@ -72,15 +75,18 @@ public void server(String name, Provider definition, Action { configureCommonProperties(name, task, serverSpec, definition); + task.getAot().set(serverSpec.getAot()); task.setDescription("Generates OpenAPI controllers from an OpenAPI definition"); configureServerTask(serverSpec, task); task.getOutputKinds().addAll("APIS", "SUPPORTING_FILES"); }); var models = project.getTasks().register(generateModelsTaskName(name), OpenApiServerGenerator.class, task -> { configureCommonProperties(name, task, serverSpec, definition); + task.getAot().set(serverSpec.getAot()); task.setDescription("Generates OpenAPI models from an OpenAPI definition"); configureServerTask(serverSpec, task); task.getOutputKinds().add("MODELS"); @@ -103,13 +109,27 @@ private void configureCommonExtensionDefaults(OpenApiSpec spec) { spec.getUseBeanValidation().convention(true); spec.getUseOptional().convention(false); spec.getUseReactive().convention(true); + spec.getLombok().convention(false); + spec.getGeneratedAnnotation().convention(true); + spec.getFluxForArrays().convention(false); spec.getSerializationFramework().convention(DEFAULT_SERIALIZATION_FRAMEWORK); spec.getAlwaysUseGenerateHttpResponse().convention(false); spec.getGenerateHttpResponseWhereRequired().convention(false); spec.getDateTimeFormat().convention("ZONED_DATETIME"); + spec.getLang().convention("java"); withJava(() -> { + var compileOnlyDeps = project.getConfigurations().getByName("compileOnly").getDependencies(); + if ("java".equalsIgnoreCase(spec.getLang().get())) { + compileOnlyDeps.addAllLater(spec.getLombok().map(lombok -> { + if (Boolean.TRUE.equals(lombok)) { + return List.of(project.getDependencies().create("org.projectlombok:lombok")); + } + return List.of(); + })); + } + compileOnlyDeps.add(project.getDependencies().create("io.micronaut.openapi:micronaut-openapi")); + var implDeps = project.getConfigurations().getByName("implementation").getDependencies(); - implDeps.add(project.getDependencies().create("io.micronaut.openapi:micronaut-openapi")); implDeps.addAllLater(spec.getUseReactive().map(reactive -> { if (Boolean.TRUE.equals(reactive)) { return List.of(project.getDependencies().create("io.projectreactor:reactor-core")); @@ -138,6 +158,10 @@ private void configureCommonProperties(String name, AbstractOpenApiGenerator definition, Action mainSrcDir(AbstractOpenApiGenerator t) { + private static Provider mainSrcDir(AbstractOpenApiGenerator t) { return t.getOutputDirectory().dir("src/main/java"); } @@ -213,6 +237,7 @@ private static String generateApisTaskName(String name) { private static void configureServerTask(OpenApiServerSpec serverSpec, OpenApiServerGenerator task) { task.getControllerPackage().convention(serverSpec.getControllerPackage()); task.getUseAuth().convention(serverSpec.getUseAuth()); + task.getAot().convention(serverSpec.getAot()); } } diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiClientSpec.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiClientSpec.java index e3619de56..ce495f2b8 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiClientSpec.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiClientSpec.java @@ -19,6 +19,7 @@ import org.gradle.api.provider.Property; public interface OpenApiClientSpec extends OpenApiSpec { + Property getClientId(); Property getUseAuth(); diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiExtension.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiExtension.java index 1731e0a4e..88081d393 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiExtension.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiExtension.java @@ -26,6 +26,7 @@ * Configures the OpenAPI code generator. */ public interface OpenApiExtension { + /** * The version of the Micronaut OpenAPI generator. * @return the version diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiServerSpec.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiServerSpec.java index a94d9822e..92d611111 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiServerSpec.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiServerSpec.java @@ -18,7 +18,10 @@ import org.gradle.api.provider.Property; public interface OpenApiServerSpec extends OpenApiSpec { + Property getControllerPackage(); Property getUseAuth(); + + Property getAot(); } diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiSpec.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiSpec.java index a454cb7c5..b62e5d282 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiSpec.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/OpenApiSpec.java @@ -19,6 +19,9 @@ import org.gradle.api.provider.Property; public interface OpenApiSpec { + + Property getLang(); + Property getInvokerPackageName(); Property getApiPackageName(); @@ -37,6 +40,12 @@ public interface OpenApiSpec { Property getGenerateHttpResponseWhereRequired(); + Property getLombok(); + + Property getGeneratedAnnotation(); + + Property getFluxForArrays(); + Property getDateTimeFormat(); ListProperty getParameterMappings(); diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/AbstractOpenApiGenerator.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/AbstractOpenApiGenerator.java index c9b960b88..fcd206e2d 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/AbstractOpenApiGenerator.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/AbstractOpenApiGenerator.java @@ -44,6 +44,9 @@ public abstract class AbstractOpenApiGenerator getLang(); + @Input public abstract Property getInvokerPackageName(); @@ -80,6 +83,15 @@ public abstract class AbstractOpenApiGenerator getParameterMappings(); + @Input + public abstract Property getLombok(); + + @Input + public abstract Property getGeneratedAnnotation(); + + @Input + public abstract Property getFluxForArrays(); + @Input public abstract ListProperty getResponseBodyMappings(); @@ -98,6 +110,7 @@ public abstract class AbstractOpenApiGenerator spec.getClasspath().from(getClasspath())) .submit(getWorkerAction(), params -> { + params.getLang().set(getLang()); params.getApiPackageName().set(getApiPackageName()); params.getInvokerPackageName().set(getInvokerPackageName()); params.getSerializationFramework().set(getSerializationFramework()); @@ -113,6 +126,9 @@ public final void execute() { params.getDateTimeFormat().set(getDateTimeFormat()); params.getParameterMappings().set(getParameterMappings()); params.getResponseBodyMappings().set(getResponseBodyMappings()); + params.getFluxForArrays().set(getFluxForArrays()); + params.getGeneratedAnnotation().set(getGeneratedAnnotation()); + params.getLombok().set(getLombok()); configureWorkerParameters(params); }); } diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/AbstractOpenApiWorkAction.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/AbstractOpenApiWorkAction.java index e75a789bf..b6543c28e 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/AbstractOpenApiWorkAction.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/AbstractOpenApiWorkAction.java @@ -15,13 +15,18 @@ */ package io.micronaut.gradle.openapi.tasks; +import java.util.Locale; + import io.micronaut.gradle.openapi.ParameterMappingModel; import io.micronaut.gradle.openapi.ResponseBodyMappingModel; -import io.micronaut.openapi.generator.AbstractMicronautJavaCodegen; import io.micronaut.openapi.generator.MicronautCodeGeneratorBuilder; import io.micronaut.openapi.generator.MicronautCodeGeneratorEntryPoint; import io.micronaut.openapi.generator.MicronautCodeGeneratorOptionsBuilder; +import io.micronaut.openapi.generator.MicronautCodeGeneratorOptionsBuilder.GeneratorLanguage; +import io.micronaut.openapi.generator.ParameterMapping; +import io.micronaut.openapi.generator.ResponseBodyMapping; import io.micronaut.openapi.generator.SerializationLibraryKind; + import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.provider.ListProperty; @@ -29,10 +34,12 @@ import org.gradle.workers.WorkAction; import org.gradle.workers.WorkParameters; -import java.util.Locale; - public abstract class AbstractOpenApiWorkAction implements WorkAction { + interface OpenApiParameters extends WorkParameters { + + Property getLang(); + RegularFileProperty getDefinitionFile(); Property getInvokerPackageName(); @@ -59,6 +66,12 @@ interface OpenApiParameters extends WorkParameters { Property getDateTimeFormat(); + Property getLombok(); + + Property getGeneratedAnnotation(); + + Property getFluxForArrays(); + ListProperty getParameterMappings(); ListProperty getResponseBodyMappings(); @@ -69,6 +82,7 @@ interface OpenApiParameters extends WorkParameters { @Override public void execute() { var parameters = getParameters(); + var lang = parameters.getLang().get(); var builder = MicronautCodeGeneratorEntryPoint.builder() .withDefinitionFile(parameters.getDefinitionFile().get().getAsFile().toURI()) .withOutputDirectory(parameters.getOutputDirectory().getAsFile().get()) @@ -78,36 +92,36 @@ public void execute() { .map(s -> MicronautCodeGeneratorEntryPoint.OutputKind.valueOf(s.toUpperCase(Locale.US))) .toArray(MicronautCodeGeneratorEntryPoint.OutputKind[]::new) ) - .withOptions(options -> { - options.withInvokerPackage(parameters.getInvokerPackageName().get()); - options.withApiPackage(parameters.getApiPackageName().get()); - options.withModelPackage(parameters.getModelPackageName().get()); - options.withBeanValidation(parameters.getUseBeanValidation().get()); - options.withOptional(parameters.getUseOptional().get()); - options.withReactive(parameters.getUseReactive().get()); - options.withSerializationLibrary(SerializationLibraryKind.valueOf(parameters.getSerializationFramework().get().toUpperCase(Locale.US))); - options.withGenerateHttpResponseAlways(parameters.getAlwaysUseGenerateHttpResponse().get()); - options.withGenerateHttpResponseWhereRequired(parameters.getGenerateHttpResponseWhereRequired().get()); - options.withDateTimeFormat(MicronautCodeGeneratorOptionsBuilder.DateTimeFormat.valueOf(parameters.getDateTimeFormat().get().toUpperCase(Locale.US))); - options.withParameterMappings(parameters.getParameterMappings() - .get() - .stream() - .map(mapping -> new AbstractMicronautJavaCodegen.ParameterMapping( - mapping.getName(), - AbstractMicronautJavaCodegen.ParameterMapping.ParameterLocation.valueOf(mapping.getLocation().name()), - mapping.getMappedType(), - mapping.getMappedName(), - mapping.isValidated()) - ) - .toList() - ); - options.withResponseBodyMappings(parameters.getResponseBodyMappings() - .get() - .stream() - .map(mapping -> new AbstractMicronautJavaCodegen.ResponseBodyMapping(mapping.getHeaderName(), mapping.getMappedBodyType(), mapping.isListWrapper(), mapping.isValidated())) - .toList() - ); - }); + .withOptions(options -> options.withInvokerPackage(parameters.getInvokerPackageName().get()) + .withLang("kotlin".equalsIgnoreCase(lang) ? GeneratorLanguage.KOTLIN : GeneratorLanguage.JAVA) + .withApiPackage(parameters.getApiPackageName().get()) + .withModelPackage(parameters.getModelPackageName().get()) + .withBeanValidation(parameters.getUseBeanValidation().get()) + .withOptional(parameters.getUseOptional().get()) + .withReactive(parameters.getUseReactive().get()) + .withSerializationLibrary(SerializationLibraryKind.valueOf(parameters.getSerializationFramework().get().toUpperCase(Locale.US))) + .withGenerateHttpResponseAlways(parameters.getAlwaysUseGenerateHttpResponse().get()) + .withGenerateHttpResponseWhereRequired(parameters.getGenerateHttpResponseWhereRequired().get()) + .withDateTimeFormat(MicronautCodeGeneratorOptionsBuilder.DateTimeFormat.valueOf(parameters.getDateTimeFormat().get().toUpperCase(Locale.US))) + .withParameterMappings(parameters.getParameterMappings() + .get() + .stream() + .map(mapping -> new ParameterMapping( + mapping.getName(), + ParameterMapping.ParameterLocation.valueOf(mapping.getLocation().name()), + mapping.getMappedType(), + mapping.getMappedName(), + mapping.isValidated()) + ) + .toList() + ) + .withResponseBodyMappings(parameters.getResponseBodyMappings() + .get() + .stream() + .map(mapping -> new ResponseBodyMapping(mapping.getHeaderName(), mapping.getMappedBodyType(), mapping.isListWrapper(), mapping.isValidated())) + .toList() + )); + configureBuilder(builder); builder.build().generate(); } diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiClientWorkAction.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiClientWorkAction.java index a51847fa8..932f898ca 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiClientWorkAction.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiClientWorkAction.java @@ -15,14 +15,18 @@ */ package io.micronaut.gradle.openapi.tasks; +import java.util.List; + import io.micronaut.openapi.generator.MicronautCodeGeneratorBuilder; +import io.micronaut.openapi.generator.MicronautCodeGeneratorOptionsBuilder.GeneratorLanguage; + import org.gradle.api.provider.ListProperty; import org.gradle.api.provider.Property; -import java.util.List; - public abstract class OpenApiClientWorkAction extends AbstractOpenApiWorkAction { + interface ClientParameters extends AbstractOpenApiWorkAction.OpenApiParameters { + Property getClientId(); Property getUseAuth(); @@ -34,21 +38,44 @@ interface ClientParameters extends AbstractOpenApiWorkAction.OpenApiParameters { ListProperty getAdditionalClientTypeAnnotations(); } + @Override protected void configureBuilder(MicronautCodeGeneratorBuilder builder) { var parameters = getParameters(); - builder.forClient(spec -> { - spec.withAuthorization(parameters.getUseAuth().get()); - if (parameters.getClientId().isPresent()) { - spec.withClientId(parameters.getClientId().get()); - } - spec.withAdditionalClientTypeAnnotations(parameters.getAdditionalClientTypeAnnotations().getOrElse(List.of())); - if (parameters.getBasePathSeparator().isPresent()) { - spec.withBasePathSeparator(parameters.getBasePathSeparator().get()); - } - if (parameters.getAuthorizationFilterPattern().isPresent()) { - spec.withAuthorizationFilterPattern(parameters.getAuthorizationFilterPattern().get()); - } - }); + if ("kotlin".equalsIgnoreCase(parameters.getLang().get())) { + builder.forKotlinClient(spec -> { + spec.withAuthorization(parameters.getUseAuth().get()) + .withAdditionalClientTypeAnnotations(parameters.getAdditionalClientTypeAnnotations().getOrElse(List.of())) + .withGeneratedAnnotation(parameters.getGeneratedAnnotation().get()); + + if (parameters.getClientId().isPresent()) { + spec.withClientId(parameters.getClientId().get()); + } + if (parameters.getBasePathSeparator().isPresent()) { + spec.withBasePathSeparator(parameters.getBasePathSeparator().get()); + } + if (parameters.getAuthorizationFilterPattern().isPresent()) { + spec.withAuthorizationFilterPattern(parameters.getAuthorizationFilterPattern().get()); + } + }); + } else { + builder.forJavaClient(spec -> { + spec.withAuthorization(parameters.getUseAuth().get()) + .withAdditionalClientTypeAnnotations(parameters.getAdditionalClientTypeAnnotations().getOrElse(List.of())) + .withLombok(parameters.getLombok().get()) + .withGeneratedAnnotation(parameters.getGeneratedAnnotation().get()) + .withFluxForArrays(parameters.getFluxForArrays().get()); + + if (parameters.getClientId().isPresent()) { + spec.withClientId(parameters.getClientId().get()); + } + if (parameters.getBasePathSeparator().isPresent()) { + spec.withBasePathSeparator(parameters.getBasePathSeparator().get()); + } + if (parameters.getAuthorizationFilterPattern().isPresent()) { + spec.withAuthorizationFilterPattern(parameters.getAuthorizationFilterPattern().get()); + } + }); + } } } diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiServerGenerator.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiServerGenerator.java index 43d81486b..b7e75abe5 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiServerGenerator.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiServerGenerator.java @@ -18,15 +18,21 @@ import org.gradle.api.provider.Property; import org.gradle.api.tasks.CacheableTask; import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.Optional; @CacheableTask public abstract class OpenApiServerGenerator extends AbstractOpenApiGenerator { + @Input public abstract Property getControllerPackage(); @Input public abstract Property getUseAuth(); + @Optional + @Input + public abstract Property getAot(); + @Override protected Class getWorkerAction() { return OpenApiServerWorkAction.class; @@ -36,5 +42,6 @@ protected Class getWorkerAction() { protected void configureWorkerParameters(OpenApiServerWorkAction.ServerParameters params) { params.getControllerPackage().set(getControllerPackage()); params.getUseAuth().set(getUseAuth()); + params.getAot().set(getAot()); } } diff --git a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiServerWorkAction.java b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiServerWorkAction.java index 5a25b2b23..87055bfb2 100644 --- a/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiServerWorkAction.java +++ b/openapi-plugin/src/main/java/io/micronaut/gradle/openapi/tasks/OpenApiServerWorkAction.java @@ -16,24 +16,45 @@ package io.micronaut.gradle.openapi.tasks; import io.micronaut.openapi.generator.MicronautCodeGeneratorBuilder; +import io.micronaut.openapi.generator.MicronautCodeGeneratorOptionsBuilder.GeneratorLanguage; + import org.gradle.api.provider.Property; public abstract class OpenApiServerWorkAction extends AbstractOpenApiWorkAction { + interface ServerParameters extends OpenApiParameters { + Property getControllerPackage(); Property getUseAuth(); + + Property getAot(); } + @Override protected void configureBuilder(MicronautCodeGeneratorBuilder builder) { var parameters = getParameters(); - builder.forServer(spec -> { - spec.withControllerPackage(parameters.getControllerPackage().get()); - spec.withAuthentication(parameters.getUseAuth().get()); - spec.withGenerateImplementationFiles(false); - spec.withGenerateControllerFromExamples(false); - spec.withGenerateOperationsToReturnNotImplemented(false); - }); - } + if ("kotlin".equalsIgnoreCase(parameters.getLang().get())) { + builder.forKotlinServer(spec -> spec.withControllerPackage(parameters.getControllerPackage().get()) + .withAuthentication(parameters.getUseAuth().get()) + .withAot(parameters.getAot().get()) + .withGenerateImplementationFiles(false) + .withGenerateControllerFromExamples(false) + .withGenerateOperationsToReturnNotImplemented(false) + .withGeneratedAnnotation(parameters.getGeneratedAnnotation().get()) + ); + } else { + builder.forJavaServer(spec -> spec.withControllerPackage(parameters.getControllerPackage().get()) + .withAuthentication(parameters.getUseAuth().get()) + .withAot(parameters.getAot().get()) + .withGenerateImplementationFiles(false) + .withGenerateControllerFromExamples(false) + .withGenerateOperationsToReturnNotImplemented(false) + .withGeneratedAnnotation(parameters.getGeneratedAnnotation().get()) + .withLombok(parameters.getLombok().get()) + .withFluxForArrays(parameters.getFluxForArrays().get()) + ); + } + } } diff --git a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/AbstractOpenApiGeneratorSpec.groovy b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/AbstractOpenApiGeneratorSpec.groovy index 1d8dc4660..679701ad9 100644 --- a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/AbstractOpenApiGeneratorSpec.groovy +++ b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/AbstractOpenApiGeneratorSpec.groovy @@ -3,6 +3,7 @@ package io.micronaut.openapi.gradle import io.micronaut.gradle.AbstractGradleBuildSpec class AbstractOpenApiGeneratorSpec extends AbstractGradleBuildSpec { + protected void withPetstore() { file("petstore.json").text = this.class.getResourceAsStream("/petstore.json").getText("UTF-8") } diff --git a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiClientGeneratorSpec.groovy b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiClientGeneratorSpec.groovy index 86bf5ef94..fa5d29b79 100644 --- a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiClientGeneratorSpec.groovy +++ b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiClientGeneratorSpec.groovy @@ -1,13 +1,12 @@ package io.micronaut.openapi.gradle - import org.gradle.testkit.runner.TaskOutcome class OpenApiClientGeneratorSpec extends AbstractOpenApiGeneratorSpec { - def "can generate an OpenAPI client implementation"() { + def "can generate an java OpenAPI client implementation"() { given: - settingsFile << "rootProject.name = 'openapi-server'" + settingsFile << "rootProject.name = 'openapi-client'" buildFile << """ plugins { id "io.micronaut.minimal.application" @@ -26,6 +25,9 @@ class OpenApiClientGeneratorSpec extends AbstractOpenApiGeneratorSpec { $repositoriesBlock dependencies { + + annotationProcessor "io.micronaut.serde:micronaut-serde-processor" + implementation "io.micronaut.serde:micronaut-serde-jackson" } @@ -46,4 +48,156 @@ class OpenApiClientGeneratorSpec extends AbstractOpenApiGeneratorSpec { } + def "can generate an java OpenAPI client implementation with some properties"() { + given: + settingsFile << "rootProject.name = 'openapi-client'" + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + id "io.micronaut.openapi" + } + + micronaut { + version "$micronautVersion" + openapi { + client(file("petstore.json")) { + lang = "java" + lombok = true + useReactive = true + generatedAnnotation = false + fluxForArrays = true + } + } + } + + $repositoriesBlock + + dependencies { + + annotationProcessor "org.projectlombok:lombok" + annotationProcessor "io.micronaut.serde:micronaut-serde-processor" + + compileOnly "org.projectlombok:lombok" + + implementation "io.micronaut.serde:micronaut-serde-jackson" + implementation "io.micronaut.reactor:micronaut-reactor" + } + + """ + + withPetstore() + + when: + def result = build('test') + + then: + result.task(":generateClientOpenApiApis").outcome == TaskOutcome.SUCCESS + result.task(":generateClientOpenApiModels").outcome == TaskOutcome.SUCCESS + result.task(":compileJava").outcome == TaskOutcome.SUCCESS + + and: + file("build/generated/openapi/generateClientOpenApiModels/src/main/java/io/micronaut/openapi/model/Pet.java").exists() + } + + def "can generate an kotlin OpenAPI client implementation with some properties (KAPT)"() { + given: + settingsFile << "rootProject.name = 'openapi-client'" + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + id "io.micronaut.openapi" + id "org.jetbrains.kotlin.jvm" version "1.9.20" + id "org.jetbrains.kotlin.plugin.allopen" version "1.9.20" + id "org.jetbrains.kotlin.kapt" version "1.9.20" + } + + micronaut { + version "$micronautVersion" + openapi { + client(file("petstore.json")) { + lang = "kotlin" + useReactive = true + generatedAnnotation = false + fluxForArrays = true + } + } + } + + $repositoriesBlock + + dependencies { + + kapt "io.micronaut.serde:micronaut-serde-processor" + + implementation "io.micronaut.serde:micronaut-serde-jackson" + implementation "io.micronaut.reactor:micronaut-reactor" + implementation "io.micronaut:micronaut-inject-kotlin" + } + + """ + + withPetstore() + + when: + def result = build('test') + + then: + result.task(":generateClientOpenApiApis").outcome == TaskOutcome.SUCCESS + result.task(":generateClientOpenApiModels").outcome == TaskOutcome.SUCCESS + result.task(":compileJava").outcome == TaskOutcome.SUCCESS + + and: + file("build/generated/openapi/generateClientOpenApiModels/src/main/kotlin/io/micronaut/openapi/model/Pet.kt").exists() + } + + def "can generate an kotlin OpenAPI client implementation with some properties (KSP)"() { + given: + settingsFile << "rootProject.name = 'openapi-client'" + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + id "io.micronaut.openapi" + id "org.jetbrains.kotlin.jvm" version "1.9.20" + id "org.jetbrains.kotlin.plugin.allopen" version "1.9.20" + id "com.google.devtools.ksp" version "1.9.20-1.0.14" + } + + micronaut { + version "$micronautVersion" + openapi { + client(file("petstore.json")) { + lang = "kotlin" + useReactive = true + generatedAnnotation = false + fluxForArrays = true + } + } + } + + $repositoriesBlock + + dependencies { + + ksp "io.micronaut.serde:micronaut-serde-processor" + + implementation "io.micronaut.serde:micronaut-serde-jackson" + implementation "io.micronaut.reactor:micronaut-reactor" + implementation "io.micronaut:micronaut-inject-kotlin" + } + + """ + + withPetstore() + + when: + def result = build('test') + + then: + result.task(":generateClientOpenApiApis").outcome == TaskOutcome.SUCCESS + result.task(":generateClientOpenApiModels").outcome == TaskOutcome.SUCCESS + result.task(":compileJava").outcome == TaskOutcome.SUCCESS + + and: + file("build/generated/openapi/generateClientOpenApiModels/src/main/kotlin/io/micronaut/openapi/model/Pet.kt").exists() + } } diff --git a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiServerGeneratorSpec.groovy b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiServerGeneratorSpec.groovy index a991e908d..35d0be11b 100644 --- a/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiServerGeneratorSpec.groovy +++ b/openapi-plugin/src/test/groovy/io/micronaut/openapi/gradle/OpenApiServerGeneratorSpec.groovy @@ -1,11 +1,10 @@ package io.micronaut.openapi.gradle - import org.gradle.testkit.runner.TaskOutcome class OpenApiServerGeneratorSpec extends AbstractOpenApiGeneratorSpec { - def "can generate an OpenAPI server implementation"() { + def "can generate an java OpenAPI server implementation"() { given: settingsFile << "rootProject.name = 'openapi-server'" buildFile << """ @@ -49,7 +48,174 @@ class OpenApiServerGeneratorSpec extends AbstractOpenApiGeneratorSpec { file("build/generated/openapi/generateServerOpenApiModels/src/main/java/io/micronaut/openapi/model/Pet.java").exists() file("build/classes/java/main/io/micronaut/openapi/api/PetApi.class").exists() file("build/classes/java/main/io/micronaut/openapi/model/Pet.class").exists() + } + + def "can generate an java OpenAPI server implementation with properties"() { + given: + settingsFile << "rootProject.name = 'openapi-server'" + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + id "io.micronaut.openapi" + } + + micronaut { + version "$micronautVersion" + runtime "netty" + testRuntime "junit5" + openapi { + server(file("petstore.json")) { + lang = "java" + lombok = true + useReactive = true + generatedAnnotation = false + fluxForArrays = true + aot = true + } + } + } + + $repositoriesBlock + mainClassName="example.Application" + + dependencies { + + annotationProcessor "org.projectlombok:lombok" + annotationProcessor "io.micronaut.serde:micronaut-serde-processor" + + compileOnly "org.projectlombok:lombok" + + implementation "io.micronaut.security:micronaut-security" + implementation "io.micronaut.serde:micronaut-serde-jackson" + } + """ + + withPetstore() + + when: + def result = build('test') + + then: + result.task(":generateServerOpenApiApis").outcome == TaskOutcome.SUCCESS + result.task(":generateServerOpenApiModels").outcome == TaskOutcome.SUCCESS + result.task(":compileJava").outcome == TaskOutcome.SUCCESS + + and: + file("build/generated/openapi/generateServerOpenApiApis/src/main/java/io/micronaut/openapi/api/PetApi.java").exists() + file("build/generated/openapi/generateServerOpenApiModels/src/main/java/io/micronaut/openapi/model/Pet.java").exists() + file("build/classes/java/main/io/micronaut/openapi/api/PetApi.class").exists() + file("build/classes/java/main/io/micronaut/openapi/model/Pet.class").exists() + } + + def "can generate an kotlin OpenAPI server implementation with properties (KAPT)"() { + given: + settingsFile << "rootProject.name = 'openapi-server'" + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + id "io.micronaut.openapi" + id "org.jetbrains.kotlin.jvm" version "1.9.20" + id "org.jetbrains.kotlin.plugin.allopen" version "1.9.20" + id "org.jetbrains.kotlin.kapt" version "1.9.20" + } + + micronaut { + version "$micronautVersion" + runtime "netty" + testRuntime "junit5" + openapi { + server(file("petstore.json")) { + lang = "kotlin" + useReactive = true + generatedAnnotation = false + fluxForArrays = true + aot = true + } + } + } + + $repositoriesBlock + mainClassName="example.Application" + + dependencies { + + kapt "io.micronaut.serde:micronaut-serde-processor" + + implementation "io.micronaut.security:micronaut-security" + implementation "io.micronaut.serde:micronaut-serde-jackson" + } + """ + withPetstore() + + when: + def result = build('test') + + then: + result.task(":generateServerOpenApiApis").outcome == TaskOutcome.SUCCESS + result.task(":generateServerOpenApiModels").outcome == TaskOutcome.SUCCESS + result.task(":compileJava").outcome == TaskOutcome.SUCCESS + + and: + file("build/generated/openapi/generateServerOpenApiApis/src/main/java/io/micronaut/openapi/api/PetApi.java").exists() + file("build/generated/openapi/generateServerOpenApiModels/src/main/java/io/micronaut/openapi/model/Pet.java").exists() + file("build/classes/java/main/io/micronaut/openapi/api/PetApi.class").exists() + file("build/classes/java/main/io/micronaut/openapi/model/Pet.class").exists() } + def "can generate an kotlin OpenAPI server implementation with properties (KSP)"() { + given: + settingsFile << "rootProject.name = 'openapi-server'" + buildFile << """ + plugins { + id "io.micronaut.minimal.application" + id "io.micronaut.openapi" + id "org.jetbrains.kotlin.jvm" version "1.9.20" + id "org.jetbrains.kotlin.plugin.allopen" version "1.9.20" + id "com.google.devtools.ksp" version "1.9.20-1.0.14" + } + + micronaut { + version "$micronautVersion" + runtime "netty" + testRuntime "junit5" + openapi { + server(file("petstore.json")) { + lang = "kotlin" + useReactive = true + generatedAnnotation = false + fluxForArrays = true + aot = true + } + } + } + + $repositoriesBlock + mainClassName="example.Application" + + dependencies { + + ksp "io.micronaut.serde:micronaut-serde-processor" + + implementation "io.micronaut.security:micronaut-security" + implementation "io.micronaut.serde:micronaut-serde-jackson" + } + """ + + withPetstore() + + when: + def result = build('test') + + then: + result.task(":generateServerOpenApiApis").outcome == TaskOutcome.SUCCESS + result.task(":generateServerOpenApiModels").outcome == TaskOutcome.SUCCESS + result.task(":compileJava").outcome == TaskOutcome.SUCCESS + + and: + file("build/generated/openapi/generateServerOpenApiApis/src/main/java/io/micronaut/openapi/api/PetApi.java").exists() + file("build/generated/openapi/generateServerOpenApiModels/src/main/java/io/micronaut/openapi/model/Pet.java").exists() + file("build/classes/java/main/io/micronaut/openapi/api/PetApi.class").exists() + file("build/classes/java/main/io/micronaut/openapi/model/Pet.class").exists() + } }