Skip to content

Commit fda71a2

Browse files
added UnaryInterceptor functionality
1 parent e86daf9 commit fda71a2

4 files changed

Lines changed: 39 additions & 10 deletions

File tree

docs/generators/java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
104104
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
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|
107+
|useUnaryInterceptor|Specify if the ResponseInterceptor should be able to change the request before beeing returned| |false|
107108
|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|
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|

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 = true;
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: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import java.util.Objects;
3737
import java.util.zip.GZIPOutputStream;
3838
{{/useGzipFeature}}
3939
import java.util.stream.Collectors;
40-
40+
{{#useUnaryInterceptor}}import java.util.function.UnaryOperator;{{/useUnaryInterceptor}}
4141
import static java.nio.charset.StandardCharsets.UTF_8;
4242

4343
/**
@@ -64,8 +64,15 @@ public class ApiClient {
6464
protected int port;
6565
protected String basePath;
6666
protected Consumer<HttpRequest.Builder> interceptor;
67+
{{#useUnaryInterceptor}}
68+
protected UnaryOperator<HttpResponse<InputStream>> responseInterceptor;
69+
protected UnaryOperator<HttpResponse<InputStream>> asyncResponseInterceptor;
70+
{{/useUnaryInterceptor}}
71+
{{^useUnaryInterceptor}}
6772
protected Consumer<HttpResponse<InputStream>> responseInterceptor;
6873
protected Consumer<HttpResponse<InputStream>> asyncResponseInterceptor;
74+
{{/useUnaryInterceptor}}
75+
6976
protected Duration readTimeout;
7077
protected Duration connectTimeout;
7178

@@ -361,12 +368,12 @@ public class ApiClient {
361368
* Set a custom response interceptor.
362369
*
363370
* <p>This is useful for logging, monitoring or extraction of header variables</p>
364-
*
371+
* <p>If you are using the UnaryInterceptor you can even manipulate the response to a certain degree</p>
365372
* @param interceptor A function invoked before creating each request. A value
366373
* of null resets the interceptor to a no-op.
367374
* @return This object.
368375
*/
369-
public ApiClient setResponseInterceptor(Consumer<HttpResponse<InputStream>> interceptor) {
376+
public ApiClient setResponseInterceptor({{#useUnaryInterceptor}}UnaryOperator{{/useUnaryInterceptor}}{{^useUnaryInterceptor}}Consumer{{/useUnaryInterceptor}}<HttpResponse<InputStream>> interceptor) {
370377
this.responseInterceptor = interceptor;
371378
return this;
372379
}
@@ -376,20 +383,20 @@ public class ApiClient {
376383
*
377384
* @return The custom interceptor that was set, or null if there isn't any.
378385
*/
379-
public Consumer<HttpResponse<InputStream>> getResponseInterceptor() {
386+
public {{#useUnaryInterceptor}}UnaryOperator{{/useUnaryInterceptor}}{{^useUnaryInterceptor}}Consumer{{/useUnaryInterceptor}}<HttpResponse<InputStream>> getResponseInterceptor() {
380387
return responseInterceptor;
381388
}
382389

383390
/**
384391
* Set a custom async response interceptor. Use this interceptor when asyncNative is set to 'true'.
385392
*
386393
* <p>This is useful for logging, monitoring or extraction of header variables</p>
387-
*
394+
* <p>If you are using the UnaryInterceptor you can even manipulate the response to a certain degree</p>
388395
* @param interceptor A function invoked before creating each request. A value
389396
* of null resets the interceptor to a no-op.
390397
* @return This object.
391398
*/
392-
public ApiClient setAsyncResponseInterceptor(Consumer<HttpResponse<InputStream>> interceptor) {
399+
public ApiClient setAsyncResponseInterceptor({{#useUnaryInterceptor}}UnaryOperator{{/useUnaryInterceptor}}{{^useUnaryInterceptor}}Consumer{{/useUnaryInterceptor}}<HttpResponse<InputStream>> interceptor) {
393400
this.asyncResponseInterceptor = interceptor;
394401
return this;
395402
}
@@ -399,7 +406,7 @@ public class ApiClient {
399406
*
400407
* @return The custom interceptor that was set, or null if there isn't any.
401408
*/
402-
public Consumer<HttpResponse<InputStream>> getAsyncResponseInterceptor() {
409+
public {{#useUnaryInterceptor}}UnaryOperator{{/useUnaryInterceptor}}{{^useUnaryInterceptor}}Consumer{{/useUnaryInterceptor}}<HttpResponse<InputStream>> getAsyncResponseInterceptor() {
403410
return asyncResponseInterceptor;
404411
}
405412

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import java.util.List;
4848
import java.util.Map;
4949
import java.util.Set;
5050
import java.util.Locale;
51+
{{#useUnaryInterceptor}}import java.util.function.UnaryOperator;{{/useUnaryInterceptor}}
5152
import java.util.function.Consumer;
5253
{{#useGzipFeature}}
5354
import java.util.function.Supplier;
@@ -86,8 +87,14 @@ public class {{classname}} {
8687
private final String memberVarBaseUri;
8788
private final Consumer<HttpRequest.Builder> memberVarInterceptor;
8889
private final Duration memberVarReadTimeout;
90+
{{#useUnaryInterceptor}}
91+
private final UnaryOperator<HttpResponse<InputStream>> memberVarResponseInterceptor;
92+
private final UnaryOperator<HttpResponse<InputStream>> memberVarAsyncResponseInterceptor;
93+
{{/useUnaryInterceptor}}
94+
{{^useUnaryInterceptor}}
8995
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
9096
private final Consumer<HttpResponse<InputStream>> memberVarAsyncResponseInterceptor;
97+
{{/useUnaryInterceptor}}
9198

9299
public {{classname}}() {
93100
this(Configuration.getDefaultApiClient());
@@ -433,7 +440,12 @@ public class {{classname}} {
433440
localVarRequestBuilder.build(),
434441
HttpResponse.BodyHandlers.ofInputStream());
435442
if (memberVarResponseInterceptor != null) {
436-
memberVarResponseInterceptor.accept(localVarResponse);
443+
{{#useUnaryInterceptor}}
444+
localVarResponse = memberVarResponseInterceptor.apply(localVarResponse);
445+
{{/useUnaryInterceptor}}
446+
{{^useUnaryInterceptor}}
447+
memberVarResponseInterceptor.accept(localVarResponse);
448+
{{/useUnaryInterceptor}}
437449
}
438450
InputStream localVarResponseBody = null;
439451
try {
@@ -526,7 +538,12 @@ public class {{classname}} {
526538
localVarRequestBuilder.build(),
527539
HttpResponse.BodyHandlers.ofInputStream()).thenComposeAsync(localVarResponse -> {
528540
if (memberVarAsyncResponseInterceptor != null) {
529-
memberVarAsyncResponseInterceptor.accept(localVarResponse);
541+
{{#useUnaryInterceptor}}
542+
localVarResponse = memberVarResponseInterceptor.apply(localVarResponse);
543+
{{/useUnaryInterceptor}}
544+
{{^useUnaryInterceptor}}
545+
memberVarResponseInterceptor.accept(localVarResponse);
546+
{{/useUnaryInterceptor}}
530547
}
531548
if (localVarResponse.statusCode()/ 100 != 2) {
532549
return CompletableFuture.failedFuture(getApiException("{{operationId}}", localVarResponse));
@@ -935,4 +952,4 @@ public class {{classname}} {
935952
{{/vendorExtensions.x-group-parameters}}
936953
{{/operation}}
937954
}
938-
{{/operations}}
955+
{{/operations}}

0 commit comments

Comments
 (0)