Skip to content

Commit 41024dc

Browse files
[JAVA](native-client) Add support for UnaryInterceptors (#22381)
* added UnaryInterceptor functionality * set useUnaryInterceptor default to false * added generated files * updated generated files to new default value * fixed asyncResponseInterceptor and indentations * fixed newlines and generate comments on config * updated comment in async response interceptor * reverting docstring * adjusting newlines
1 parent 9116411 commit 41024dc

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

docs/generators/java-microprofile.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
105105
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
106106
|useSealedOneOfInterfaces|Generate the oneOf interfaces as sealed interfaces. Only supported for WebClient and RestClient.| |false|
107107
|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|
108+
|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|
108109
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
109110
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|
110111
|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
@@ -105,6 +105,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
105105
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
106106
|useSealedOneOfInterfaces|Generate the oneOf interfaces as sealed interfaces. Only supported for WebClient and RestClient.| |false|
107107
|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|
108+
|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|
108109
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
109110
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|
110111
|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/languages/JavaClientCodegen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
105105
public static final String FAIL_ON_UNKNOWN_PROPERTIES = "failOnUnknownProperties";
106106
public static final String SUPPORT_VERTX_FUTURE = "supportVertxFuture";
107107
public static final String USE_SEALED_ONE_OF_INTERFACES = "useSealedOneOfInterfaces";
108+
public static final String USE_UNARY_INTERCEPTOR = "useUnaryInterceptor";
108109

109110
// Internal configurations
110111
public static final String SINGLE_REQUEST_PARAMETER = "singleRequestParameter";
@@ -149,6 +150,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
149150
@Getter @Setter protected boolean failOnUnknownProperties = false;
150151
@Setter protected boolean supportVertxFuture = false;
151152
@Setter protected boolean useSealedOneOfInterfaces = false;
153+
@Setter protected boolean useUnaryInterceptor = false;
152154
protected String authFolder;
153155
/**
154156
* Serialization library.
@@ -260,6 +262,7 @@ public JavaClientCodegen() {
260262
cliOptions.add(CliOption.newBoolean(FAIL_ON_UNKNOWN_PROPERTIES, "Fail Jackson de-serialization on unknown properties", this.failOnUnknownProperties));
261263
cliOptions.add(CliOption.newBoolean(SUPPORT_VERTX_FUTURE, "Also generate api methods that return a vertx Future instead of taking a callback. Only `vertx` supports this option. Requires vertx 4 or greater.", this.supportVertxFuture));
262264
cliOptions.add(CliOption.newBoolean(USE_SEALED_ONE_OF_INTERFACES, "Generate the oneOf interfaces as sealed interfaces. Only supported for WebClient and RestClient.", this.useSealedOneOfInterfaces));
265+
cliOptions.add(CliOption.newBoolean(USE_UNARY_INTERCEPTOR, "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", this.useUnaryInterceptor));
263266

264267
supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.17.1");
265268
supportedLibraries.put(JERSEY3, "HTTP client: Jersey client 3.1.1. JSON processing: Jackson 2.17.1");
@@ -376,6 +379,7 @@ public void processOpts() {
376379
}
377380
convertPropertyToStringAndWriteBack(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, this::setUseSingleRequestParameter);
378381
convertPropertyToBooleanAndWriteBack(USE_SEALED_ONE_OF_INTERFACES, this::setUseSealedOneOfInterfaces);
382+
convertPropertyToBooleanAndWriteBack(USE_UNARY_INTERCEPTOR, this::setUseUnaryInterceptor);
379383
writePropertyBack(SINGLE_REQUEST_PARAMETER, getSingleRequestParameter());
380384
writePropertyBack(STATIC_REQUEST, getStaticRequest());
381385

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import java.util.Objects;
3737
import java.util.zip.GZIPOutputStream;
3838
{{/useGzipFeature}}
3939
import java.util.stream.Collectors;
40+
{{#useUnaryInterceptor}}
41+
import java.util.function.UnaryOperator;
42+
{{/useUnaryInterceptor}}
4043

4144
import static java.nio.charset.StandardCharsets.UTF_8;
4245

@@ -64,8 +67,14 @@ public class ApiClient {
6467
protected int port;
6568
protected String basePath;
6669
protected Consumer<HttpRequest.Builder> interceptor;
70+
{{#useUnaryInterceptor}}
71+
protected UnaryOperator<HttpResponse<InputStream>> responseInterceptor;
72+
protected UnaryOperator<HttpResponse<InputStream>> asyncResponseInterceptor;
73+
{{/useUnaryInterceptor}}
74+
{{^useUnaryInterceptor}}
6775
protected Consumer<HttpResponse<InputStream>> responseInterceptor;
6876
protected Consumer<HttpResponse<InputStream>> asyncResponseInterceptor;
77+
{{/useUnaryInterceptor}}
6978
protected Duration readTimeout;
7079
protected Duration connectTimeout;
7180

@@ -361,12 +370,12 @@ public class ApiClient {
361370
* Set a custom response interceptor.
362371
*
363372
* <p>This is useful for logging, monitoring or extraction of header variables</p>
364-
*
373+
*{{#useUnaryInterceptor}} <p>If you are using the UnaryInterceptor you can even manipulate the response to a certain degree</p>{{/useUnaryInterceptor}}
365374
* @param interceptor A function invoked before creating each request. A value
366375
* of null resets the interceptor to a no-op.
367376
* @return This object.
368377
*/
369-
public ApiClient setResponseInterceptor(Consumer<HttpResponse<InputStream>> interceptor) {
378+
public ApiClient setResponseInterceptor({{#useUnaryInterceptor}}UnaryOperator{{/useUnaryInterceptor}}{{^useUnaryInterceptor}}Consumer{{/useUnaryInterceptor}}<HttpResponse<InputStream>> interceptor) {
370379
this.responseInterceptor = interceptor;
371380
return this;
372381
}
@@ -376,20 +385,20 @@ public class ApiClient {
376385
*
377386
* @return The custom interceptor that was set, or null if there isn't any.
378387
*/
379-
public Consumer<HttpResponse<InputStream>> getResponseInterceptor() {
388+
public {{#useUnaryInterceptor}}UnaryOperator{{/useUnaryInterceptor}}{{^useUnaryInterceptor}}Consumer{{/useUnaryInterceptor}}<HttpResponse<InputStream>> getResponseInterceptor() {
380389
return responseInterceptor;
381390
}
382391

383392
/**
384393
* Set a custom async response interceptor. Use this interceptor when asyncNative is set to 'true'.
385394
*
386395
* <p>This is useful for logging, monitoring or extraction of header variables</p>
387-
*
396+
*{{#useUnaryInterceptor}} <p>If you are using the UnaryInterceptor you can even manipulate the response to a certain degree</p>{{/useUnaryInterceptor}}
388397
* @param interceptor A function invoked before creating each request. A value
389398
* of null resets the interceptor to a no-op.
390399
* @return This object.
391400
*/
392-
public ApiClient setAsyncResponseInterceptor(Consumer<HttpResponse<InputStream>> interceptor) {
401+
public ApiClient setAsyncResponseInterceptor({{#useUnaryInterceptor}}UnaryOperator{{/useUnaryInterceptor}}{{^useUnaryInterceptor}}Consumer{{/useUnaryInterceptor}}<HttpResponse<InputStream>> interceptor) {
393402
this.asyncResponseInterceptor = interceptor;
394403
return this;
395404
}
@@ -399,7 +408,7 @@ public class ApiClient {
399408
*
400409
* @return The custom interceptor that was set, or null if there isn't any.
401410
*/
402-
public Consumer<HttpResponse<InputStream>> getAsyncResponseInterceptor() {
411+
public {{#useUnaryInterceptor}}UnaryOperator{{/useUnaryInterceptor}}{{^useUnaryInterceptor}}Consumer{{/useUnaryInterceptor}}<HttpResponse<InputStream>> getAsyncResponseInterceptor() {
403412
return asyncResponseInterceptor;
404413
}
405414

modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ import java.util.List;
4848
import java.util.Map;
4949
import java.util.Set;
5050
import java.util.Locale;
51+
{{#useUnaryInterceptor}}
52+
import java.util.function.UnaryOperator;
53+
{{/useUnaryInterceptor}}
5154
import java.util.function.Consumer;
5255
{{#useGzipFeature}}
5356
import java.util.function.Supplier;
@@ -86,8 +89,14 @@ public class {{classname}} {
8689
private final String memberVarBaseUri;
8790
private final Consumer<HttpRequest.Builder> memberVarInterceptor;
8891
private final Duration memberVarReadTimeout;
92+
{{#useUnaryInterceptor}}
93+
private final UnaryOperator<HttpResponse<InputStream>> memberVarResponseInterceptor;
94+
private final UnaryOperator<HttpResponse<InputStream>> memberVarAsyncResponseInterceptor;
95+
{{/useUnaryInterceptor}}
96+
{{^useUnaryInterceptor}}
8997
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
9098
private final Consumer<HttpResponse<InputStream>> memberVarAsyncResponseInterceptor;
99+
{{/useUnaryInterceptor}}
91100

92101
public {{classname}}() {
93102
this(Configuration.getDefaultApiClient());
@@ -433,7 +442,12 @@ public class {{classname}} {
433442
localVarRequestBuilder.build(),
434443
HttpResponse.BodyHandlers.ofInputStream());
435444
if (memberVarResponseInterceptor != null) {
445+
{{#useUnaryInterceptor}}
446+
localVarResponse = memberVarResponseInterceptor.apply(localVarResponse);
447+
{{/useUnaryInterceptor}}
448+
{{^useUnaryInterceptor}}
436449
memberVarResponseInterceptor.accept(localVarResponse);
450+
{{/useUnaryInterceptor}}
437451
}
438452
InputStream localVarResponseBody = null;
439453
try {
@@ -526,7 +540,12 @@ public class {{classname}} {
526540
localVarRequestBuilder.build(),
527541
HttpResponse.BodyHandlers.ofInputStream()).thenComposeAsync(localVarResponse -> {
528542
if (memberVarAsyncResponseInterceptor != null) {
543+
{{#useUnaryInterceptor}}
544+
localVarResponse = memberVarAsyncResponseInterceptor.apply(localVarResponse);
545+
{{/useUnaryInterceptor}}
546+
{{^useUnaryInterceptor}}
529547
memberVarAsyncResponseInterceptor.accept(localVarResponse);
548+
{{/useUnaryInterceptor}}
530549
}
531550
if (localVarResponse.statusCode()/ 100 != 2) {
532551
return CompletableFuture.failedFuture(getApiException("{{operationId}}", localVarResponse));
@@ -935,4 +954,4 @@ public class {{classname}} {
935954
{{/vendorExtensions.x-group-parameters}}
936955
{{/operation}}
937956
}
938-
{{/operations}}
957+
{{/operations}}

0 commit comments

Comments
 (0)