Skip to content

Commit 5997acb

Browse files
authored
feat (JAVA NATIVE): add support for useSingleRequestParameter to java native client (#21331)
1 parent bce88c9 commit 5997acb

File tree

9 files changed

+64
-30
lines changed

9 files changed

+64
-30
lines changed

docs/generators/java-microprofile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
101101
|useRuntimeException|Use RuntimeException instead of Exception. Only jersey2, jersey3, okhttp-gson, vertx, microprofile support this option.| |false|
102102
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
103103
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
104-
|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, 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|
104+
|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|
105105
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
106106
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|
107107
|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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
101101
|useRuntimeException|Use RuntimeException instead of Exception. Only jersey2, jersey3, okhttp-gson, vertx, microprofile support this option.| |false|
102102
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
103103
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
104-
|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, 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|
104+
|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|
105105
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
106106
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|
107107
|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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public JavaClientCodegen() {
239239
cliOptions.add(CliOption.newString(CONFIG_KEY_FROM_CLASS_NAME, "If true, set tag as key in @RegisterRestClient. Default to false. Only `microprofile` supports this option."));
240240
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC + " Only jersey2, jersey3, native, okhttp-gson support this option."));
241241
cliOptions.add(CliOption.newString(MICROPROFILE_REST_CLIENT_VERSION, "Version of MicroProfile Rest Client API."));
242-
cliOptions.add(CliOption.newString(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, 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.").defaultValue("false"));
242+
cliOptions.add(CliOption.newString(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 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.").defaultValue("false"));
243243
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));
244244
cliOptions.add(CliOption.newBoolean(GENERATE_CLIENT_AS_BEAN, "For resttemplate, restclient and webclient, configure whether to create `ApiClient.java` and Apis clients as bean (with `@Component` annotation).", this.generateClientAsBean));
245245
cliOptions.add(CliOption.newBoolean(SUPPORT_URL_QUERY, "Generate toUrlQueryString in POJO (default to true). Available on `native`, `apache-httpclient` libraries."));
@@ -800,7 +800,7 @@ public void processOpts() {
800800
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
801801
super.postProcessOperationsWithModels(objs, allModels);
802802

803-
if (this.getSingleRequestParameter() && (isLibrary(JERSEY2) || isLibrary(JERSEY3) || isLibrary(OKHTTP_GSON))) {
803+
if (this.getSingleRequestParameter() && (isLibrary(JERSEY2) || isLibrary(JERSEY3) || isLibrary(OKHTTP_GSON) || isLibrary(NATIVE))) {
804804
// loop through operations to set x-group-parameters extension to true if useSingleRequestParameter option is enabled
805805
OperationMap operations = objs.getOperations();
806806
if (operations != null) {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public class {{classname}} {
106106
/**
107107
* {{summary}}
108108
* {{notes}}
109-
* @param apiRequest {@link API{{operationId}}Request}
109+
* @param apiRequest {@link API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request}
110110
{{#returnType}}
111111
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}{{returnType}}{{#asyncNative}}&gt;{{/asyncNative}}
112112
{{/returnType}}
@@ -127,7 +127,7 @@ public class {{classname}} {
127127
{{#isDeprecated}}
128128
@Deprecated
129129
{{/isDeprecated}}
130-
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}(API{{operationId}}Request apiRequest) throws ApiException {
130+
public {{#returnType}}{{#asyncNative}}CompletableFuture<{{{returnType}}}>{{/asyncNative}}{{^asyncNative}}{{{returnType}}}{{/asyncNative}}{{/returnType}}{{^returnType}}{{#asyncNative}}CompletableFuture<Void>{{/asyncNative}}{{^asyncNative}}void{{/asyncNative}}{{/returnType}} {{operationId}}(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest) throws ApiException {
131131
{{#allParams}}
132132
{{>nullable_var_annotations}}
133133
{{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
@@ -138,7 +138,7 @@ public class {{classname}} {
138138
/**
139139
* {{summary}}
140140
* {{notes}}
141-
* @param apiRequest {@link API{{operationId}}Request}
141+
* @param apiRequest {@link API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request}
142142
* @return {{#asyncNative}}CompletableFuture&lt;{{/asyncNative}}ApiResponse&lt;{{returnType}}{{^returnType}}Void{{/returnType}}&gt;{{#asyncNative}}&gt;{{/asyncNative}}
143143
* @throws ApiException if fails to make API call
144144
{{#isDeprecated}}
@@ -152,7 +152,7 @@ public class {{classname}} {
152152
{{#isDeprecated}}
153153
@Deprecated
154154
{{/isDeprecated}}
155-
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo(API{{operationId}}Request apiRequest) throws ApiException {
155+
public {{#asyncNative}}CompletableFuture<{{/asyncNative}}ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>{{#asyncNative}}>{{/asyncNative}} {{operationId}}WithHttpInfo(API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request apiRequest) throws ApiException {
156156
{{#allParams}}
157157
{{{dataType}}} {{paramName}} = apiRequest.{{paramName}}();
158158
{{/allParams}}
@@ -574,7 +574,7 @@ public class {{classname}} {
574574
{{#vendorExtensions.x-group-parameters}}
575575
{{#hasParams}}
576576

577-
public static final class API{{operationId}}Request {
577+
public static final class API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {
578578
{{#requiredParams}}
579579
{{>nullable_var_annotations}}
580580
private {{{dataType}}} {{paramName}}; // {{description}} (required)
@@ -584,7 +584,7 @@ public class {{classname}} {
584584
private {{{dataType}}} {{paramName}}; // {{description}} (optional{{^isContainer}}{{#defaultValue}}, default to {{.}}{{/defaultValue}}{{/isContainer}})
585585
{{/optionalParams}}
586586

587-
private API{{operationId}}Request(Builder builder) {
587+
private API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request(Builder builder) {
588588
{{#requiredParams}}
589589
this.{{paramName}} = builder.{{paramName}};
590590
{{/requiredParams}}
@@ -616,8 +616,8 @@ public class {{classname}} {
616616
return this;
617617
}
618618
{{/allParams}}
619-
public API{{operationId}}Request build() {
620-
return new API{{operationId}}Request(this);
619+
public API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request build() {
620+
return new API{{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request(this);
621621
}
622622
}
623623
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,6 +3237,7 @@ public void testRestClientWithUseSingleRequestParameter_static_issue_20668() {
32373237
"String apiKey()",
32383238
"DeletePetRequest apiKey(@jakarta.annotation.Nullable String apiKey) {",
32393239
"public void deletePet(DeletePetRequest requestParameters) throws RestClientResponseException {",
3240+
"Pet getPetById(@jakarta.annotation.Nonnull Long petId) throws RestClientResponseException",
32403241
"public ResponseEntity<Void> deletePetWithHttpInfo(DeletePetRequest requestParameters) throws RestClientResponseException {",
32413242
"public ResponseSpec deletePetWithResponseSpec(DeletePetRequest requestParameters) throws RestClientResponseException {",
32423243
"public void deletePet(@jakarta.annotation.Nonnull Long petId, @jakarta.annotation.Nullable String apiKey) throws RestClientResponseException {",
@@ -3665,4 +3666,37 @@ public void visit(MethodCallExpr n, Void arg) {
36653666
assertTrue(defaultFields.get("testNullableEmptyReference").getVariable(0).getInitializer().get().isObjectCreationExpr());
36663667
assertTrue(defaultFields.get("testNullableComplexReference").getVariable(0).getInitializer().get().isMethodCallExpr());
36673668
}
3669+
3670+
@Test
3671+
public void testNativeClientWithUseSingleRequestParameter() {
3672+
final Path output = newTempFolder();
3673+
final CodegenConfigurator configurator = new CodegenConfigurator()
3674+
.setGeneratorName("java")
3675+
.setLibrary(NATIVE)
3676+
.setAdditionalProperties(Map.of(
3677+
CodegenConstants.API_PACKAGE, "xyz.abcdef.api",
3678+
CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, "true"
3679+
))
3680+
.setInputSpec("src/test/resources/3_1/java/petstore.yaml")
3681+
.setOutputDir(output.toString().replace("\\", "/"));
3682+
3683+
new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
3684+
3685+
TestUtils.assertFileContains(
3686+
output.resolve("src/main/java/xyz/abcdef/api/PetApi.java"),
3687+
"public static final class APIDeletePetRequest {",
3688+
"private APIDeletePetRequest(Builder builder) {",
3689+
"public Builder petId(@javax.annotation.Nonnull Long petId) {",
3690+
"public Builder apiKey(@javax.annotation.Nullable String apiKey) {",
3691+
"Long petId()",
3692+
"String apiKey()",
3693+
"public void deletePet(APIDeletePetRequest apiRequest) throws ApiException {",
3694+
"Pet getPetById(@javax.annotation.Nonnull Long petId) throws ApiException",
3695+
"public ApiResponse<Void> deletePetWithHttpInfo(APIDeletePetRequest apiRequest) throws ApiException {",
3696+
"public void deletePet(@javax.annotation.Nonnull Long petId, @javax.annotation.Nullable String apiKey) throws ApiException {",
3697+
"public ApiResponse<Void> deletePetWithHttpInfo(@javax.annotation.Nonnull Long petId, @javax.annotation.Nullable String apiKey) throws ApiException {"
3698+
);
3699+
TestUtils.assertFileNotContains(output.resolve("src/main/java/xyz/abcdef/api/PetApi.java"),
3700+
"public record DeletePetRequest(Long petId, String apiKey){}");
3701+
}
36683702
}

samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,11 +1501,11 @@ private HttpRequest.Builder testEnumParametersRequestBuilder(@javax.annotation.N
15011501
/**
15021502
* Fake endpoint to test group parameters (optional)
15031503
* Fake endpoint to test group parameters (optional)
1504-
* @param apiRequest {@link APItestGroupParametersRequest}
1504+
* @param apiRequest {@link APITestGroupParametersRequest}
15051505
* @return CompletableFuture&lt;Void&gt;
15061506
* @throws ApiException if fails to make API call
15071507
*/
1508-
public CompletableFuture<Void> testGroupParameters(APItestGroupParametersRequest apiRequest) throws ApiException {
1508+
public CompletableFuture<Void> testGroupParameters(APITestGroupParametersRequest apiRequest) throws ApiException {
15091509
@javax.annotation.Nonnull
15101510
Integer requiredStringGroup = apiRequest.requiredStringGroup();
15111511
@javax.annotation.Nonnull
@@ -1524,11 +1524,11 @@ public CompletableFuture<Void> testGroupParameters(APItestGroupParametersRequest
15241524
/**
15251525
* Fake endpoint to test group parameters (optional)
15261526
* Fake endpoint to test group parameters (optional)
1527-
* @param apiRequest {@link APItestGroupParametersRequest}
1527+
* @param apiRequest {@link APITestGroupParametersRequest}
15281528
* @return CompletableFuture&lt;ApiResponse&lt;Void&gt;&gt;
15291529
* @throws ApiException if fails to make API call
15301530
*/
1531-
public CompletableFuture<ApiResponse<Void>> testGroupParametersWithHttpInfo(APItestGroupParametersRequest apiRequest) throws ApiException {
1531+
public CompletableFuture<ApiResponse<Void>> testGroupParametersWithHttpInfo(APITestGroupParametersRequest apiRequest) throws ApiException {
15321532
Integer requiredStringGroup = apiRequest.requiredStringGroup();
15331533
Boolean requiredBooleanGroup = apiRequest.requiredBooleanGroup();
15341534
Long requiredInt64Group = apiRequest.requiredInt64Group();
@@ -1662,7 +1662,7 @@ private HttpRequest.Builder testGroupParametersRequestBuilder(@javax.annotation.
16621662
}
16631663

16641664

1665-
public static final class APItestGroupParametersRequest {
1665+
public static final class APITestGroupParametersRequest {
16661666
@javax.annotation.Nonnull
16671667
private Integer requiredStringGroup; // Required String in group parameters (required)
16681668
@javax.annotation.Nonnull
@@ -1676,7 +1676,7 @@ public static final class APItestGroupParametersRequest {
16761676
@javax.annotation.Nullable
16771677
private Long int64Group; // Integer in group parameters (optional)
16781678

1679-
private APItestGroupParametersRequest(Builder builder) {
1679+
private APITestGroupParametersRequest(Builder builder) {
16801680
this.requiredStringGroup = builder.requiredStringGroup;
16811681
this.requiredBooleanGroup = builder.requiredBooleanGroup;
16821682
this.requiredInt64Group = builder.requiredInt64Group;
@@ -1744,8 +1744,8 @@ public Builder int64Group(@javax.annotation.Nullable Long int64Group) {
17441744
this.int64Group = int64Group;
17451745
return this;
17461746
}
1747-
public APItestGroupParametersRequest build() {
1748-
return new APItestGroupParametersRequest(this);
1747+
public APITestGroupParametersRequest build() {
1748+
return new APITestGroupParametersRequest(this);
17491749
}
17501750
}
17511751
}

samples/client/petstore/java/native-async/src/test/java/org/openapitools/client/api/FakeApiTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public void testGroupParametersTest() throws ApiException {
318318
Boolean booleanGroup = null;
319319
Long int64Group = null;
320320

321-
FakeApi.APItestGroupParametersRequest request = FakeApi.APItestGroupParametersRequest.newBuilder()
321+
FakeApi.APITestGroupParametersRequest request = FakeApi.APITestGroupParametersRequest.newBuilder()
322322
.requiredStringGroup(requiredStringGroup)
323323
.requiredBooleanGroup(requiredBooleanGroup)
324324
.requiredInt64Group(requiredInt64Group)

0 commit comments

Comments
 (0)