Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -846,14 +846,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4211,4 +4211,30 @@ public void testFeignHc5CustomTemplateDirIsPreserved() {
"feign-hc5 must preserve a user-provided templateDir and not overwrite it with 'feign'");
}

@Test(description = "Regression test for multipart/form-data list handling in restclient ApiClient to avoid IndexOutOfBoundsException on empty lists")
public void testRestClientMultipartFormParamsGuardAgainstEmptyLists() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(JAVA_GENERATOR)
.setLibrary(JavaClientCodegen.RESTCLIENT)
.setAdditionalProperties(Map.of(CodegenConstants.API_PACKAGE, "xyz.abcdef.api"))
.setInputSpec("src/test/resources/3_0/form-multipart-binary-array.yaml")
.setOutputDir(output.toString().replace("\\", "/"));

List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();

validateJavaSourceFiles(files);

assertFileContains(
output.resolve("src/main/java/xyz/abcdef/ApiClient.java"),
"if (v instanceof java.util.ArrayList && !v.isEmpty()) {",
"first.getClass().isEnum()"
);

TestUtils.assertFileNotContains(
output.resolve("src/main/java/xyz/abcdef/ApiClient.java"),
"if (v instanceof java.util.ArrayList) {",
"o.getClass().getEnumConstants() != null"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -724,14 +724,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,14 +723,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,14 +748,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,14 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth

if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
formParams.forEach(
(k, v) -> {
if (v instanceof java.util.ArrayList) {
Object o = v.get(0);
if (o != null && o.getClass().getEnumConstants() != null) {
v.set(0, o.toString());
}
(k, v) -> {
if (v instanceof java.util.ArrayList && !v.isEmpty()) {
Object first = v.get(0);
if (first != null && first.getClass().isEnum()) {
v.set(0, first.toString());
}
});
}
});
}

var selectedBody = selectBody(body, formParams, contentType);
Expand Down
Loading