From cdf41067de62962a7b964373f526e54a0e7a5d84 Mon Sep 17 00:00:00 2001 From: altro3 Date: Sun, 20 Aug 2023 17:04:42 +0700 Subject: [PATCH] OpenAPI generator new properties --- .../openapi/DefaultOpenApiExtension.java | 7 +- .../gradle/openapi/OpenApiClientSpec.java | 1 + .../gradle/openapi/OpenApiExtension.java | 1 + .../gradle/openapi/OpenApiServerSpec.java | 3 + .../micronaut/gradle/openapi/OpenApiSpec.java | 7 ++ .../tasks/AbstractOpenApiGenerator.java | 9 +++ .../tasks/AbstractOpenApiWorkAction.java | 69 +++++++++++-------- .../tasks/OpenApiClientWorkAction.java | 7 +- .../openapi/tasks/OpenApiServerGenerator.java | 7 ++ .../tasks/OpenApiServerWorkAction.java | 19 ++--- .../AbstractOpenApiGeneratorSpec.groovy | 1 + .../gradle/OpenApiClientGeneratorSpec.groovy | 1 - .../gradle/OpenApiServerGeneratorSpec.groovy | 1 - 13 files changed, 90 insertions(+), 43 deletions(-) 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..b22ee38ee 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 @@ -39,6 +39,7 @@ 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"; @@ -48,7 +49,7 @@ public abstract class DefaultOpenApiExtension implements OpenApiExtension { private final Configuration classpath; @Inject - public DefaultOpenApiExtension(Project project, Configuration classpath) { + protected DefaultOpenApiExtension(Project project, Configuration classpath) { this.project = project; this.classpath = classpath; } @@ -138,6 +139,9 @@ private void configureCommonProperties(String name, AbstractOpenApiGenerator 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..2ab7c1b6a 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,7 @@ import org.gradle.api.provider.Property; public interface OpenApiSpec { + Property getInvokerPackageName(); Property getApiPackageName(); @@ -37,6 +38,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..cafe6879b 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 @@ -80,6 +80,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(); 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..137e2fc31 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 @@ -32,7 +32,9 @@ import java.util.Locale; public abstract class AbstractOpenApiWorkAction implements WorkAction { + interface OpenApiParameters extends WorkParameters { + RegularFileProperty getDefinitionFile(); Property getInvokerPackageName(); @@ -59,6 +61,12 @@ interface OpenApiParameters extends WorkParameters { Property getDateTimeFormat(); + Property getLombok(); + + Property getGeneratedAnnotation(); + + Property getFluxForArrays(); + ListProperty getParameterMappings(); ListProperty getResponseBodyMappings(); @@ -78,36 +86,37 @@ 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()) + .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))) + .withLombok(parameters.getLombok().get()) + .withGeneratedAnnotation(parameters.getGeneratedAnnotation().get()) + .withFluxForArrays(parameters.getFluxForArrays().get()) + .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() + ) + .withResponseBodyMappings(parameters.getResponseBodyMappings() + .get() + .stream() + .map(mapping -> new AbstractMicronautJavaCodegen.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..524dd9641 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 @@ -22,7 +22,9 @@ import java.util.List; public abstract class OpenApiClientWorkAction extends AbstractOpenApiWorkAction { + interface ClientParameters extends AbstractOpenApiWorkAction.OpenApiParameters { + Property getClientId(); Property getUseAuth(); @@ -34,14 +36,15 @@ interface ClientParameters extends AbstractOpenApiWorkAction.OpenApiParameters { ListProperty getAdditionalClientTypeAnnotations(); } + @Override protected void configureBuilder(MicronautCodeGeneratorBuilder builder) { var parameters = getParameters(); builder.forClient(spec -> { - spec.withAuthorization(parameters.getUseAuth().get()); + spec.withAuthorization(parameters.getUseAuth().get()) + .withAdditionalClientTypeAnnotations(parameters.getAdditionalClientTypeAnnotations().getOrElse(List.of())); 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()); } 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..8edcf199a 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 @@ -19,21 +19,24 @@ 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); - }); + builder.forServer(spec -> spec.withControllerPackage(parameters.getControllerPackage().get()) + .withAuthentication(parameters.getUseAuth().get()) + .withAot(parameters.getAot().get()) + .withGenerateImplementationFiles(false) + .withGenerateControllerFromExamples(false) + .withGenerateOperationsToReturnNotImplemented(false)); } - } 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..584fc57ff 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,6 +1,5 @@ package io.micronaut.openapi.gradle - import org.gradle.testkit.runner.TaskOutcome class OpenApiClientGeneratorSpec extends AbstractOpenApiGeneratorSpec { 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..5840b75a1 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,6 +1,5 @@ package io.micronaut.openapi.gradle - import org.gradle.testkit.runner.TaskOutcome class OpenApiServerGeneratorSpec extends AbstractOpenApiGeneratorSpec {