Skip to content

Commit 4fc04b3

Browse files
Add so that the singleRequestParameter can be constructed one parameter at a time
1 parent 2722fdc commit 4fc04b3

9 files changed

Lines changed: 514 additions & 81 deletions

File tree

docs/generators/java-microprofile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
100100
|useRuntimeException|Use RuntimeException instead of Exception. Only jersey2, jersey3, okhttp-gson, vertx, microprofile support this option.| |false|
101101
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
102102
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
103-
|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. Only WebClient supports this option.| |false|
103+
|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|
104104
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
105105
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|
106106
|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
@@ -100,7 +100,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
100100
|useRuntimeException|Use RuntimeException instead of Exception. Only jersey2, jersey3, okhttp-gson, vertx, microprofile support this option.| |false|
101101
|useRxJava2|Whether to use the RxJava2 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
102102
|useRxJava3|Whether to use the RxJava3 adapter with the retrofit2 library. IMPORTANT: This option has been deprecated.| |false|
103-
|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. Only WebClient supports this option.| |false|
103+
|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|
104104
|webclientBlockingOperations|Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync| |false|
105105
|withAWSV4Signature|whether to include AWS v4 signature support (only available for okhttp-gson library)| |false|
106106
|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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public JavaClientCodegen() {
237237
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."));
238238
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC + " Only jersey2, jersey3, native, okhttp-gson support this option."));
239239
cliOptions.add(CliOption.newString(MICROPROFILE_REST_CLIENT_VERSION, "Version of MicroProfile Rest Client API."));
240-
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. Only WebClient supports this option.").defaultValue("false"));
240+
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"));
241241
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));
242242
cliOptions.add(CliOption.newBoolean(GENERATE_CLIENT_AS_BEAN, "For resttemplate, configure whether to create `ApiClient.java` and Apis clients as bean (with `@Component` annotation).", this.generateClientAsBean));
243243
cliOptions.add(CliOption.newBoolean(SUPPORT_URL_QUERY, "Generate toUrlQueryString in POJO (default to true). Available on `native`, `apache-httpclient` libraries."));

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,36 @@ public class {{classname}} {
5656
{{#hasParams}}
5757
{{^hasSingleParam}}
5858

59+
{{^staticRequest}}
5960
public record {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request({{#allParams}}{{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}){}
61+
{{/staticRequest}}
62+
{{#staticRequest}}
63+
@lombok.EqualsAndHashCode
64+
public static class {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {
65+
{{#allParams}}
66+
private final {{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}};
67+
{{/allParams}}
68+
69+
public {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request() {}
70+
71+
public {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request({{#allParams}}{{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) {
72+
{{#allParams}}
73+
this.{{paramName}} = {{paramName}};
74+
{{/allParams}}
75+
}
76+
77+
{{#allParams}}
78+
public {{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}() {
79+
return this.{{paramName}};
80+
}
81+
public {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {{paramName}}({{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}) {
82+
this.{{paramName}} = {{paramName}};
83+
return this;
84+
}
85+
86+
{{/allParams}}
87+
}
88+
{{/staticRequest}}
6089

6190
/**
6291
* {{summary}}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,30 @@ public class {{classname}} {
5454
}
5555

5656
{{#operation}}{{#singleRequestParameter}}{{#hasParams}}{{^hasSingleParam}}
57+
@lombok.EqualsAndHashCode
5758
public {{#staticRequest}}static {{/staticRequest}}class {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {
5859
{{#allParams}}
5960
private final {{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}};
6061
{{/allParams}}
6162

63+
public {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request() {}
64+
6265
public {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request({{#allParams}}{{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) {
63-
{{#allParams}}
64-
this.{{paramName}} = {{paramName}};
65-
{{/allParams}}
66-
}
66+
{{#allParams}}
67+
this.{{paramName}} = {{paramName}};
68+
{{/allParams}}
69+
}
6770

68-
{{#allParams}}
71+
{{#allParams}}
6972
public {{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}() {
7073
return this.{{paramName}};
7174
}
72-
{{/allParams}}
75+
public {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {{paramName}}({{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}) {
76+
this.{{paramName}} = {{paramName}};
77+
return this;
78+
}
79+
80+
{{/allParams}}
7381
}
7482

7583
/**

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,40 @@ public void testRestClientWithUseSingleRequestParameter_issue_19406() {
32063206
);
32073207
}
32083208

3209+
@Test
3210+
public void testRestClientWithUseSingleRequestParameter_static_issue_20668() {
3211+
final Path output = newTempFolder();
3212+
final CodegenConfigurator configurator = new CodegenConfigurator()
3213+
.setGeneratorName("java")
3214+
.setLibrary(JavaClientCodegen.RESTCLIENT)
3215+
.setAdditionalProperties(Map.of(
3216+
CodegenConstants.API_PACKAGE, "xyz.abcdef.api",
3217+
CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, "static"
3218+
))
3219+
.setInputSpec("src/test/resources/3_1/java/petstore.yaml")
3220+
.setOutputDir(output.toString().replace("\\", "/"));
3221+
3222+
new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
3223+
3224+
TestUtils.assertFileContains(
3225+
output.resolve("src/main/java/xyz/abcdef/api/PetApi.java"),
3226+
"public static class DeletePetRequest {",
3227+
"DeletePetRequest(Long petId, String apiKey)",
3228+
"Long petId()",
3229+
"DeletePetRequest petId(Long petId) {",
3230+
"String apiKey()",
3231+
"DeletePetRequest apiKey(String apiKey) {",
3232+
"public void deletePet(DeletePetRequest requestParameters) throws RestClientResponseException {",
3233+
"public ResponseEntity<Void> deletePetWithHttpInfo(DeletePetRequest requestParameters) throws RestClientResponseException {",
3234+
"public ResponseSpec deletePetWithResponseSpec(DeletePetRequest requestParameters) throws RestClientResponseException {",
3235+
"public void deletePet(Long petId, String apiKey) throws RestClientResponseException {",
3236+
"public ResponseEntity<Void> deletePetWithHttpInfo(Long petId, String apiKey) throws RestClientResponseException {",
3237+
"public ResponseSpec deletePetWithResponseSpec(Long petId, String apiKey) throws RestClientResponseException {"
3238+
);
3239+
TestUtils.assertFileNotContains(output.resolve("src/main/java/xyz/abcdef/api/PetApi.java"),
3240+
"public record DeletePetRequest(Long petId, String apiKey){}");
3241+
}
3242+
32093243
@Test
32103244
public void testDuplicatedOperationId() {
32113245
final Path output = newTempFolder();
@@ -3243,7 +3277,8 @@ public void testDuplicatedOperationId() {
32433277
);
32443278
}
32453279

3246-
@Test public void testWebClientWithUseSingleRequestParameter_issue_19407() {
3280+
@Test
3281+
public void testWebClientWithUseSingleRequestParameter_issue_19407() {
32473282
final Path output = newTempFolder();
32483283
final CodegenConfigurator configurator = new CodegenConfigurator()
32493284
.setGeneratorName("java")
@@ -3301,6 +3336,38 @@ public void testDuplicatedOperationId() {
33013336
);
33023337
}
33033338

3339+
@Test
3340+
public void testWebClientWithUseSingleRequestParameter_static_issue_20668() {
3341+
final Path output = newTempFolder();
3342+
final CodegenConfigurator configurator = new CodegenConfigurator()
3343+
.setGeneratorName("java")
3344+
.setLibrary(JavaClientCodegen.WEBCLIENT)
3345+
.setAdditionalProperties(Map.of(
3346+
CodegenConstants.API_PACKAGE, "xyz.abcdef.api",
3347+
CodegenConstants.USE_SINGLE_REQUEST_PARAMETER, "static"
3348+
))
3349+
.setInputSpec("src/test/resources/3_1/java/petstore.yaml")
3350+
.setOutputDir(output.toString().replace("\\", "/"));
3351+
3352+
new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
3353+
3354+
TestUtils.assertFileContains(
3355+
output.resolve("src/main/java/xyz/abcdef/api/PetApi.java"),
3356+
"public static class DeletePetRequest {",
3357+
"DeletePetRequest(Long petId, String apiKey)",
3358+
"Long petId()",
3359+
"DeletePetRequest petId(Long petId) {",
3360+
"String apiKey()",
3361+
"DeletePetRequest apiKey(String apiKey) {",
3362+
"public Mono<Void> deletePet(DeletePetRequest requestParameters) throws WebClientResponseException {",
3363+
"public Mono<ResponseEntity<Void>> deletePetWithHttpInfo(DeletePetRequest requestParameters) throws WebClientResponseException {",
3364+
"public ResponseSpec deletePetWithResponseSpec(DeletePetRequest requestParameters) throws WebClientResponseException {",
3365+
"public Mono<Void> deletePet(Long petId, String apiKey) throws WebClientResponseException {",
3366+
"public Mono<ResponseEntity<Void>> deletePetWithHttpInfo(Long petId, String apiKey) throws WebClientResponseException {",
3367+
"public ResponseSpec deletePetWithResponseSpec(Long petId, String apiKey) throws WebClientResponseException {"
3368+
);
3369+
}
3370+
33043371
@Test
33053372
public void testGenerateParameterId() {
33063373
final Path output = newTempFolder();

0 commit comments

Comments
 (0)