Skip to content

Commit

Permalink
Add ability to genearate Flux<?> instead Mono<List<?>> for arrays. (#…
Browse files Browse the repository at this point in the history
…1213)

Fixed bugs in generated tests.
Removed duplicates model_test templates.

Fixed #1200
  • Loading branch information
altro3 authored Sep 22, 2023
1 parent 79b0dc2 commit db69912
Show file tree
Hide file tree
Showing 30 changed files with 127 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public abstract class AbstractMicronautJavaCodegen<T extends GeneratorOptionsBui
public static final String OPT_REQUIRED_PROPERTIES_IN_CONSTRUCTOR = "requiredPropertiesInConstructor";
public static final String OPT_USE_AUTH = "useAuth";
public static final String OPT_USE_LOMBOK = "lombok";
public static final String OPT_FLUX_FOR_ARRAYS = "fluxForArrays";
public static final String OPT_GENERATED_ANNOTATION = "generatedAnnotation";
public static final String OPT_VISITABLE = "visitable";
public static final String OPT_DATE_LIBRARY_ZONED_DATETIME = "ZONED_DATETIME";
Expand All @@ -98,11 +99,15 @@ public abstract class AbstractMicronautJavaCodegen<T extends GeneratorOptionsBui
public static final String CONTENT_TYPE_MULTIPART_FORM_DATA = "multipart/form-data";
public static final String CONTENT_TYPE_ANY = "*/*";

private static final String MONO_CLASS_NAME = "reactor.core.publisher.Mono";
private static final String FLUX_CLASS_NAME = "reactor.core.publisher.Flux";

protected String title;
protected boolean useBeanValidation;
protected boolean useOptional;
protected boolean visitable;
protected boolean lombok;
protected boolean fluxForArrays;
protected boolean generatedAnnotation = true;
protected String testTool;
protected boolean requiredPropertiesInConstructor = true;
Expand Down Expand Up @@ -170,6 +175,7 @@ protected AbstractMicronautJavaCodegen() {
cliOptions.add(new CliOption(OPT_TITLE, "Client service name").defaultValue(title));
cliOptions.add(new CliOption(OPT_APPLICATION_NAME, "Micronaut application name (Defaults to the " + CodegenConstants.ARTIFACT_ID + " value)").defaultValue(appName));
cliOptions.add(CliOption.newBoolean(OPT_USE_LOMBOK, "Whether or not to use lombok annotations in generated code", lombok));
cliOptions.add(CliOption.newBoolean(OPT_FLUX_FOR_ARRAYS, "Whether or not to use Flux<?> instead Mono<List<?>> 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));
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -254,6 +261,7 @@ ClientOptions build() {
clientId,
useAuth,
lombok,
fluxForArrays,
generatedAnnotation
);
}
Expand All @@ -266,6 +274,7 @@ record ClientOptions(
String clientId,
boolean useAuth,
boolean lombok,
boolean fluxForArrays,
boolean generatedAnnotation
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -376,6 +383,7 @@ ServerOptions build() {
generateControllerFromExamples,
useAuth,
lombok,
fluxForArrays,
generatedAnnotation,
aot
);
Expand All @@ -389,6 +397,7 @@ record ServerOptions(
boolean generateControllerFromExamples,
boolean useAuth,
boolean lombok,
boolean fluxForArrays,
boolean generatedAnnotation,
boolean aot
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand All @@ -168,6 +169,7 @@ public void configureClientOptions() {
}
clientCodegen.setConfigureAuthorization(clientOptions.useAuth());
clientCodegen.setLombok(clientOptions.lombok());
clientCodegen.setFluxForArrays(clientOptions.fluxForArrays());
clientCodegen.setGeneratedAnnotation(clientOptions.generatedAnnotation());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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<String, OauthClientConfiguration> clientConfigurationByName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Or the `@Inject` annotation:
```java
@Singleton
class MyClass {
@Inject
{{classname}} {{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}};

Expand All @@ -41,7 +42,7 @@ More information can be found inside [Inversion of Control guide section](https:
<a id="{{operationId}}"></a>
# **{{operationId}}**
```java
{{#returnType}}Mono<{{{returnType}}}>{{/returnType}}{{^returnType}}Mono<Void>{{/returnType}} {{classname}}.{{nickname}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Mono<Void>{{/returnType}} {{classname}}.{{nickname}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
```

{{summary}}{{#notes}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

This file was deleted.

Loading

0 comments on commit db69912

Please sign in to comment.