Skip to content

Commit 179288d

Browse files
committed
#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.
1 parent c07f3a0 commit 179288d

4 files changed

Lines changed: 95 additions & 1 deletion

File tree

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: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,26 @@ 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+
455+
{{^useVertx5}}
436456
public void sendBody(HttpRequest<Buffer> request,
437457
Handler<AsyncResult<HttpResponse<Buffer>>> responseHandler,
438458
Object body) {
@@ -450,6 +470,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
450470
}
451471
}
452472
}
473+
{{/useVertx5}}
453474

454475
/**
455476
* Invoke API by sending HTTP request with the given options.
@@ -514,13 +535,28 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
514535

515536
Handler<AsyncResult<HttpResponse<Buffer>>> responseHandler = buildResponseHandler(returnType, resultHandler);
516537
if (body != null) {
538+
{{#useVertx5}}
539+
sendBody(request, body).onComplete(responseHandler::handle);
540+
{{/useVertx5}}
541+
{{^useVertx5}}
517542
sendBody(request, responseHandler, body);
543+
{{/useVertx5}}
518544
} else if (formParams != null && !formParams.isEmpty()) {
519545
Map<String, String> formMap = formParams.entrySet().stream().collect(toMap(Map.Entry::getKey, entry -> parameterToString(entry.getValue())));
520546
MultiMap form = MultiMap.caseInsensitiveMultiMap().addAll(formMap);
547+
{{#useVertx5}}
548+
request.sendForm(form).onComplete(responseHandler::handle);
549+
{{/useVertx5}}
550+
{{^useVertx5}}
521551
request.sendForm(form, responseHandler);
552+
{{/useVertx5}}
522553
} else {
554+
{{#useVertx5}}
555+
request.send().onComplete(responseHandler::handle);
556+
{{/useVertx5}}
557+
{{^useVertx5}}
523558
request.send(responseHandler);
559+
{{/useVertx5}}
524560
}
525561
}
526562

@@ -577,6 +613,19 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
577613
578614
String filename = generateFilename(response.headers());
579615
Consumer<String> fileHandler = directory -> {
616+
{{#useVertx5}}
617+
fs.open(directory + filename, FILE_DOWNLOAD_OPTIONS).onComplete(asyncFileResult -> {
618+
if (asyncFileResult.succeeded()) {
619+
AsyncFile asyncFile = asyncFileResult.result();
620+
asyncFile.write(response.bodyAsBuffer());
621+
//noinspection unchecked
622+
handler.handle(Future.succeededFuture((T) asyncFile));
623+
} else {
624+
handler.handle(ApiException.fail(asyncFileResult.cause()));
625+
}
626+
});
627+
{{/useVertx5}}
628+
{{^useVertx5}}
580629
fs.open(directory + filename, FILE_DOWNLOAD_OPTIONS, asyncFileResult -> {
581630
if (asyncFileResult.succeeded()) {
582631
AsyncFile asyncFile = asyncFileResult.result();
@@ -587,14 +636,22 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
587636
handler.handle(ApiException.fail(asyncFileResult.cause()));
588637
}
589638
});
639+
{{/useVertx5}}
590640
};
591-
592641
String dir = getDownloadsDir();
593642
if (dir != null && !dir.isEmpty()) {
643+
{{#useVertx5}}
644+
fs.mkdirs(dir).onComplete(mkdirResult -> {
645+
String sanitizedFolder = dir.endsWith("/") ? dir : dir + "/";
646+
fileHandler.accept(sanitizedFolder);
647+
});
648+
{{/useVertx5}}
649+
{{^useVertx5}}
594650
fs.mkdirs(dir, mkdirResult -> {
595651
String sanitizedFolder = dir.endsWith("/") ? dir : dir + "/";
596652
fileHandler.accept(sanitizedFolder);
597653
});
654+
{{/useVertx5}}
598655
} else {
599656
fileHandler.accept("");
600657
}

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)