Skip to content

Commit

Permalink
[Java][WebClient] global blocking operations config (#14076)
Browse files Browse the repository at this point in the history
* [Java][WebClient] global blocking operations config

* update samples
  • Loading branch information
borsch committed Dec 6, 2022
1 parent 811e0de commit 2524e8f
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/generators/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
|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 jersey2, jersey3, okhttp-gson support this option.| |false|
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
|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|

## SUPPORTED VENDOR EXTENSIONS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
public static final String MICROPROFILE_REST_CLIENT_DEFAULT_ROOT_PACKAGE = "javax";
public static final String MICROPROFILE_DEFAULT = "default";
public static final String MICROPROFILE_KUMULUZEE = "kumuluzee";
public static final String WEBCLIENT_BLOCKING_OPERATIONS = "webclientBlockingOperations";

public static final String SERIALIZATION_LIBRARY_GSON = "gson";
public static final String SERIALIZATION_LIBRARY_JACKSON = "jackson";
Expand Down Expand Up @@ -126,6 +127,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
protected String rootJavaEEPackage;
protected Map<String, MpRestClientVersion> mpRestClientVersions = new HashMap<>();
protected boolean useSingleRequestParameter = false;
protected boolean webclientBlockingOperations = false;

private static class MpRestClientVersion {
public final String rootPackage;
Expand Down Expand Up @@ -204,6 +206,7 @@ public JavaClientCodegen() {
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC + " Only jersey2, jersey3, native, okhttp-gson support this option."));
cliOptions.add(CliOption.newString(MICROPROFILE_REST_CLIENT_VERSION, "Version of MicroProfile Rest Client API."));
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter. ONLY jersey2, jersey3, okhttp-gson support this option."));
cliOptions.add(CliOption.newBoolean(WEBCLIENT_BLOCKING_OPERATIONS, "Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync", this.webclientBlockingOperations));

supportedLibraries.put(JERSEY1, "HTTP client: Jersey client 1.19.x. JSON processing: Jackson 2.9.x. Enable gzip request encoding using '-DuseGzipFeature=true'. IMPORTANT NOTE: jersey 1.x is no longer actively maintained so please upgrade to 'jersey3' or other HTTP libraries instead.");
supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.9.x");
Expand Down Expand Up @@ -413,6 +416,9 @@ public void processOpts() {
this.setErrorObjectType(additionalProperties.get(ERROR_OBJECT_TYPE).toString());
}
additionalProperties.put(ERROR_OBJECT_TYPE, errorObjectType);
if (additionalProperties.containsKey(WEBCLIENT_BLOCKING_OPERATIONS)) {
this.webclientBlockingOperations = Boolean.parseBoolean(additionalProperties.get(WEBCLIENT_BLOCKING_OPERATIONS).toString());
}

final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
Expand Down Expand Up @@ -817,6 +823,18 @@ public int compare(CodegenParameter one, CodegenParameter another) {
objs = AbstractJavaJAXRSServerCodegen.jaxrsPostProcessOperations(objs);
}

if (WEBCLIENT.equals(getLibrary())) {
OperationMap operations = objs.getOperations();
if (operations != null) {
List<CodegenOperation> ops = operations.getOperation();
for (CodegenOperation operation : ops) {
if (!operation.vendorExtensions.containsKey(VendorExtension.X_WEBCLIENT_BLOCKING.getName()) && webclientBlockingOperations) {
operation.vendorExtensions.put(VendorExtension.X_WEBCLIENT_BLOCKING.getName(), true);
}
}
}
}

return objs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,35 @@ public void testWebClientFormMultipart() throws IOException {
);
}

@Test
public void shouldGenerateBlockingAndNoBlockingOperationsForWebClient() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
properties.put(JavaClientCodegen.WEBCLIENT_BLOCKING_OPERATIONS, true);

File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.WEBCLIENT)
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(configurator.toClientOptInput()).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

JavaFileAssert.assertThat(files.get("StoreApi.java"))
.assertMethod("getInventory").hasReturnType("Mono<Map<String, Integer>>") //explicit 'x-webclient-blocking: false' which overrides global config
.toFileAssert()
.assertMethod("placeOrder").hasReturnType("Order"); // use global config

JavaFileAssert.assertThat(files.get("PetApi.java"))
.assertMethod("findPetsByStatus").hasReturnType("List<Pet>"); // explicit 'x-webclient-blocking: true' which overrides global config
}

@Test
public void testAllowModelWithNoProperties() throws Exception {
File output = Files.createTempDirectory("test").toFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ paths:
summary: Returns pet inventories by status
description: Returns a map of status codes to quantities
operationId: getInventory
x-webclient-blocking: false
responses:
'200':
description: successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ paths:
summary: Returns pet inventories by status
tags:
- store
x-webclient-blocking: false
x-accepts: application/json
/store/order:
post:
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/java/feign/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ paths:
summary: Returns pet inventories by status
tags:
- store
x-webclient-blocking: false
x-accepts: application/json
/store/order:
post:
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/java/native-async/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ paths:
summary: Returns pet inventories by status
tags:
- store
x-webclient-blocking: false
x-accepts: application/json
/store/order:
post:
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/java/native/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ paths:
summary: Returns pet inventories by status
tags:
- store
x-webclient-blocking: false
x-accepts: application/json
/store/order:
post:
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/java/webclient/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ paths:
summary: Returns pet inventories by status
tags:
- store
x-webclient-blocking: false
x-accepts: application/json
/store/order:
post:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ paths:
summary: Returns pet inventories by status
tags:
- store
x-webclient-blocking: false
x-accepts: application/json
/store/order:
post:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ paths:
summary: Returns pet inventories by status
tags:
- store
x-webclient-blocking: false
x-accepts: application/json
/store/order:
post:
Expand Down

0 comments on commit 2524e8f

Please sign in to comment.