From db699127a0789c2187fb4282a57deb3636748d45 Mon Sep 17 00:00:00 2001 From: altro3 Date: Fri, 22 Sep 2023 14:28:24 +0700 Subject: [PATCH] Add ability to genearate Flux instead Mono> for arrays. (#1213) Fixed bugs in generated tests. Removed duplicates model_test templates. Fixed #1200 --- ...ternal.openapi-generator-test-suite.gradle | 2 +- .../AbstractMicronautJavaCodegen.java | 31 ++++++++++--- .../generator/JavaMicronautClientCodegen.java | 9 ++++ .../JavaMicronautClientOptionsBuilder.java | 8 ++++ .../generator/JavaMicronautServerCodegen.java | 9 ++++ .../JavaMicronautServerOptionsBuilder.java | 8 ++++ .../MicronautCodeGeneratorEntryPoint.java | 2 + .../java-micronaut/client/api.mustache | 2 + .../client/auth/Authorization.mustache | 2 + .../client/auth/AuthorizationBinder.mustache | 2 + .../client/auth/AuthorizationFilter.mustache | 4 +- .../client/auth/Authorizations.mustache | 5 ++- .../ApiKeyAuthConfiguration.mustache | 2 + .../ConfigurableAuthorization.mustache | 2 + .../HttpBasicAuthConfiguration.mustache | 2 + .../client/doc/api_doc.mustache | 3 +- .../client/test/api_test.groovy.mustache | 2 +- .../client/test/model_test.groovy.mustache | 41 ------------------ .../client/test/model_test.mustache | 43 ------------------- .../java-micronaut/common/model/pojo.mustache | 12 +++--- .../common/test/model_test.groovy.mustache | 3 +- .../server/controller-implementation.mustache | 1 + .../server/controller-interface.mustache | 3 ++ .../server/controllerOperationBody.mustache | 12 +++--- .../test/controller_test.groovy.mustache | 1 + .../server/test/controller_test.mustache | 8 ++-- .../generator/MicronautServerCodegenTest.java | 16 +++++++ test-suite-client-generator/build.gradle | 2 + test-suite-server-generator/build.gradle | 2 +- .../test/api/RequestBodyController.java | 1 - 30 files changed, 127 insertions(+), 113 deletions(-) delete mode 100644 openapi-generator/src/main/resources/templates/java-micronaut/client/test/model_test.groovy.mustache delete mode 100644 openapi-generator/src/main/resources/templates/java-micronaut/client/test/model_test.mustache diff --git a/build-logic/src/main/groovy/io.micronaut.build.internal.openapi-generator-test-suite.gradle b/build-logic/src/main/groovy/io.micronaut.build.internal.openapi-generator-test-suite.gradle index 45768bc435..b3f324a399 100644 --- a/build-logic/src/main/groovy/io.micronaut.build.internal.openapi-generator-test-suite.gradle +++ b/build-logic/src/main/groovy/io.micronaut.build.internal.openapi-generator-test-suite.gradle @@ -24,7 +24,7 @@ def openapiGenerate = tasks.register("generateOpenApi", OpenApiGeneratorTask) { openApiDefinition.convention(layout.projectDirectory.file("petstore.json")) outputDirectory.convention(layout.buildDirectory.dir("generated/openapi")) generatorKind.convention("client") - outputKinds.convention(["models", "apis", "supportingFiles", "modelTests", "apiTests"]) + outputKinds.convention(["models", "apis", "apiDocs", "modelDocs", "supportingFiles", "modelTests", "apiTests"]) parameterMappings.convention([]) responseBodyMappings.convention([]) } 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 e2d3c7c214..f22f5005cb 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 @@ -76,6 +76,7 @@ public abstract class AbstractMicronautJavaCodegen instead Mono> for arrays in generated code", fluxForArrays)); cliOptions.add(CliOption.newBoolean(OPT_GENERATED_ANNOTATION, "Generate code with \"@Generated\" annotation", generatedAnnotation)); cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations", useBeanValidation)); cliOptions.add(CliOption.newBoolean(USE_OPTIONAL, "Use Optional container for optional parameters", useOptional)); @@ -275,6 +281,10 @@ public void setLombok(boolean lombok) { this.lombok = lombok; } + public void setFluxForArrays(boolean fluxForArrays) { + this.fluxForArrays = fluxForArrays; + } + public void setGeneratedAnnotation(boolean generatedAnnotation) { this.generatedAnnotation = generatedAnnotation; } @@ -311,6 +321,11 @@ public void processOpts() { } writePropertyBack(OPT_USE_LOMBOK, lombok); + if (additionalProperties.containsKey(OPT_FLUX_FOR_ARRAYS)) { + fluxForArrays = convertPropertyToBoolean(OPT_FLUX_FOR_ARRAYS); + } + writePropertyBack(OPT_FLUX_FOR_ARRAYS, fluxForArrays); + if (additionalProperties.containsKey(OPT_GENERATED_ANNOTATION)) { generatedAnnotation = convertPropertyToBoolean(OPT_GENERATED_ANNOTATION); } @@ -751,8 +766,7 @@ private void processWithResponseBodyMapping(CodegenOperation op) { } if (bodyMapping != null) { - wrapOperationReturnType(op, bodyMapping.mappedBodyType, - bodyMapping.isValidated, bodyMapping.isListWrapper); + wrapOperationReturnType(op, bodyMapping.mappedBodyType, bodyMapping.isValidated, bodyMapping.isListWrapper); } } @@ -771,11 +785,17 @@ private void wrapOperationReturnType(CodegenOperation op, String wrapperType, bo String typeName = makeSureImported(wrapperType, op.imports); - if (isListWrapper && op.isArray && op.returnProperty.items != null) { + String originalReturnType; + if ((isListWrapper || fluxForArrays) && op.isArray && op.returnProperty.items != null) { + if (fluxForArrays && wrapperType.equals(MONO_CLASS_NAME)) { + typeName = makeSureImported(FLUX_CLASS_NAME, op.imports); + op.vendorExtensions.put("isReturnFlux", true); + } + originalReturnType = op.returnBaseType; newReturnType.dataType = typeName + '<' + op.returnBaseType + '>'; newReturnType.items = op.returnProperty.items; } else { - String originalReturnType = op.returnType; + originalReturnType = op.returnType; if (originalReturnType == null) { originalReturnType = "Void"; op.returnProperty = new CodegenProperty(); @@ -784,6 +804,7 @@ private void wrapOperationReturnType(CodegenOperation op, String wrapperType, bo newReturnType.dataType = typeName + '<' + originalReturnType + '>'; newReturnType.items = op.returnProperty; } + op.vendorExtensions.put("originalReturnType", originalReturnType); op.returnType = newReturnType.dataType; op.returnContainer = null; @@ -802,7 +823,7 @@ private void processOperationWithResponseWrappers(CodegenOperation op) { } if (reactive) { - wrapOperationReturnType(op, "reactor.core.publisher.Mono", false, false); + wrapOperationReturnType(op, MONO_CLASS_NAME, false, false); } } diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientCodegen.java index d53f299f51..6c521ae64b 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientCodegen.java @@ -202,6 +202,7 @@ static class DefaultClientOptionsBuilder implements JavaMicronautClientOptionsBu private String clientId; private boolean useAuth; private boolean lombok; + private boolean fluxForArrays; private boolean generatedAnnotation = true; @Override @@ -240,6 +241,12 @@ public JavaMicronautClientOptionsBuilder withLombok(boolean lombok) { return this; } + @Override + public JavaMicronautClientOptionsBuilder withFluxForArrays(boolean fluxForArrays) { + this.fluxForArrays = fluxForArrays; + return this; + } + @Override public JavaMicronautClientOptionsBuilder withGeneratedAnnotation(boolean generatedAnnotation) { this.generatedAnnotation = generatedAnnotation; @@ -254,6 +261,7 @@ ClientOptions build() { clientId, useAuth, lombok, + fluxForArrays, generatedAnnotation ); } @@ -266,6 +274,7 @@ record ClientOptions( String clientId, boolean useAuth, boolean lombok, + boolean fluxForArrays, boolean generatedAnnotation ) { } diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientOptionsBuilder.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientOptionsBuilder.java index 10f197b131..562a8b1171 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientOptionsBuilder.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautClientOptionsBuilder.java @@ -71,6 +71,14 @@ public interface JavaMicronautClientOptionsBuilder extends GeneratorOptionsBuild */ JavaMicronautClientOptionsBuilder withLombok(boolean lombok); + /** + * If set to true, generated code will be with Flux{@literal <}?> instead Mono{@literal <}List{@literal <}?>>. + * + * @param fluxForArrays generate code with Flux{@literal <}?> instead Mono{@literal <}List{@literal <}?>> or not + * @return this builder + */ + JavaMicronautClientOptionsBuilder withFluxForArrays(boolean fluxForArrays); + /** * If set to true, generated code will be with jakarta.annotation.Generated annotation. * diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautServerCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautServerCodegen.java index 5e1ca97cd5..dda36cb6f3 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautServerCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautServerCodegen.java @@ -317,6 +317,7 @@ static class DefaultServerOptionsBuilder implements JavaMicronautServerOptionsBu private boolean generateOperationsToReturnNotImplemented = true; private boolean useAuth = true; private boolean lombok; + private boolean fluxForArrays; private boolean generatedAnnotation = true; private boolean aot; @@ -356,6 +357,12 @@ public JavaMicronautServerOptionsBuilder withLombok(boolean lombok) { return this; } + @Override + public JavaMicronautServerOptionsBuilder withFluxForArrays(boolean fluxForArrays) { + this.fluxForArrays = fluxForArrays; + return this; + } + @Override public JavaMicronautServerOptionsBuilder withGeneratedAnnotation(boolean generatedAnnotation) { this.generatedAnnotation = generatedAnnotation; @@ -376,6 +383,7 @@ ServerOptions build() { generateControllerFromExamples, useAuth, lombok, + fluxForArrays, generatedAnnotation, aot ); @@ -389,6 +397,7 @@ record ServerOptions( boolean generateControllerFromExamples, boolean useAuth, boolean lombok, + boolean fluxForArrays, boolean generatedAnnotation, boolean aot ) { diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautServerOptionsBuilder.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautServerOptionsBuilder.java index 757982912e..83bdb0a1e8 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautServerOptionsBuilder.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/JavaMicronautServerOptionsBuilder.java @@ -69,6 +69,14 @@ public interface JavaMicronautServerOptionsBuilder extends GeneratorOptionsBuild */ JavaMicronautServerOptionsBuilder withLombok(boolean lombok); + /** + * If set to true, generated code will be with Flux{@literal <}?> instead Mono{@literal <}List{@literal <}?>>. + * + * @param fluxForArrays generate code with Flux{@literal <}?> instead Mono{@literal <}List{@literal <}?>> or not + * @return this builder + */ + JavaMicronautServerOptionsBuilder withFluxForArrays(boolean fluxForArrays); + /** * If set to true, generated code will be with jakarta.annotation.Generated annotation. * 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 bcc37be1f8..436b5ed68c 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 @@ -148,6 +148,7 @@ private void configureServerOptions() { serverCodegen.setGenerateControllerFromExamples(serverOptions.generateControllerFromExamples()); serverCodegen.setUseAuth(serverOptions.useAuth()); serverCodegen.setLombok(serverOptions.lombok()); + serverCodegen.setFluxForArrays(serverOptions.fluxForArrays()); serverCodegen.setGeneratedAnnotation(serverOptions.generatedAnnotation()); } } @@ -168,6 +169,7 @@ public void configureClientOptions() { } clientCodegen.setConfigureAuthorization(clientOptions.useAuth()); clientCodegen.setLombok(clientOptions.lombok()); + clientCodegen.setFluxForArrays(clientOptions.fluxForArrays()); clientCodegen.setGeneratedAnnotation(clientOptions.generatedAnnotation()); } } diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/api.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/api.mustache index 2f5b296289..bfa59fbf18 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/api.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/api.mustache @@ -17,7 +17,9 @@ import io.micronaut.http.HttpResponse; {{#imports}} import {{import}}; {{/imports}} +{{#withGeneratedAnnotation}} import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/Authorization.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/Authorization.mustache index 8b86bbc0e6..8ff35dedc2 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/Authorization.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/Authorization.mustache @@ -11,7 +11,9 @@ import java.lang.annotation.Target; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; +{{#withGeneratedAnnotation}} import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} {{#withGeneratedAnnotation}} {{>common/generatedAnnotation}} 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 a02c4533f3..754cb78f10 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 @@ -13,7 +13,9 @@ import io.micronaut.http.client.bind.ClientRequestUriContext; import jakarta.inject.Singleton; import java.util.ArrayList; import java.util.List; +{{#withGeneratedAnnotation}} import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} {{#withGeneratedAnnotation}} {{>common/generatedAnnotation}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/AuthorizationFilter.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/AuthorizationFilter.mustache index c9744a2606..ac0e002e94 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/AuthorizationFilter.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/AuthorizationFilter.mustache @@ -37,7 +37,9 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; +{{#withGeneratedAnnotation}} import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} {{#withGeneratedAnnotation}} {{>common/generatedAnnotation}} @@ -50,8 +52,8 @@ public class AuthorizationFilter implements HttpClientFilter { {{^lombok}} private static final Logger log = LoggerFactory.getLogger(ClientCredentialsHttpClientFilter.class); - {{/lombok}} + private final BeanContext beanContext; private final Map clientConfigurationByName; diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/Authorizations.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/Authorizations.mustache index f81c908007..9c59dbf382 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/Authorizations.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/Authorizations.mustache @@ -6,9 +6,12 @@ import io.micronaut.core.bind.annotation.Bindable; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; +{{#withGeneratedAnnotation}} +import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} + import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; -import {{javaxPackage}}.annotation.Generated; {{#withGeneratedAnnotation}} {{>common/generatedAnnotation}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/ApiKeyAuthConfiguration.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/ApiKeyAuthConfiguration.mustache index 2a6ee7e3b0..8144fd5a5d 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/ApiKeyAuthConfiguration.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/ApiKeyAuthConfiguration.mustache @@ -7,7 +7,9 @@ import io.micronaut.context.annotation.Parameter; import io.micronaut.core.annotation.NonNull; import io.micronaut.http.MutableHttpRequest; import io.micronaut.http.cookie.Cookie; +{{#withGeneratedAnnotation}} import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} {{#lombok}} import lombok.Getter; import lombok.Setter; diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/ConfigurableAuthorization.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/ConfigurableAuthorization.mustache index 912f52d763..dc65a84102 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/ConfigurableAuthorization.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/ConfigurableAuthorization.mustache @@ -3,7 +3,9 @@ package {{invokerPackage}}.auth.configuration; import io.micronaut.core.annotation.NonNull; import io.micronaut.http.MutableHttpRequest; +{{#withGeneratedAnnotation}} import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} {{#withGeneratedAnnotation}} {{>common/generatedAnnotation}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/HttpBasicAuthConfiguration.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/HttpBasicAuthConfiguration.mustache index f582f41db8..c0bdc0c510 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/HttpBasicAuthConfiguration.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/auth/configuration/HttpBasicAuthConfiguration.mustache @@ -6,7 +6,9 @@ import io.micronaut.context.annotation.EachProperty; import io.micronaut.context.annotation.Parameter; import io.micronaut.core.annotation.NonNull; import io.micronaut.http.MutableHttpRequest; +{{#withGeneratedAnnotation}} import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} {{#lombok}} import lombok.Getter; import lombok.Setter; diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/doc/api_doc.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/doc/api_doc.mustache index d0cb484d5d..932e8c1f1f 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/doc/api_doc.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/doc/api_doc.mustache @@ -26,6 +26,7 @@ Or the `@Inject` annotation: ```java @Singleton class MyClass { + @Inject {{classname}} {{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}; @@ -41,7 +42,7 @@ More information can be found inside [Inversion of Control guide section](https: # **{{operationId}}** ```java -{{#returnType}}Mono<{{{returnType}}}>{{/returnType}}{{^returnType}}Mono{{/returnType}} {{classname}}.{{nickname}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) +{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Mono{{/returnType}} {{classname}}.{{nickname}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) ``` {{summary}}{{#notes}} 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 a819c1fd48..b1ff42e96d 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 @@ -45,7 +45,7 @@ class {{classname}}Spec extends Specification { {{{returnType}}} body = response.body(){{/returnType}} {{/wrapInHttpResponse}} {{^wrapInHttpResponse}} - {{#returnType}}{{{returnType}}} body = {{/returnType}}api.{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}){{#reactive}}.block(){{/reactive}} + {{#returnType}}{{#reactive}}{{{vendorExtensions.originalReturnType}}}{{/reactive}}{{^reactive}}{{{returnType}}}{{/reactive}} responseBody = {{/returnType}}api.{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}){{#reactive}}{{#vendorExtensions.isReturnFlux}}.blockLast(){{/vendorExtensions.isReturnFlux}}{{^vendorExtensions.isReturnFlux}}.block(){{/vendorExtensions.isReturnFlux}}{{/reactive}} {{/wrapInHttpResponse}} then: diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/model_test.groovy.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/model_test.groovy.mustache deleted file mode 100644 index db55f23e81..0000000000 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/model_test.groovy.mustache +++ /dev/null @@ -1,41 +0,0 @@ -package {{package}} - -{{#imports}}import {{import}} -{{/imports}} -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import spock.lang.Specification -import jakarta.inject.Inject - -/** - * Model tests for {{classname}} - */ -@MicronautTest -public class {{classname}}Spec extends Specification { - - {{#models}} - {{#model}} - {{^vendorExtensions.x-is-one-of-interface}} - {{^isEnum}} - private final {{classname}} model = new {{classname}}() - - {{/isEnum}} - /** - * Model tests for {{classname}} - */ - void '{{classname}} test'() { - // TODO: test {{classname}} - } - - {{#allVars}} - /** - * Test the property '{{name}}' - */ - void '{{classname}} property {{name}} test'() { - // TODO: test {{name}} - } - - {{/allVars}} - {{/vendorExtensions.x-is-one-of-interface}} - {{/model}} - {{/models}} -} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/model_test.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/model_test.mustache deleted file mode 100644 index 915500ff69..0000000000 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/model_test.mustache +++ /dev/null @@ -1,43 +0,0 @@ -package {{package}}; - -{{#imports}}import {{import}}; -{{/imports}} -import io.micronaut.test.extensions.junit5.annotation.MicronautTest; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Assertions; - -/** - * Model tests for {{classname}} - */ -@MicronautTest -class {{classname}}Test { - - {{#models}} - {{#model}} - {{^vendorExtensions.x-is-one-of-interface}} - {{^isEnum}} - private final {{classname}} model = new {{classname}}(); - - {{/isEnum}} - /** - * Model tests for {{classname}} - */ - @Test - void test{{classname}}() { - // TODO: test {{classname}} - } - - {{#allVars}} - /** - * Test the property '{{name}}' - */ - @Test - void {{name}}Test() { - // TODO: test {{name}} - } - - {{/allVars}} - {{/vendorExtensions.x-is-one-of-interface}} - {{/model}} - {{/models}} -} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache index abe6116970..443abb6914 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache @@ -291,22 +291,22 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE */ public {{classname}} put{{nameInCamelCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) { {{#vendorExtensions.x-is-jackson-optional-nullable}} - if (this.{{name}} == null || !this.{{name}}.isPresent()) { - this.{{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}}); + if ({{name}} == null || !{{name}}.isPresent()) { + {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}}); } try { - this.{{name}}.get().put(key, {{name}}Item); + {{name}}.get().put(key, {{name}}Item); } catch (java.util.NoSuchElementException e) { // this can never happen, as we make sure above that the value is present } {{/vendorExtensions.x-is-jackson-optional-nullable}} {{^vendorExtensions.x-is-jackson-optional-nullable}} {{^required}} - if (this.{{name}} == null) { - this.{{name}} = {{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}}; + if ({{name}} == null) { + {{name}} = {{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}}; } {{/required}} - this.{{name}}.put(key, {{name}}Item); + {{name}}.put(key, {{name}}Item); {{/vendorExtensions.x-is-jackson-optional-nullable}} return this; } 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 7a7a80ad71..f45e6c11e9 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 @@ -10,7 +10,8 @@ import jakarta.inject.Inject * Model tests for {{classname}} */ @MicronautTest -public class {{classname}}Spec extends Specification { +class {{classname}}Spec extends Specification { + {{#models}} {{#model}} {{^vendorExtensions.x-is-one-of-interface}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-implementation.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-implementation.mustache index 6f32a22ea7..dc65d4c311 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-implementation.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-implementation.mustache @@ -8,6 +8,7 @@ import io.micronaut.http.exceptions.HttpStatusException; {{/generateOperationsToReturnNotImplemented}} {{#reactive}} import reactor.core.publisher.Mono; +import reactor.core.publisher.Flux; {{/reactive}} {{#wrapInHttpResponse}} import io.micronaut.http.HttpResponse; diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-interface.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-interface.mustache index da7709e81a..934f5b00bf 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-interface.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-interface.mustache @@ -15,6 +15,7 @@ import io.micronaut.security.rules.SecurityRule; {{/useAuth}} {{#reactive}} import reactor.core.publisher.Mono; +import reactor.core.publisher.Flux; {{/reactive}} {{#wrapInHttpResponse}} import io.micronaut.http.HttpResponse; @@ -22,7 +23,9 @@ import io.micronaut.http.HttpResponse; {{#imports}} import {{import}}; {{/imports}} +{{#withGeneratedAnnotation}} import {{javaxPackage}}.annotation.Generated; +{{/withGeneratedAnnotation}} import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/controllerOperationBody.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/controllerOperationBody.mustache index d16aa20f51..b1bf1c667a 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/controllerOperationBody.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/controllerOperationBody.mustache @@ -2,10 +2,10 @@ {{!The body needs to be implemented by user}} // TODO implement {{nickname}}(); {{^generateOperationsToReturnNotImplemented}} - {{#reactive}}{{#wrapInHttpResponse}}return Mono.fromCallable(HttpResponse::ok);{{/wrapInHttpResponse}}{{^wrapInHttpResponse}}return Mono.empty();{{/wrapInHttpResponse}}{{/reactive}}{{^reactive}}{{#wrapInHttpResponse}}return HttpResponse.ok();{{/wrapInHttpResponse}}{{^wrapInHttpResponse}}{{#returnType}}return null;{{/returnType}}{{/wrapInHttpResponse}}{{/reactive}} + {{#reactive}}{{#wrapInHttpResponse}}return {{#vendorExtensions.isReturnFlux}}Flux.from({{/vendorExtensions.isReturnFlux}}Mono.fromCallable(HttpResponse::ok){{#vendorExtensions.isReturnFlux}}){{/vendorExtensions.isReturnFlux}};{{/wrapInHttpResponse}}{{^wrapInHttpResponse}}{{#vendorExtensions.isReturnFlux}}return Flux.empty();{{/vendorExtensions.isReturnFlux}}{{^vendorExtensions.isReturnFlux}}return Mono.empty();{{/vendorExtensions.isReturnFlux}}{{/wrapInHttpResponse}}{{/reactive}}{{^reactive}}{{#wrapInHttpResponse}}return HttpResponse.ok();{{/wrapInHttpResponse}}{{^wrapInHttpResponse}}{{#returnType}}return null;{{/returnType}}{{/wrapInHttpResponse}}{{/reactive}} {{/generateOperationsToReturnNotImplemented}} {{#generateOperationsToReturnNotImplemented}} - {{#reactive}}{{#wrapInHttpResponse}}return Mono.just(HttpResponse.status(HttpStatus.NOT_IMPLEMENTED));{{/wrapInHttpResponse}}{{^wrapInHttpResponse}}return Mono.error(new HttpStatusException(HttpStatus.NOT_IMPLEMENTED, null));{{/wrapInHttpResponse}}{{/reactive}}{{^reactive}}{{#wrapInHttpResponse}}return HttpResponse.status(HttpStatus.NOT_IMPLEMENTED);{{/wrapInHttpResponse}}{{^wrapInHttpResponse}}throw new HttpStatusException(HttpStatus.NOT_IMPLEMENTED, null);{{/wrapInHttpResponse}}{{/reactive}} + {{#reactive}}{{#wrapInHttpResponse}}{{#vendorExtensions.isReturnFlux}}return Flux.just(HttpResponse.status(HttpStatus.NOT_IMPLEMENTED));{{/vendorExtensions.isReturnFlux}}{{^vendorExtensions.isReturnFlux}}return Mono.just(HttpResponse.status(HttpStatus.NOT_IMPLEMENTED));{{/vendorExtensions.isReturnFlux}}{{/wrapInHttpResponse}}{{^wrapInHttpResponse}}{{#vendorExtensions.isReturnFlux}}return Flux.error(new HttpStatusException(HttpStatus.NOT_IMPLEMENTED, null));{{/vendorExtensions.isReturnFlux}}{{^vendorExtensions.isReturnFlux}}return Mono.error(new HttpStatusException(HttpStatus.NOT_IMPLEMENTED, null));{{/vendorExtensions.isReturnFlux}}{{/wrapInHttpResponse}}{{/reactive}}{{^reactive}}{{#wrapInHttpResponse}}return HttpResponse.status(HttpStatus.NOT_IMPLEMENTED);{{/wrapInHttpResponse}}{{^wrapInHttpResponse}}throw new HttpStatusException(HttpStatus.NOT_IMPLEMENTED, null);{{/wrapInHttpResponse}}{{/reactive}} {{/generateOperationsToReturnNotImplemented}} {{/generateControllerFromExamples}} {{#generateControllerFromExamples}} @@ -20,7 +20,7 @@ return type present }}{{#returnType}}return {{! reactive start - }}{{#reactive}}Mono.fromCallable(() -> {{/reactive}}{{! + }}{{#reactive}}{{#vendorExtensions.isReturnFlux}}Flux.from({{/vendorExtensions.isReturnFlux}}Mono.fromCallable(() -> {{/reactive}}{{! wrapInHttpResponse start }}{{#wrapInHttpResponse}}HttpResponse.ok({{/wrapInHttpResponse}}{{! body @@ -28,15 +28,15 @@ wrapInHttpResponse end }}{{#wrapInHttpResponse}}){{/wrapInHttpResponse}}{{! reactive end - }}{{#reactive}}){{/reactive}};{{/returnType}}{{! + }}{{#vendorExtensions.isReturnFlux}}){{/vendorExtensions.isReturnFlux}}{{#reactive}}){{/reactive}};{{/returnType}}{{! return type not present }}{{^returnType}}{{! reactive }}{{#reactive}}return {{! wrapInHttpResponse - }}{{#wrapInHttpResponse}}Mono.fromCallable(HttpResponse::ok){{/wrapInHttpResponse}}{{^wrapInHttpResponse}}Mono.fromCallable(() -> null){{/wrapInHttpResponse}};{{/reactive}}{{! + }}{{#vendorExtensions.isReturnFlux}}Flux.from({{/vendorExtensions.isReturnFlux}}{{#wrapInHttpResponse}}Mono.fromCallable(HttpResponse::ok){{/wrapInHttpResponse}}{{^wrapInHttpResponse}}Mono.fromCallable(() -> null){{/wrapInHttpResponse}}{{#vendorExtensions.isReturnFlux}}){{/vendorExtensions.isReturnFlux}};{{/reactive}}{{! not reactive }}{{^reactive}}{{! wrapInHttpResponse }}{{#wrapInHttpResponse}}return HttpResponse.ok();{{/wrapInHttpResponse}}{{/reactive}}{{/returnType}} - {{/generateControllerFromExamples}} \ No newline at end of file + {{/generateControllerFromExamples}} 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 df25a4309c..d8a72f960f 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 @@ -21,6 +21,7 @@ import spock.lang.Specification import spock.lang.Ignore {{#reactive}} import reactor.core.publisher.Mono +import reactor.core.publisher.Flux {{/reactive}} import java.io.File import java.io.FileReader 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 2201915844..2219e9c4b6 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 @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Assertions; import jakarta.inject.Inject; {{#reactive}} import reactor.core.publisher.Mono; +import reactor.core.publisher.Flux; {{/reactive}} import java.io.File; import java.io.FileReader; @@ -105,17 +106,16 @@ class {{classname}}Test { {{{dataType}}} body = {{{example}}}; {{/bodyParam}} {{#formParams.0}} - Map form = new HashMap(){{openbrace}}{{openbrace}} + var form = new HashMap(); // Fill in the body form parameters {{#formParams}} {{^isFile}} - put("{{{baseName}}}", {{{example}}}); + form.put("{{{baseName}}}", {{{example}}}); {{/isFile}} {{#isFile}} - put("{{{baseName}}}", new FileReader(File.createTempFile("test", ".tmp"))); + form.put("{{{baseName}}}", new FileReader(File.createTempFile("test", ".tmp"))); {{/isFile}} {{/formParams}} - {{closebrace}}{{closebrace}}; {{/formParams.0}} {{#isMultipart}} {{^formParams}} diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/MicronautServerCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/MicronautServerCodegenTest.java index 1f5bc6cc4c..056fd95d66 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/MicronautServerCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/MicronautServerCodegenTest.java @@ -217,11 +217,27 @@ void doGenerateMonoWrapHttpResponse() { void doGenerateMono() { JavaMicronautServerCodegen codegen = new JavaMicronautServerCodegen(); codegen.additionalProperties().put(JavaMicronautServerCodegen.OPT_REACTIVE, "true"); + codegen.additionalProperties().put(JavaMicronautServerCodegen.OPT_FLUX_FOR_ARRAYS, "false"); codegen.additionalProperties().put(JavaMicronautServerCodegen.OPT_GENERATE_HTTP_RESPONSE_ALWAYS, "false"); String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.MODELS, CodegenConstants.APIS); String apiPath = outputPath + "src/main/java/org/openapitools/api/"; assertFileContains(apiPath + "PetApi.java", "Mono"); + assertFileNotContains(apiPath + "PetApi.java", "Flux"); + assertFileNotContains(apiPath + "PetApi.java", "HttpResponse"); + } + + @Test + void doGenerateMonoAndFlux() { + JavaMicronautServerCodegen codegen = new JavaMicronautServerCodegen(); + codegen.additionalProperties().put(JavaMicronautServerCodegen.OPT_REACTIVE, "true"); + codegen.additionalProperties().put(JavaMicronautServerCodegen.OPT_FLUX_FOR_ARRAYS, "true"); + codegen.additionalProperties().put(JavaMicronautServerCodegen.OPT_GENERATE_HTTP_RESPONSE_ALWAYS, "false"); + String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.MODELS, CodegenConstants.APIS); + + String apiPath = outputPath + "src/main/java/org/openapitools/api/"; + assertFileContains(apiPath + "PetApi.java", "Mono"); + assertFileContains(apiPath + "PetApi.java", "Flux"); assertFileNotContains(apiPath + "PetApi.java", "HttpResponse"); } diff --git a/test-suite-client-generator/build.gradle b/test-suite-client-generator/build.gradle index ddaccc92ad..d82e6eb12e 100644 --- a/test-suite-client-generator/build.gradle +++ b/test-suite-client-generator/build.gradle @@ -16,4 +16,6 @@ dependencies { implementation("io.micronaut.validation:micronaut-validation") implementation("io.projectreactor:reactor-core") runtimeOnly("ch.qos.logback:logback-classic") + + testImplementation("io.micronaut.test:micronaut-test-spock") } diff --git a/test-suite-server-generator/build.gradle b/test-suite-server-generator/build.gradle index ef17665bc2..94a8506e44 100644 --- a/test-suite-server-generator/build.gradle +++ b/test-suite-server-generator/build.gradle @@ -39,7 +39,7 @@ sourceSets { tasks.named("generateOpenApi") { generatorKind = "server" openApiDefinition = layout.projectDirectory.file("spec.yaml") - outputKinds = ["models", "apis", "supportingFiles"] + outputKinds = ["models", "apis", "modelDocs", "supportingFiles", "modelTests", "apiTests"] parameterMappings = [ // Pageable parameter [name: "page", location: "QUERY", mappedType: "io.micronaut.data.model.Pageable"], diff --git a/test-suite-server-generator/src/main/java/io/micronaut/openapi/test/api/RequestBodyController.java b/test-suite-server-generator/src/main/java/io/micronaut/openapi/test/api/RequestBodyController.java index 9d25633785..212b632dbc 100644 --- a/test-suite-server-generator/src/main/java/io/micronaut/openapi/test/api/RequestBodyController.java +++ b/test-suite-server-generator/src/main/java/io/micronaut/openapi/test/api/RequestBodyController.java @@ -13,7 +13,6 @@ import io.micronaut.http.annotation.Post; import io.micronaut.http.multipart.CompletedFileUpload; import jakarta.validation.Valid; -import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.io.ByteArrayOutputStream;