Skip to content

Commit bbbfc85

Browse files
authored
#23560 Correct imports for RxJava2 and RxJava3. Add support for Vert.x 5. (#23563)
* #23560 Correct imports for RxJava2 and RxJava3. Add support for Vert.x 5 while keeping backward compatibility for earlier versions. Add new parameter for useVertx5. * #23560 Update Samples and Docs * #23560 Remove unnecessary whitespace and add sample for Vert.x 5 * #23560 Update gradle template for generated samples to use correct libraries and add path to samples-java-client-jdk11.yaml * #23560 Update Java compatibility and fix code review comments * #23560 fix code review comment * #23560 fix maven pom template * #23560 fix maven pom template
1 parent 8144279 commit bbbfc85

254 files changed

Lines changed: 28325 additions & 12 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/samples-java-client-jdk11.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ jobs:
7373
- samples/client/petstore/java/webclient-swagger2
7474
- samples/client/petstore/java/webclient-useSingleRequestParameter
7575
- samples/client/petstore/java/vertx
76+
- samples/client/petstore/java/vertx5
7677
- samples/client/petstore/java/vertx-no-nullable
7778
- samples/client/petstore/java/vertx-supportVertxFuture
7879
- samples/client/petstore/java/jersey2-java8-localdatetime

bin/configs/java-vertx5.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
generatorName: java
2+
outputDir: samples/client/petstore/java/vertx5
3+
library: vertx
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/Java
6+
useVertx5: "true"
7+
useRxJava3: "true"
8+
additionalProperties:
9+
artifactId: petstore-vertx5
10+
hideGenerationTimestamp: "true"
11+
dateLibrary: java8

docs/generators/java-microprofile.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
111111
|useSingleRequestParameter|Setting this property to "true" will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter. ONLY native, jersey2, jersey3, okhttp-gson, microprofile, Spring RestClient, Spring WebClient support this option. Setting this property to "static" does the same as "true", but also makes the generated arguments class static with single parameter instantiation.| |false|
112112
|useSpringBoot4|Generate code and provide dependencies for use with Spring Boot 4.x.| |false|
113113
|useUnaryInterceptor|If true it will generate ResponseInterceptors using a UnaryOperator. This can be usefull for manipulating the request before it gets passed, for example doing your own decryption| |false|
114+
|useVertx5|Whether to use Vert.x 5 syntax.| |false|
114115
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
115116
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|
116117
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|

docs/generators/java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
111111
|useSingleRequestParameter|Setting this property to "true" will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter. ONLY native, jersey2, jersey3, okhttp-gson, microprofile, Spring RestClient, Spring WebClient support this option. Setting this property to "static" does the same as "true", but also makes the generated arguments class static with single parameter instantiation.| |false|
112112
|useSpringBoot4|Generate code and provide dependencies for use with Spring Boot 4.x.| |false|
113113
|useUnaryInterceptor|If true it will generate ResponseInterceptors using a UnaryOperator. This can be usefull for manipulating the request before it gets passed, for example doing your own decryption| |false|
114+
|useVertx5|Whether to use Vert.x 5 syntax.| |false|
114115
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
115116
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|
116117
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
406406
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
407407
public static final String USE_SINGLE_REQUEST_PARAMETER_DESC = "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.";
408408

409+
public static final String USE_VERTX_5 = "useVertx5";
410+
public static final String USE_VERTX_5_DESC = "Setting this property to true will generate Vert.x 5 specific callbacks using Callables.";
411+
409412
public static final String DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT = "disallowAdditionalPropertiesIfNotPresent";
410413
public static final String DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC =
411414
"If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. " +

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
6262
public static final String USE_RX_JAVA2 = "useRxJava2";
6363
public static final String USE_RX_JAVA3 = "useRxJava3";
6464
public static final String DO_NOT_USE_RX = "doNotUseRx";
65+
public static final String USE_VERTX_5 = "useVertx5";
6566
public static final String USE_PLAY_WS = "usePlayWS";
6667
public static final String ASYNC_NATIVE = "asyncNative";
6768
public static final String CONFIG_KEY = "configKey";
@@ -130,6 +131,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
130131
protected boolean useRxJava = false;
131132
protected boolean useRxJava2 = false;
132133
protected boolean useRxJava3 = false;
134+
@Setter protected boolean useVertx5 = false;
133135
// backwards compatibility for openapi configs that specify neither rx1 nor rx2
134136
// (mustache does not allow for boolean operators so we need this extra field)
135137
@Setter protected boolean doNotUseRx = true;
@@ -243,6 +245,7 @@ public JavaClientCodegen() {
243245
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA2, "Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated."));
244246
cliOptions.add(CliOption.newBoolean(USE_RX_JAVA3, "Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated."));
245247
cliOptions.add(CliOption.newBoolean(PARCELABLE_MODEL, "Whether to generate models for Android that implement Parcelable with the okhttp-gson library."));
248+
cliOptions.add(CliOption.newBoolean(USE_VERTX_5, "Whether to use Vert.x 5 syntax."));
246249
cliOptions.add(CliOption.newBoolean(USE_PLAY_WS, "Use Play! Async HTTP client (Play WS API)"));
247250
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
248251
cliOptions.add(CliOption.newBoolean(PERFORM_BEANVALIDATION, "Perform BeanValidation"));
@@ -424,6 +427,8 @@ public void processOpts() {
424427
convertPropertyToBooleanAndWriteBack(USE_RX_JAVA3, this::setUseRxJava3);
425428
convertPropertyToBooleanAndWriteBack(USE_RX_JAVA2, this::setUseRxJava2);
426429
}
430+
convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_VERTX_5, this::setUseVertx5);
431+
427432
convertPropertyToStringAndWriteBack(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, this::setUseSingleRequestParameter);
428433
convertPropertyToBooleanAndWriteBack(USE_SEALED_ONE_OF_INTERFACES, this::setUseSealedOneOfInterfaces);
429434
convertPropertyToBooleanAndWriteBack(USE_UNARY_INTERCEPTOR, this::setUseUnaryInterceptor);

modules/openapi-generator/src/main/resources/Java/libraries/vertx/ApiClient.mustache

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,25 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
433433
return contentTypes[0];
434434
}
435435

436+
{{#useVertx5}}
437+
public Future<HttpResponse<Buffer>> sendBody(HttpRequest<Buffer> request,
438+
Object body) {
439+
if (body instanceof byte[]) {
440+
Buffer buffer = Buffer.buffer((byte[]) body);
441+
return request.sendBuffer(buffer);
442+
} else if (body instanceof AsyncFile) {
443+
AsyncFile file = (AsyncFile) body;
444+
return request.sendStream(file);
445+
} else {
446+
try {
447+
return request.sendBuffer(Buffer.buffer(this.objectMapper.writeValueAsBytes(body)));
448+
} catch (JsonProcessingException jsonProcessingException) {
449+
return Future.failedFuture(jsonProcessingException);
450+
}
451+
}
452+
}
453+
{{/useVertx5}}
454+
{{^useVertx5}}
436455
public void sendBody(HttpRequest<Buffer> request,
437456
Handler<AsyncResult<HttpResponse<Buffer>>> responseHandler,
438457
Object body) {
@@ -450,6 +469,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
450469
}
451470
}
452471
}
472+
{{/useVertx5}}
453473

454474
/**
455475
* Invoke API by sending HTTP request with the given options.
@@ -514,13 +534,28 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
514534

515535
Handler<AsyncResult<HttpResponse<Buffer>>> responseHandler = buildResponseHandler(returnType, resultHandler);
516536
if (body != null) {
537+
{{#useVertx5}}
538+
sendBody(request, body).onComplete(responseHandler::handle);
539+
{{/useVertx5}}
540+
{{^useVertx5}}
517541
sendBody(request, responseHandler, body);
542+
{{/useVertx5}}
518543
} else if (formParams != null && !formParams.isEmpty()) {
519544
Map<String, String> formMap = formParams.entrySet().stream().collect(toMap(Map.Entry::getKey, entry -> parameterToString(entry.getValue())));
520545
MultiMap form = MultiMap.caseInsensitiveMultiMap().addAll(formMap);
546+
{{#useVertx5}}
547+
request.sendForm(form).onComplete(responseHandler::handle);
548+
{{/useVertx5}}
549+
{{^useVertx5}}
521550
request.sendForm(form, responseHandler);
551+
{{/useVertx5}}
522552
} else {
553+
{{#useVertx5}}
554+
request.send().onComplete(responseHandler::handle);
555+
{{/useVertx5}}
556+
{{^useVertx5}}
523557
request.send(responseHandler);
558+
{{/useVertx5}}
524559
}
525560
}
526561

@@ -577,6 +612,19 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
577612
578613
String filename = generateFilename(response.headers());
579614
Consumer<String> fileHandler = directory -> {
615+
{{#useVertx5}}
616+
fs.open(directory + filename, FILE_DOWNLOAD_OPTIONS).onComplete(asyncFileResult -> {
617+
if (asyncFileResult.succeeded()) {
618+
AsyncFile asyncFile = asyncFileResult.result();
619+
asyncFile.write(response.bodyAsBuffer());
620+
//noinspection unchecked
621+
handler.handle(Future.succeededFuture((T) asyncFile));
622+
} else {
623+
handler.handle(ApiException.fail(asyncFileResult.cause()));
624+
}
625+
});
626+
{{/useVertx5}}
627+
{{^useVertx5}}
580628
fs.open(directory + filename, FILE_DOWNLOAD_OPTIONS, asyncFileResult -> {
581629
if (asyncFileResult.succeeded()) {
582630
AsyncFile asyncFile = asyncFileResult.result();
@@ -587,14 +635,22 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
587635
handler.handle(ApiException.fail(asyncFileResult.cause()));
588636
}
589637
});
638+
{{/useVertx5}}
590639
};
591-
592640
String dir = getDownloadsDir();
593641
if (dir != null && !dir.isEmpty()) {
642+
{{#useVertx5}}
643+
fs.mkdirs(dir).onComplete(mkdirResult -> {
644+
String sanitizedFolder = dir.endsWith("/") ? dir : dir + "/";
645+
fileHandler.accept(sanitizedFolder);
646+
});
647+
{{/useVertx5}}
648+
{{^useVertx5}}
594649
fs.mkdirs(dir, mkdirResult -> {
595650
String sanitizedFolder = dir.endsWith("/") ? dir : dir + "/";
596651
fileHandler.accept(sanitizedFolder);
597652
});
653+
{{/useVertx5}}
598654
} else {
599655
fileHandler.accept("");
600656
}

modules/openapi-generator/src/main/resources/Java/libraries/vertx/build.gradle.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ repositories {
1111
apply plugin: 'java'
1212
apply plugin: 'maven-publish'
1313

14-
sourceCompatibility = JavaVersion.VERSION_1_8
15-
targetCompatibility = JavaVersion.VERSION_1_8
14+
sourceCompatibility = {{#useVertx5}}JavaVersion.VERSION_11{{/useVertx5}}{{^useVertx5}}JavaVersion.VERSION_1_8{{/useVertx5}}
15+
targetCompatibility = {{#useVertx5}}JavaVersion.VERSION_11{{/useVertx5}}{{^useVertx5}}JavaVersion.VERSION_1_8{{/useVertx5}}
1616

1717
publishing {
1818
publications {
@@ -32,7 +32,7 @@ ext {
3232
swagger_annotations_version = "1.5.21"
3333
jackson_version = "2.19.2"
3434
jackson_databind_version = "2.19.2"
35-
vertx_version = "{{#supportVertxFuture}}4.0.0{{/supportVertxFuture}}{{^supportVertxFuture}}3.5.2{{/supportVertxFuture}}"
35+
vertx_version = "{{#useVertx5}}5.0.11{{/useVertx5}}{{#supportVertxFuture}}{{^useVertx5}}4.0.0{{/useVertx5}}{{/supportVertxFuture}}{{^supportVertxFuture}}{{^useVertx5}}3.5.2{{/useVertx5}}{{/supportVertxFuture}}"
3636
junit_version = "5.10.3"
3737
{{#openApiNullable}}
3838
jackson_databind_nullable_version = "0.2.10"
@@ -44,7 +44,7 @@ dependencies {
4444
implementation "io.swagger:swagger-annotations:$swagger_annotations_version"
4545
implementation "com.google.code.findbugs:jsr305:3.0.2"
4646
implementation "io.vertx:vertx-web-client:$vertx_version"
47-
implementation "io.vertx:vertx-rx-java:$vertx_version"
47+
implementation "io.vertx:{{#useRxJava3}}vertx-rx-java3{{/useRxJava3}}{{#useRxJava2}}vertx-rx-java2{{/useRxJava2}}{{^useRxJava3}}{{^useRxJava2}}vertx-rx-java{{/useRxJava2}}{{/useRxJava3}}:$vertx_version"
4848
implementation "com.fasterxml.jackson.core:jackson-core:$jackson_version"
4949
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
5050
implementation "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"

modules/openapi-generator/src/main/resources/Java/libraries/vertx/pom.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@
144144
<artifactId>maven-compiler-plugin</artifactId>
145145
<version>3.6.1</version>
146146
<configuration>
147-
<source>1.8</source>
148-
<target>1.8</target>
147+
<source>{{#useVertx5}}11{{/useVertx5}}{{^useVertx5}}1.8{{/useVertx5}}</source>
148+
<target>{{#useVertx5}}11{{/useVertx5}}{{^useVertx5}}1.8{{/useVertx5}}</target>
149149
</configuration>
150150
</plugin>
151151
<plugin>
@@ -231,7 +231,7 @@
231231
<!-- Vertx -->
232232
<dependency>
233233
<groupId>io.vertx</groupId>
234-
<artifactId>vertx-rx-java</artifactId>
234+
<artifactId>{{#useRxJava3}}vertx-rx-java3{{/useRxJava3}}{{#useRxJava2}}vertx-rx-java2{{/useRxJava2}}{{^useRxJava3}}{{^useRxJava2}}vertx-rx-java{{/useRxJava2}}{{/useRxJava3}}</artifactId>
235235
<version>${vertx-version}</version>
236236
</dependency>
237237
<dependency>
@@ -299,7 +299,7 @@
299299

300300
<properties>
301301
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
302-
<vertx-version>{{#supportVertxFuture}}4.0.0{{/supportVertxFuture}}{{^supportVertxFuture}}3.5.2{{/supportVertxFuture}}</vertx-version>
302+
<vertx-version>{{#useVertx5}}5.0.11{{/useVertx5}}{{#supportVertxFuture}}{{^useVertx5}}4.0.0{{/useVertx5}}{{/supportVertxFuture}}{{^supportVertxFuture}}{{^useVertx5}}3.5.2{{/useVertx5}}{{/supportVertxFuture}}</vertx-version>
303303
{{#swagger1AnnotationLibrary}}
304304
<swagger-annotations-version>1.6.6</swagger-annotations-version>
305305
{{/swagger1AnnotationLibrary}}

modules/openapi-generator/src/main/resources/Java/libraries/vertx/rxApiImpl.mustache

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ import {{invokerPackage}}.ApiClient;
66

77
import java.util.*;
88

9+
{{#useRxJava3}}
10+
import io.reactivex.rxjava3.core.Single;
11+
import io.vertx.rxjava3.SingleHelper;
12+
{{/useRxJava3}}
13+
{{#useRxJava2}}
14+
import io.reactivex.Single;
15+
import io.vertx.reactivex.SingleHelper;
16+
{{/useRxJava2}}
17+
{{^useRxJava2}}{{^useRxJava3}}
918
import rx.Single;
19+
{{/useRxJava3}}{{/useRxJava2}}
1020
import io.vertx.core.AsyncResult;
1121
import io.vertx.core.Handler;
1222

@@ -60,9 +70,18 @@ public class {{classname}} {
6070
* @return Asynchronous result handler (RxJava Single)
6171
*/
6272
public Single<{{{returnType}}}{{^returnType}}Void{{/returnType}}> rx{{operationIdCamelCase}}({{#allParams}}{{{dataType}}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) {
73+
{{#useRxJava2}}
74+
return SingleHelper.toSingle(fut ->
75+
delegate.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}fut));
76+
{{/useRxJava2}}
77+
{{#useRxJava3}}
78+
return SingleHelper.toSingle(fut -> delegate.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}fut));
79+
{{/useRxJava3}}
80+
{{^useRxJava2}}{{^useRxJava3}}
6381
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut ->
6482
delegate.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}fut)
6583
));
84+
{{/useRxJava3}}{{/useRxJava2}}
6685
}
6786

6887
/**
@@ -75,9 +94,19 @@ public class {{classname}} {
7594
* @return Asynchronous result handler (RxJava Single)
7695
*/
7796
public Single<{{{returnType}}}{{^returnType}}Void{{/returnType}}> rx{{operationIdCamelCase}}({{#allParams}}{{{dataType}}} {{paramName}}, {{/allParams}}ApiClient.AuthInfo authInfo) {
97+
{{#useRxJava2}}
98+
return SingleHelper.toSingle(fut ->
99+
delegate.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}authInfo, fut));
100+
{{/useRxJava2}}
101+
{{#useRxJava3}}
102+
return SingleHelper.toSingle(fut ->
103+
delegate.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}authInfo, fut));
104+
{{/useRxJava3}}
105+
{{^useRxJava2}}{{^useRxJava3}}
78106
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut ->
79107
delegate.{{operationId}}({{#allParams}}{{paramName}}, {{/allParams}}authInfo, fut)
80108
));
109+
{{/useRxJava3}}{{/useRxJava2}}
81110
}
82111
{{/operation}}
83112

0 commit comments

Comments
 (0)