Fix issue 8352: [Java][RestTemplate][WebClient] Incorrect handling of free form query parameters (#10428)

* [Java][RestTemplate][WebClient] fix issue #8352 handling of free-form
query parameters with Java

* replace tab by spaces

* commit samples files generated by the generate-samples.sh script
This commit is contained in:
RomainPruvostMHH
2021-09-21 05:40:56 +02:00
committed by GitHub
parent ce30a3f3bc
commit 04e67acd0a
8 changed files with 124 additions and 0 deletions

View File

@@ -479,6 +479,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
collectionFormat = CollectionFormat.CSV;
}
if (value instanceof Map) {
final Map<String, Object> valuesMap = (Map<String, Object>) value;
for (final Entry<String, Object> entry : valuesMap.entrySet()) {
params.add(entry.getKey(), parameterToString(entry.getValue()));
}
return params;
}
Collection<?> valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection<?>) value;

View File

@@ -457,6 +457,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
collectionFormat = CollectionFormat.CSV;
}
if (value instanceof Map) {
final Map<String, Object> valuesMap = (Map<String, Object>) value;
for (final Entry<String, Object> entry : valuesMap.entrySet()) {
params.add(entry.getKey(), parameterToString(entry.getValue()));
}
return params;
}
Collection<?> valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection<?>) value;

View File

@@ -1164,4 +1164,62 @@ public class JavaClientCodegenTest {
"formParams.add(\"file\", file);"
);
}
/**
* See https://github.com/OpenAPITools/openapi-generator/issues/8352
*/
@Test
public void testRestTemplateWithFreeFormInQueryParameters() throws IOException {
final Map<String, Object> properties = new HashMap<>();
properties.put(AbstractJavaCodegen.JAVA8_MODE, true);
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
final File output = Files.createTempDirectory("test")
.toFile();
output.deleteOnExit();
final CodegenConfigurator configurator = new CodegenConfigurator().setGeneratorName("java")
.setLibrary(JavaClientCodegen.RESTTEMPLATE)
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/issue8352.yaml")
.setOutputDir(output.getAbsolutePath()
.replace("\\", "/"));
final DefaultGenerator generator = new DefaultGenerator();
final List<File> files = generator.opts(configurator.toClientOptInput())
.generate();
files.forEach(File::deleteOnExit);
final Path defaultApi = Paths.get(output + "/src/main/java/xyz/abcdef/ApiClient.java");
TestUtils.assertFileContains(defaultApi, "value instanceof Map");
}
/**
* See https://github.com/OpenAPITools/openapi-generator/issues/8352
*/
@Test
public void testWebClientWithFreeFormInQueryParameters() throws IOException {
final Map<String, Object> properties = new HashMap<>();
properties.put(AbstractJavaCodegen.JAVA8_MODE, true);
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
final 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/issue8352.yaml")
.setOutputDir(output.getAbsolutePath()
.replace("\\", "/"));
final DefaultGenerator generator = new DefaultGenerator();
final List<File> files = generator.opts(configurator.toClientOptInput())
.generate();
files.forEach(File::deleteOnExit);
final Path defaultApi = Paths.get(output + "/src/main/java/xyz/abcdef/ApiClient.java");
TestUtils.assertFileContains(defaultApi, "value instanceof Map");
}
}

View File

@@ -0,0 +1,18 @@
openapi: 3.0.0
info:
title: test handle of free form query parameters
version: 0.0.1
servers:
- url: "http://localhost"
paths:
/some/endpoint:
get:
parameters:
- in: "query"
name: "free-form"
schema:
type: "object"
style: "form"
responses:
200:
description: "test"

View File

@@ -435,6 +435,14 @@ public class ApiClient extends JavaTimeFormatter {
collectionFormat = CollectionFormat.CSV;
}
if (value instanceof Map) {
final Map<String, Object> valuesMap = (Map<String, Object>) value;
for (final Entry<String, Object> entry : valuesMap.entrySet()) {
params.add(entry.getKey(), parameterToString(entry.getValue()));
}
return params;
}
Collection<?> valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection<?>) value;

View File

@@ -430,6 +430,14 @@ public class ApiClient extends JavaTimeFormatter {
collectionFormat = CollectionFormat.CSV;
}
if (value instanceof Map) {
final Map<String, Object> valuesMap = (Map<String, Object>) value;
for (final Entry<String, Object> entry : valuesMap.entrySet()) {
params.add(entry.getKey(), parameterToString(entry.getValue()));
}
return params;
}
Collection<?> valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection<?>) value;

View File

@@ -423,6 +423,14 @@ public class ApiClient extends JavaTimeFormatter {
collectionFormat = CollectionFormat.CSV;
}
if (value instanceof Map) {
final Map<String, Object> valuesMap = (Map<String, Object>) value;
for (final Entry<String, Object> entry : valuesMap.entrySet()) {
params.add(entry.getKey(), parameterToString(entry.getValue()));
}
return params;
}
Collection<?> valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection<?>) value;

View File

@@ -444,6 +444,14 @@ public class ApiClient extends JavaTimeFormatter {
collectionFormat = CollectionFormat.CSV;
}
if (value instanceof Map) {
final Map<String, Object> valuesMap = (Map<String, Object>) value;
for (final Entry<String, Object> entry : valuesMap.entrySet()) {
params.add(entry.getKey(), parameterToString(entry.getValue()));
}
return params;
}
Collection<?> valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection<?>) value;