mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-11 02:32:45 +00:00
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:
@@ -479,6 +479,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
|
|||||||
collectionFormat = CollectionFormat.CSV;
|
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;
|
Collection<?> valueCollection = null;
|
||||||
if (value instanceof Collection) {
|
if (value instanceof Collection) {
|
||||||
valueCollection = (Collection<?>) value;
|
valueCollection = (Collection<?>) value;
|
||||||
|
|||||||
@@ -457,6 +457,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
|
|||||||
collectionFormat = CollectionFormat.CSV;
|
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;
|
Collection<?> valueCollection = null;
|
||||||
if (value instanceof Collection) {
|
if (value instanceof Collection) {
|
||||||
valueCollection = (Collection<?>) value;
|
valueCollection = (Collection<?>) value;
|
||||||
|
|||||||
@@ -1164,4 +1164,62 @@ public class JavaClientCodegenTest {
|
|||||||
"formParams.add(\"file\", file);"
|
"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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
@@ -435,6 +435,14 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
collectionFormat = CollectionFormat.CSV;
|
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;
|
Collection<?> valueCollection = null;
|
||||||
if (value instanceof Collection) {
|
if (value instanceof Collection) {
|
||||||
valueCollection = (Collection<?>) value;
|
valueCollection = (Collection<?>) value;
|
||||||
|
|||||||
@@ -430,6 +430,14 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
collectionFormat = CollectionFormat.CSV;
|
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;
|
Collection<?> valueCollection = null;
|
||||||
if (value instanceof Collection) {
|
if (value instanceof Collection) {
|
||||||
valueCollection = (Collection<?>) value;
|
valueCollection = (Collection<?>) value;
|
||||||
|
|||||||
@@ -423,6 +423,14 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
collectionFormat = CollectionFormat.CSV;
|
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;
|
Collection<?> valueCollection = null;
|
||||||
if (value instanceof Collection) {
|
if (value instanceof Collection) {
|
||||||
valueCollection = (Collection<?>) value;
|
valueCollection = (Collection<?>) value;
|
||||||
|
|||||||
@@ -444,6 +444,14 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
collectionFormat = CollectionFormat.CSV;
|
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;
|
Collection<?> valueCollection = null;
|
||||||
if (value instanceof Collection) {
|
if (value instanceof Collection) {
|
||||||
valueCollection = (Collection<?>) value;
|
valueCollection = (Collection<?>) value;
|
||||||
|
|||||||
Reference in New Issue
Block a user