diff --git a/src/main/java/io/swagger/codegen/v3/generators/java/MicronautCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/java/MicronautCodegen.java index 91c4859f28..41d6d4ad40 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/java/MicronautCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/java/MicronautCodegen.java @@ -1,5 +1,6 @@ package io.swagger.codegen.v3.generators.java; +import com.github.jknack.handlebars.Handlebars; import com.github.jknack.handlebars.Lambda; import com.google.common.collect.ImmutableMap; import io.swagger.codegen.v3.*; @@ -30,10 +31,18 @@ public class MicronautCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, OptionalFeatures { private static Logger LOGGER = LoggerFactory.getLogger(MicronautCodegen.class); + private static final String DEFAULT_LIBRARY = "rxjava3"; + private static final String RXJAVA3_LIBRARY = "rxjava3"; + private static final String RXJAVA2_LIBRARY = "rxjava2"; + private static final String REACTOR_LIBRARY = "reactor"; private static final String TITLE = "title"; private static final String CONFIG_PACKAGE = "configPackage"; private static final String BASE_PACKAGE = "basePackage"; private static final String USE_TAGS = "useTags"; + private static final String USE_RXJAVA = "useRxJava"; + private static final String USE_RXJAVA2 = "useRxJava2"; + private static final String USE_RXJAVA3 = "useRxJava3"; + private static final String USE_REACTOR = "useReactor"; private static final String IMPLICIT_HEADERS = "implicitHeaders"; private static final String SKIP_SUPPORT_FILES = "skipSupportFiles"; @@ -69,16 +78,21 @@ private void init() { cliOptions.add(new CliOption(BASE_PACKAGE, "base package (invokerPackage) for generated code")); cliOptions.add(new CliOption(SKIP_SUPPORT_FILES, "skip support files such as pom.xml, mvnw, etc from code generation.")); cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames")); - cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations")); + + CliOption useBeanValidation = CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"); + useBeanValidation.setDefault("true"); + cliOptions.add(useBeanValidation); + cliOptions.add(CliOption.newBoolean(IMPLICIT_HEADERS, "Use of @ApiImplicitParams for headers.")); cliOptions.add(CliOption.newBoolean(USE_OPTIONAL, "Use Optional container for optional parameters")); - supportedLibraries.put(DEFAULT_LIBRARY, "Java Micronaut Server application."); + supportedLibraries.put(DEFAULT_LIBRARY, "Java Micronaut Server application with RxJava3 reactive streams implementation"); + supportedLibraries.put(USE_RXJAVA2, "Java Micronaut Server application with RxJava2 reactive streams implementation"); + supportedLibraries.put(REACTOR_LIBRARY, "Java Micronaut Server application with Project Reactor reactive streams implementation"); setLibrary(DEFAULT_LIBRARY); CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use"); - library.setDefault(DEFAULT_LIBRARY); library.setEnum(supportedLibraries); library.setDefault(DEFAULT_LIBRARY); cliOptions.add(library); @@ -138,8 +152,8 @@ public void processOpts() { this.setBasePackage((String) additionalProperties.get(BASE_PACKAGE)); } - if (additionalProperties.containsKey(USE_TAGS)) { - this.setUseTags(Boolean.valueOf(additionalProperties.get(USE_TAGS).toString())); + if (additionalProperties.get(USE_TAGS) != null) { + this.setUseTags(Boolean.parseBoolean(additionalProperties.get(USE_TAGS).toString())); } if (additionalProperties.containsKey(USE_BEANVALIDATION)) { @@ -151,21 +165,23 @@ public void processOpts() { } boolean skipSupportFiles = false; - if (additionalProperties.containsKey(SKIP_SUPPORT_FILES)) { - skipSupportFiles = Boolean.valueOf(additionalProperties.get(SKIP_SUPPORT_FILES).toString()); + if (additionalProperties.get(SKIP_SUPPORT_FILES) != null) { + skipSupportFiles = Boolean.parseBoolean(additionalProperties.get(SKIP_SUPPORT_FILES).toString()); } - if (useBeanValidation) { - writePropertyBack(USE_BEANVALIDATION, useBeanValidation); - } + writePropertyBack(USE_BEANVALIDATION, useBeanValidation); - if (additionalProperties.containsKey(IMPLICIT_HEADERS)) { - this.setImplicitHeaders(Boolean.valueOf(additionalProperties.get(IMPLICIT_HEADERS).toString())); + if (additionalProperties.get(IMPLICIT_HEADERS) != null) { + this.setImplicitHeaders(Boolean.parseBoolean(additionalProperties.get(IMPLICIT_HEADERS).toString())); } - if (useOptional) { - writePropertyBack(USE_OPTIONAL, useOptional); + writePropertyBack(USE_OPTIONAL, useOptional); + if (isRxJava2Library()) { + additionalProperties.put(USE_RXJAVA2, true); + } else { + additionalProperties.put(USE_RXJAVA3, isRxJava3Library()); } + additionalProperties.put(USE_REACTOR, isReactorLibrary()); if (!skipSupportFiles) { supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); @@ -174,7 +190,8 @@ public void processOpts() { supportingFiles.add(new SupportingFile("mvnw.cmd", "", "mvnw.cmd")); supportingFiles.add(new SupportingFile("unsupportedOperationExceptionHandler.mustache", (sourceFolder + File.separator + configPackage).replace(".", File.separator), "UnsupportedOperationExceptionHandler.java")); - supportingFiles.add(new SupportingFile("mainApplication.mustache", (sourceFolder + File.separator).replace(".", File.separator), "MainApplication.java")); + supportingFiles.add(new SupportingFile("mainApplication.mustache", (sourceFolder + File.separator + basePackage).replace(".", File.separator), "MainApplication.java")); + apiTemplateFiles.put("apiController.mustache", "Controller.java"); } addHandlebarsLambdas(additionalProperties); } @@ -529,4 +546,22 @@ public void setUseBeanValidation(boolean useBeanValidation) { public void setUseOptional(boolean useOptional) { this.useOptional = useOptional; } + + @Override + public void addHandlebarHelpers(Handlebars handlebars) { + handlebars.setInfiniteLoops(true); + super.addHandlebarHelpers(handlebars); + } + + private boolean isRxJava2Library() { + return library.equals(RXJAVA2_LIBRARY); + } + + private boolean isRxJava3Library() { + return library.equals(RXJAVA3_LIBRARY); + } + + private boolean isReactorLibrary() { + return library.equals(REACTOR_LIBRARY); + } } diff --git a/src/main/resources/handlebars/JavaMicronaut/api.mustache b/src/main/resources/handlebars/JavaMicronaut/api.mustache index 64f65fa789..2bc0e282ba 100644 --- a/src/main/resources/handlebars/JavaMicronaut/api.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/api.mustache @@ -10,11 +10,20 @@ package {{package}}; import com.fasterxml.jackson.databind.ObjectMapper; import io.micronaut.http.*; import io.micronaut.http.annotation.*; +{{#useRxJava3}} +import io.reactivex.rxjava3.core.Single; +{{/useRxJava3}} +{{^useRxJava3}} +{{#useRxJava2}} import io.reactivex.Single; +{{/useRxJava2}} +{{/useRxJava3}} import io.swagger.v3.oas.annotations.*; import io.swagger.v3.oas.annotations.responses.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +{{#useReactor}} +import org.reactivestreams.Publisher; +import reactor.core.publisher.Mono; +{{/useReactor}} {{#useBeanValidation}} {{#jakarta}} @@ -31,11 +40,12 @@ import javax.validation.constraints.*; import java.io.IOException; import java.util.List; import java.util.Map; +{{#useOptional}} import java.util.Optional; +{{/useOptional}} {{>generatedAnnotation}} {{#operations}} -@Controller public interface {{classname}} { {{#operation}} @@ -53,8 +63,8 @@ public interface {{classname}} { }) {{/implicitHeaders}} @{{#lambda.capitalise}}{{httpMethod}}{{/lambda.capitalise}}(value = "{{{path}}}"{{#hasProduces}}, produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}}, consumes = {{braces "left"}}{{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}{{braces "right"}}{{/hasConsumes}}) - default SinglereturnTypes}}>> {{operationId}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/parameters}}) { - return Single.fromCallable(() -> { + default SinglereturnTypes}}>> {{operationId}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/parameters}}) { + return {{^useReactor}}Single{{/useReactor}}{{#useReactor}}Mono{{/useReactor}}.fromCallable(() -> { throw new UnsupportedOperationException(); }); } diff --git a/src/main/resources/handlebars/JavaMicronaut/apiController.mustache b/src/main/resources/handlebars/JavaMicronaut/apiController.mustache new file mode 100644 index 0000000000..4cdc59424e --- /dev/null +++ b/src/main/resources/handlebars/JavaMicronaut/apiController.mustache @@ -0,0 +1,55 @@ +package {{package}}; + +{{#imports}} +import {{import}}; + +{{/imports}} +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.HttpResponse; +{{#useRxJava3}} +import io.reactivex.rxjava3.core.Single; +{{/useRxJava3}} +{{^useRxJava3}} +{{#useRxJava2}} +import io.reactivex.Single; +{{/useRxJava2}} +{{/useRxJava3}} +{{#useReactor}} +import org.reactivestreams.Publisher; +import reactor.core.publisher.Mono; +{{/useReactor}} + +{{#useBeanValidation}} +import javax.annotation.Nullable; +import javax.validation.Valid; +import javax.validation.constraints.*; +{{/useBeanValidation}} +import java.util.List; +import java.util.Map; +{{#useOptional}} +import java.util.Optional; +{{/useOptional}} + +{{#operations}} +@Controller +public class {{classname}}Controller implements {{classname}} { +{{#operation}} +{{#contents}} + + @Override + public SinglereturnTypes}}>> {{operationId}}({{#parameters}}{{#useBeanValidation}}{{! + Bean validation spec for path parameters (PathParam is always required) +}}{{#isPathParam}}{{>beanValidationParams}}{{/isPathParam}}{{! + Bean validation spec for any other parameters +}}{{^isPathParam}}{{>nullableBeanValidationParams}}{{/isPathParam}}{{/useBeanValidation}}{{! + Method argument type and name +}}{{>optionalDataType}} {{paramName}}{{! + Arguments separator, if required +}}{{#hasMore}}, {{/hasMore}}{{/parameters}}) { + // TODO: Implement me + return {{classname}}.super.{{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}}); + } +{{/contents}} +{{/operation}} +} +{{/operations}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/api_test.mustache b/src/main/resources/handlebars/JavaMicronaut/api_test.mustache index 9bb78225f2..a3f012f24b 100644 --- a/src/main/resources/handlebars/JavaMicronaut/api_test.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/api_test.mustache @@ -5,15 +5,12 @@ package {{package}}; import io.micronaut.http.HttpResponse; import io.micronaut.http.HttpStatus; -import io.micronaut.test.annotation.MicronautTest; +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; import org.junit.jupiter.api.Test; - -{{#jakarta}} +{{#useReactor}} +import reactor.core.publisher.Mono; +{{/useReactor}} import jakarta.inject.Inject; -{{/jakarta}} -{{^jakarta}} -import javax.inject.Inject; -{{/jakarta}} import java.util.*; @@ -28,18 +25,20 @@ class {{classname}}ControllerTest { {{#operations}} {{#operation}} {{#contents}} + {{#@first}} @Test - void {{operationId}}{{#isForm}}WithForm{{/isForm}}Test() { + void {{operationId}}{{#isForm}}Form{{/isForm}}Test() { {{#parameters}} - {{{dataType}}} {{paramName}} = {{{example}}}; + {{{dataType}}} {{paramName}} = null; {{/parameters}} try { - api.{{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}}).blockingGet(); + //TODO: {{#useReactor}}Mono.from({{/useReactor}}api.{{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}}){{#useReactor}}){{/useReactor}}{{^useReactor}}.blockingGet();{{/useReactor}}{{#useReactor}}.block();{{/useReactor}} } catch (UnsupportedOperationException e) { assumeTrue(false, "API is not yet implemented"); } } + {{/@first}} {{/contents}} {{/operation}} {{/operations}} diff --git a/src/main/resources/handlebars/JavaMicronaut/beanValidation.mustache b/src/main/resources/handlebars/JavaMicronaut/beanValidation.mustache index 3e4ef612a4..0894c7cdbc 100644 --- a/src/main/resources/handlebars/JavaMicronaut/beanValidation.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/beanValidation.mustache @@ -3,4 +3,4 @@ {{/required}}{{#isContainer}}{{^isPrimitiveType}}{{^isEnum}} @Valid{{/isEnum}}{{/isPrimitiveType}}{{/isContainer}}{{#isNotContainer}}{{^isPrimitiveType}} @Valid{{/isPrimitiveType}}{{/isNotContainer}} -{{>beanValidationCore}} +{{>beanValidationCore}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/beanValidationHeaderParams.mustache b/src/main/resources/handlebars/JavaMicronaut/beanValidationHeaderParams.mustache deleted file mode 100644 index 6ff580a67c..0000000000 --- a/src/main/resources/handlebars/JavaMicronaut/beanValidationHeaderParams.mustache +++ /dev/null @@ -1 +0,0 @@ -{{#required}}@NotNull {{/required}}{{^required}}@Nullable {{/required}}{{>beanValidationCore}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/beanValidationParams.mustache b/src/main/resources/handlebars/JavaMicronaut/beanValidationParams.mustache new file mode 100644 index 0000000000..b01b404811 --- /dev/null +++ b/src/main/resources/handlebars/JavaMicronaut/beanValidationParams.mustache @@ -0,0 +1 @@ +{{#useBeanValidation}}{{>beanValidationParamsInner}}{{>beanValidationCore}}{{/useBeanValidation}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/beanValidationParamsInner.mustache b/src/main/resources/handlebars/JavaMicronaut/beanValidationParamsInner.mustache new file mode 100644 index 0000000000..aaa696691e --- /dev/null +++ b/src/main/resources/handlebars/JavaMicronaut/beanValidationParamsInner.mustache @@ -0,0 +1,5 @@ +{{^isPrimitiveType}}{{^isEnum}}{{! + Non-container +}}{{^isContainer}}@Valid {{/isContainer}}{{! + Container +}}{{#isContainer}}{{#items}}{{>beanValidationParamsInner}}{{/items}}{{/isContainer}}{{/isEnum}}{{/isPrimitiveType}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/beanValidationPathParams.mustache b/src/main/resources/handlebars/JavaMicronaut/beanValidationPathParams.mustache deleted file mode 100644 index 051bd53c0a..0000000000 --- a/src/main/resources/handlebars/JavaMicronaut/beanValidationPathParams.mustache +++ /dev/null @@ -1 +0,0 @@ -{{! PathParam is always required, no @NotNull necessary }}{{>beanValidationCore}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/beanValidationQueryParams.mustache b/src/main/resources/handlebars/JavaMicronaut/beanValidationQueryParams.mustache deleted file mode 100644 index 6ff580a67c..0000000000 --- a/src/main/resources/handlebars/JavaMicronaut/beanValidationQueryParams.mustache +++ /dev/null @@ -1 +0,0 @@ -{{#required}}@NotNull {{/required}}{{^required}}@Nullable {{/required}}{{>beanValidationCore}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/bodyParams.mustache b/src/main/resources/handlebars/JavaMicronaut/bodyParams.mustache index 3fffbee7e8..8bd655d9fa 100644 --- a/src/main/resources/handlebars/JavaMicronaut/bodyParams.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/bodyParams.mustache @@ -1 +1 @@ -{{#isBodyParam}}@Parameter(description = "{{{description}}}") {{#useBeanValidation}}@Valid {{/useBeanValidation}}@Body {{{dataType}}} {{paramName}}{{/isBodyParam}} \ No newline at end of file +{{#isBodyParam}}{{>nullableBeanValidationParams}}@Parameter(description = "{{{description}}}") @Body {{{dataType}}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/cookieParams.mustache b/src/main/resources/handlebars/JavaMicronaut/cookieParams.mustache new file mode 100644 index 0000000000..9e4c72a8ab --- /dev/null +++ b/src/main/resources/handlebars/JavaMicronaut/cookieParams.mustache @@ -0,0 +1 @@ +{{#isCookieParam}}{{>nullableBeanValidationParams}}@Parameter(description = "{{{description}}}") @CookieValue(value="{{baseName}}") {{>optionalDataType}} {{paramName}}{{/isCookieParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/enumClass.mustache b/src/main/resources/handlebars/JavaMicronaut/enumClass.mustache index 8884062720..f52f371d2d 100644 --- a/src/main/resources/handlebars/JavaMicronaut/enumClass.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/enumClass.mustache @@ -2,23 +2,16 @@ * {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}} */ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} { + {{#allowableValues}} + {{#enumVars}} {{#gson}} - {{#allowableValues}} - {{#enumVars}} - @SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}) - {{{name}}}({{{value}}}){{^@last}}, - {{/@last}}{{#@last}};{{/@last}} - {{/enumVars}} - {{/allowableValues}} - {{/gson}} - {{^gson}} - {{#allowableValues}} - {{#enumVars}} - {{{name}}}({{{value}}}){{^@last}}, - {{/@last}}{{#@last}};{{/@last}} - {{/enumVars}} - {{/allowableValues}} + {{#value}} + @SerializedName({{{toQuotedWord value}}}) + {{/value}} {{/gson}} + {{{name}}}({{#value}}{{{value}}}{{/value}}{{^value}}null{{/value}}){{^@last}},{{/@last}}{{#@last}};{{/@last}} + {{/enumVars}} + {{/allowableValues}} private {{{datatype}}} value; diff --git a/src/main/resources/handlebars/JavaMicronaut/enumOuterClass.mustache b/src/main/resources/handlebars/JavaMicronaut/enumOuterClass.mustache index 8d9e187986..14858672f1 100644 --- a/src/main/resources/handlebars/JavaMicronaut/enumOuterClass.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/enumOuterClass.mustache @@ -6,17 +6,16 @@ import com.fasterxml.jackson.annotation.JsonCreator; * {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}} */ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} { - {{#gson}} - {{#allowableValues}}{{#enumVars}} - @SerializedName({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}) - {{{name}}}({{{value}}}){{^@last}}, - {{/@last}}{{#@last}};{{/@last}}{{/enumVars}}{{/allowableValues}} - {{/gson}} - {{^gson}} - {{#allowableValues}}{{#enumVars}} - {{{name}}}({{{value}}}){{^@last}}, - {{/@last}}{{#@last}};{{/@last}}{{/enumVars}}{{/allowableValues}} - {{/gson}} + {{#allowableValues}} + {{#enumVars}} + {{#gson}} + {{#value}} + @SerializedName({{{toQuotedWord value}}}) + {{/value}} + {{/gson}} + {{{name}}}({{#value}}{{{value}}}{{/value}}{{^value}}null{{/value}}){{^@last}},{{/@last}}{{#@last}};{{/@last}} + {{/enumVars}} + {{/allowableValues}} private {{{dataType}}} value; diff --git a/src/main/resources/handlebars/JavaMicronaut/formParams.mustache b/src/main/resources/handlebars/JavaMicronaut/formParams.mustache index 5b5e062853..494a4fb912 100644 --- a/src/main/resources/handlebars/JavaMicronaut/formParams.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/formParams.mustache @@ -1 +1 @@ -{{#isFormParam}}{{#notFile}}@Parameter(description = "{{{description}}}") @QueryValue(value = "{{baseName}}") {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@Parameter(description = "file detail") {{#useBeanValidation}}@Valid {{/useBeanValidation}}MultipartFile {{baseName}}{{/isFile}}{{/isFormParam}} +{{#isFormParam}}{{#notFile}}{{>nullableBeanValidationParams}}@Parameter(description = "{{{description}}}") @Body(value = "{{baseName}}") {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@Parameter(description = "file detail") {{#useBeanValidation}}@Valid {{/useBeanValidation}}File {{baseName}}{{/isFile}}{{/isFormParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/headerParams.mustache b/src/main/resources/handlebars/JavaMicronaut/headerParams.mustache index b56740532b..5f6dcf0b3e 100644 --- a/src/main/resources/handlebars/JavaMicronaut/headerParams.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/headerParams.mustache @@ -1 +1 @@ -{{#isHeaderParam}}{{#useBeanValidation}}{{>beanValidationHeaderParams}}{{/useBeanValidation}}@Parameter(description = "{{{description}}}") {{#useBeanValidation}}@Valid {{/useBeanValidation}}@Header(value = "{{baseName}}") {{>optionalDataType}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file +{{#isHeaderParam}}{{>nullableBeanValidationParams}}@Parameter(description = "{{{description}}}") @Header(value = "{{baseName}}") {{>optionalDataType}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/mainApplication.mustache b/src/main/resources/handlebars/JavaMicronaut/mainApplication.mustache index b3ace528b9..653395132d 100644 --- a/src/main/resources/handlebars/JavaMicronaut/mainApplication.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/mainApplication.mustache @@ -8,7 +8,7 @@ import io.swagger.v3.oas.annotations.info.*; info = @Info( title = "{{appName}}", version = "{{appVersion}}", - description = "{{appDescription}}", + description = "{{{appDescription}}}", contact = @Contact( name = "{{infoName}}", email = "{{infoEmail}}" diff --git a/src/main/resources/handlebars/JavaMicronaut/model.mustache b/src/main/resources/handlebars/JavaMicronaut/model.mustache index c2debf22ad..5c26b09746 100644 --- a/src/main/resources/handlebars/JavaMicronaut/model.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/model.mustache @@ -7,6 +7,7 @@ import java.util.Objects; import java.io.Serializable; {{/serializableModel}} {{#useBeanValidation}} +import io.micronaut.core.annotation.Introspected; import io.micronaut.validation.Validated; {{#jakarta}} import jakarta.validation.Valid; diff --git a/src/main/resources/handlebars/JavaMicronaut/nullableBeanValidationParams.mustache b/src/main/resources/handlebars/JavaMicronaut/nullableBeanValidationParams.mustache new file mode 100644 index 0000000000..5d387de368 --- /dev/null +++ b/src/main/resources/handlebars/JavaMicronaut/nullableBeanValidationParams.mustache @@ -0,0 +1 @@ +{{#useBeanValidation}}{{#required}}@NotNull {{/required}}{{^required}}@Nullable {{/required}}{{>beanValidationParams}}{{/useBeanValidation}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/pathParams.mustache b/src/main/resources/handlebars/JavaMicronaut/pathParams.mustache index 2e51d583bb..4ac4b0ecd0 100644 --- a/src/main/resources/handlebars/JavaMicronaut/pathParams.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/pathParams.mustache @@ -1 +1,3 @@ -{{#isPathParam}}{{#useBeanValidation}}{{>beanValidationPathParams}}{{/useBeanValidation}}@Parameter(description = "{{{description}}}") @PathVariable("{{baseName}}") {{>optionalDataType}} {{paramName}}{{/isPathParam}} \ No newline at end of file +{{#isPathParam}}{{! + PathParam is always required, no @NotNull necessary +}}{{>beanValidationParams}}@Parameter(description = "{{{description}}}") @PathVariable("{{baseName}}") {{>optionalDataType}} {{paramName}}{{/isPathParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/pojo.mustache b/src/main/resources/handlebars/JavaMicronaut/pojo.mustache index 55669741b3..5e86ef1bcb 100644 --- a/src/main/resources/handlebars/JavaMicronaut/pojo.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/pojo.mustache @@ -2,7 +2,10 @@ * {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}} */{{#description}} @Schema(description = "{{{description}}}"){{/description}} -{{#useBeanValidation}}@Validated{{/useBeanValidation}} +{{#useBeanValidation}} +@Validated +@Introspected +{{/useBeanValidation}} {{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}} public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} { {{#serializableModel}} diff --git a/src/main/resources/handlebars/JavaMicronaut/pom.mustache b/src/main/resources/handlebars/JavaMicronaut/pom.mustache index 85318c9f5f..d5db343bf6 100644 --- a/src/main/resources/handlebars/JavaMicronaut/pom.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/pom.mustache @@ -1,73 +1,106 @@ - + + 4.0.0 {{groupId}} {{artifactId}} {{artifactVersion}} + ${packaging} + + + io.micronaut + micronaut-parent + 3.0.0 + + - 1.1.4 + jar 1.8 - UTF-8 + + + 3.0.0 {{basePackage}}.MainApplication + netty + - jcenter.bintray.com - https://jcenter.bintray.com + central + https://repo.maven.apache.org/maven2 - - - - io.micronaut - micronaut-bom - ${micronaut.version} - pom - import - - - + io.micronaut micronaut-inject compile +{{#useBeanValidation}} io.micronaut micronaut-validation compile +{{/useBeanValidation}} io.micronaut - micronaut-runtime + micronaut-http-client compile - io.swagger.core.v3 - swagger-annotations + io.micronaut + micronaut-http-server-netty compile io.micronaut - micronaut-http-client + micronaut-runtime compile - io.micronaut - micronaut-http-server-netty +{{#useRxJava3}} + io.micronaut.rxjava3 + micronaut-rxjava3 +{{/useRxJava3}} +{{^useRxJava3}} +{{#useRxJava2}} + io.micronaut.rxjava2 + micronaut-rxjava2 +{{/useRxJava2}} +{{/useRxJava3}} + +{{#useReactor}} + io.micronaut.reactor + micronaut-reactor +{{/useReactor}} compile ch.qos.logback logback-classic - 1.2.3 runtime + + io.swagger.core.v3 + swagger-annotations + compile + +{{#useBeanValidation}} + + com.google.code.findbugs + jsr305 + +{{/useBeanValidation}} +{{#threetenbp}} com.github.joschi.jackson jackson-datatype-threetenbp 2.6.4 +{{/threetenbp}} +{{#generateApiTests}} + org.junit.jupiter junit-jupiter-api @@ -83,114 +116,40 @@ micronaut-test-junit5 test +{{/generateApiTests}} + - org.apache.maven.plugins - maven-shade-plugin - 3.1.0 - - - package - - shade - - - - - ${exec.mainClass} - - - - - - + io.micronaut.build + micronaut-maven-plugin + - org.codehaus.mojo - exec-maven-plugin - 1.6.0 + org.apache.maven.plugins + maven-compiler-plugin - java - - -noverify - -XX:TieredStopAtLevel=1 - -Dcom.sun.management.jmxremote - -classpath - - ${exec.mainClass} - + + + +{{#useBeanValidation}} + + + io.micronaut + micronaut-http-validation + ${micronaut.version} + + +{{/useBeanValidation}} + + -Amicronaut.processing.group={{groupId}} + -Amicronaut.processing.module={{artifactId}} + - - maven-surefire-plugin - 2.22.0 - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.7.0 - - ${jdk.version} - ${jdk.version} - UTF-8 - - -parameters - - - - io.micronaut - micronaut-inject-java - ${micronaut.version} - - - io.micronaut - micronaut-validation - ${micronaut.version} - - - io.micronaut.configuration - micronaut-openapi - 1.1.1 - - - - - - test-compile - - testCompile - - - - -parameters - - - - io.micronaut - micronaut-inject-java - ${micronaut.version} - - - io.micronaut - micronaut-validation - ${micronaut.version} - - - io.micronaut.configuration - micronaut-openapi - 1.1.1 - - - - - - - - + + diff --git a/src/main/resources/handlebars/JavaMicronaut/publisher.mustache b/src/main/resources/handlebars/JavaMicronaut/publisher.mustache new file mode 100644 index 0000000000..e76087e08e --- /dev/null +++ b/src/main/resources/handlebars/JavaMicronaut/publisher.mustache @@ -0,0 +1 @@ +{{#useReactor}}Publisher{{/useReactor}}{{^useReactor}}Single{{/useReactor}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/queryParams.mustache b/src/main/resources/handlebars/JavaMicronaut/queryParams.mustache index eb1cbbb525..a27659430f 100644 --- a/src/main/resources/handlebars/JavaMicronaut/queryParams.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/queryParams.mustache @@ -1 +1 @@ -{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@Parameter(description = "{{{description}}}") {{#useBeanValidation}}@Valid {{/useBeanValidation}}@QueryValue(value = "{{baseName}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{>optionalDataType}} {{paramName}}{{/isQueryParam}} \ No newline at end of file +{{#isQueryParam}}{{>nullableBeanValidationParams}}@Parameter(description = "{{{description}}}") @QueryValue(value = "{{baseName}}"{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{>optionalDataType}} {{paramName}}{{/isQueryParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaMicronaut/unsupportedOperationExceptionHandler.mustache b/src/main/resources/handlebars/JavaMicronaut/unsupportedOperationExceptionHandler.mustache index 98d04d41a4..1ea8ed604f 100644 --- a/src/main/resources/handlebars/JavaMicronaut/unsupportedOperationExceptionHandler.mustache +++ b/src/main/resources/handlebars/JavaMicronaut/unsupportedOperationExceptionHandler.mustache @@ -7,13 +7,7 @@ import io.micronaut.http.HttpResponse; import io.micronaut.http.HttpStatus; import io.micronaut.http.annotation.Produces; import io.micronaut.http.server.exceptions.ExceptionHandler; - -{{#jakarta}} import jakarta.inject.Singleton; -{{/jakarta}} -{{^jakarta}} -import javax.inject.Singleton; -{{/jakarta}} @Produces @Singleton diff --git a/src/test/java/io/swagger/codegen/v3/generators/java/MicronautGeneratorCodegenTest.java b/src/test/java/io/swagger/codegen/v3/generators/java/MicronautGeneratorCodegenTest.java index 177f1e98bd..db50d0e105 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/java/MicronautGeneratorCodegenTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/java/MicronautGeneratorCodegenTest.java @@ -50,8 +50,7 @@ public void testConfigurations() { @Test(description = "verify interface api generated") public void testApiInterface() throws IOException { - final String expectedContent = "@Controller" + System.lineSeparator() - + "public interface AdminApi {"; + final String expectedContent = "public interface AdminApi {"; final File controllerFile = new File(output, "/src/main/java/io/swagger/api/AdminApi.java"); final String content = FileUtils.readFileToString(controllerFile); Assert.assertTrue(content.contains(expectedContent)); @@ -59,10 +58,10 @@ public void testApiInterface() throws IOException { @Test(description = "verify that parameters are listed as follows: header, path, query, cookie, body") public void testApiParameters() throws IOException { - final String expectedContent = "default Single> updateTest(@Parameter(description = \"Localized Text object.\") @Valid @Body LocalizedText body" + System.lineSeparator() - + ",@NotNull @Pattern(regexp=\"[0-9]+\") @Parameter(description = \"header description\") @Valid @Header(value = \"x-header\") String xHeader" + System.lineSeparator() - + ",@Parameter(description = \"path description\") @PathVariable(\"id\") Long id" + System.lineSeparator() - + ",@Nullable @Parameter(description = \"query description\") @Valid @QueryValue(value = \"name\") String name"; + final String expectedContent = "default Single> updateTest(@NotNull @Valid @Parameter(description = \"Localized Text object.\") @Body LocalizedText body" + + ",@NotNull @Pattern(regexp=\"[0-9]+\") @Parameter(description = \"header description\") @Header(value = \"x-header\") String xHeader" + + ",@Parameter(description = \"path description\") @PathVariable(\"id\") Long id" + + ",@Nullable @Parameter(description = \"query description\") @QueryValue(value = \"name\") String name"; final File controllerFile = new File(output, "/src/main/java/io/swagger/api/AdminApi.java"); final String content = FileUtils.readFileToString(controllerFile); Assert.assertTrue(content.contains(expectedContent));