Skip to content

[BUG][Java][RestClient] IndexOutOfBoundsException when sending empty List in multipart/form-data request #23153

@flabbamann

Description

@flabbamann

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I generate RestClient code to upload a json body and optional file attachments. Everything works as expected if I add files, but I get an IndexOutOfBoundsException when the list of attachments is empty.

openapi-generator version

7.20.0

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  title: Test-Service
  description: Demostrate a bug with empty multipart-array
  version: 1.0.0
paths:
  /upload:    
    post:
      summary: Crashes when sending an empty array of files
      operationId: postUpload
      requestBody:
        required: true
        content:
          'multipart/form-data':
            schema:              
              required: [document]
              properties:
                document:
                  type: object
                  properties:
                    text1:
                      type: string
                    text2:
                      type: string        
                image:
                  type: array
                  minItems: 0
                  maxItems: 15
                  items:
                    type: string
                    format: binary
      responses:
        "200":
          description: Upload successful
Generation Details
          <execution>
            <id>generate-test-client</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/openapi-test/test-service.yaml</inputSpec>
              <generatorName>java</generatorName>
              <library>restclient</library>
              <modelNamePrefix>Test</modelNamePrefix>
              <configOptions>
                <generateClientAsBean>true</generateClientAsBean>
                <useAbstractionForFiles>true</useAbstractionForFiles>
                <useJakartaEe>true</useJakartaEe>
              </configOptions>
              <globalProperties>
                <skipFormModel>false</skipFormModel>
              </globalProperties>
            </configuration>
          </execution>
Steps to reproduce
  @Test
  void test01() {

    DefaultApi api = new DefaultApi();

    TestPostUploadRequestDocument document = new TestPostUploadRequestDocument().text1("Foo").text2("bar");

    List<AbstractResource> list = new ArrayList<>();

    api.postUpload(document, list);
  }
Related issues/PRs
Suggest a fix

In the generated ApiClient class, inside the prepareRequest method there is this code:

        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());
                            }
                        }
                    });
        }

We get an IndexOutOfBoundsException in this line if the array is empty:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions