Skip to content

Commit 60bae73

Browse files
SingleRequestParameter setter parameter construction (#20679)
* Add so that the singleRequestParameter can be constructed one parameter at a time * Replace the lombok EqualsAndHashcode with a mustache implementation * Remove equals and hashcode from oneRequestParameter model * Remove the final keyword from variables in the oneRequestParameter class
1 parent 275107e commit 60bae73

File tree

9 files changed

+558
-140
lines changed

9 files changed

+558
-140
lines changed

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
@@ -236,7 +236,7 @@ public JavaClientCodegen() {
236236
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."));
237237
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC + " Only jersey2, jersey3, native, okhttp-gson support this option."));
238238
cliOptions.add(CliOption.newString(MICROPROFILE_REST_CLIENT_VERSION, "Version of MicroProfile Rest Client API."));
239-
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"));
239+
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"));
240240
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));
241241
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));
242242
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,35 @@ 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+
public static class {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {
64+
{{#allParams}}
65+
private {{#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}};
66+
{{/allParams}}
67+
68+
public {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request() {}
69+
70+
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}}) {
71+
{{#allParams}}
72+
this.{{paramName}} = {{paramName}};
73+
{{/allParams}}
74+
}
75+
76+
{{#allParams}}
77+
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}}() {
78+
return this.{{paramName}};
79+
}
80+
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}}) {
81+
this.{{paramName}} = {{paramName}};
82+
return this;
83+
}
84+
85+
{{/allParams}}
86+
}
87+
{{/staticRequest}}
6088

6189
/**
6290
* {{summary}}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,27 @@ public class {{classname}} {
5656
{{#operation}}{{#singleRequestParameter}}{{#hasParams}}{{^hasSingleParam}}
5757
public {{#staticRequest}}static {{/staticRequest}}class {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request {
5858
{{#allParams}}
59-
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}};
59+
private {{#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}};
6060
{{/allParams}}
6161

62+
public {{#lambda.titlecase}}{{operationId}}{{/lambda.titlecase}}Request() {}
63+
6264
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-
}
65+
{{#allParams}}
66+
this.{{paramName}} = {{paramName}};
67+
{{/allParams}}
68+
}
6769

68-
{{#allParams}}
70+
{{#allParams}}
6971
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}}() {
7072
return this.{{paramName}};
7173
}
72-
{{/allParams}}
74+
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}}) {
75+
this.{{paramName}} = {{paramName}};
76+
return this;
77+
}
78+
79+
{{/allParams}}
7380
}
7481

7582
/**

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
@@ -3207,6 +3207,40 @@ public void testRestClientWithUseSingleRequestParameter_issue_19406() {
32073207
);
32083208
}
32093209

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

3247-
@Test public void testWebClientWithUseSingleRequestParameter_issue_19407() {
3281+
@Test
3282+
public void testWebClientWithUseSingleRequestParameter_issue_19407() {
32483283
final Path output = newTempFolder();
32493284
final CodegenConfigurator configurator = new CodegenConfigurator()
32503285
.setGeneratorName("java")
@@ -3302,6 +3337,38 @@ public void testDuplicatedOperationId() {
33023337
);
33033338
}
33043339

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

0 commit comments

Comments
 (0)