Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][JAVA] Wrong request body created for array of files upload #2210

Open
wshands opened this issue Feb 21, 2019 · 1 comment
Open

[BUG][JAVA] Wrong request body created for array of files upload #2210

wshands opened this issue Feb 21, 2019 · 1 comment

Comments

@wshands
Copy link

wshands commented Feb 21, 2019

Java code generated for request body for multi-part form data does not put the content of the files in request body, instead it places the file path and name in the request body.

WGET OpenAPI generator version 3.3.4 via instructions on Github README
OpenAPI yaml openapi-upload-files.yaml
openapi: 3.0.0
info:
  title: OpenApi file upload request body
  version: "1.0.0"
  contact:
    name: Walt Shands
    email: jshands@ucsc.edu
tags:
        - name: upload_test
          description: test multipart upload
paths:
  /runs:
    post:
      summary: Upload an array of files.
      description: >-
        This endpoint uploads an array of files.

      operationId: UploadFiles
      responses:
        '200':
          description: ''
        '400':
          description: The request is malformed.
        '401':
          description: The request is unauthorized.
        '403':
          description: The requester is not authorized to perform this action.
        '500':
          description: An unexpected error occurred.

      tags:
        - UploadFilesService
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file_list:
                  type: array
                  items:
                    type: string
                    format: binary
Build client from OpenApi yaml with OpenAPI generator
java -jar <path>/openapi-generator-cli.jar generate -l java -i <path>/openapi-upload-files.yaml
Java class used for generation of POST request
package io.arrayoffiles;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.openapitools.client.ApiClient;
import org.openapitools.client.ApiException;
import org.openapitools.client.api.UploadFilesServiceApi;

public class PostArrayOfFiles {
    public static void main(String[] args) {
        UploadFilesServiceApi apiInstance = new UploadFilesServiceApi();
        try {
            ApiClient apiClient = apiInstance.getApiClient();
            //apiClient.setBasePath("http://0.0.0.0:8080");
            apiClient.setBasePath("http://swagger.io");

            List<File> uploadFileList = new ArrayList<File>();
            File testfile = new File("Users/waltershands/junk/test1.txt");
            uploadFileList.add(testfile);
            testfile = new File("Users/waltershands/junk/test2.txt");
            uploadFileList.add(testfile);

            apiInstance.uploadFiles(uploadFileList);
        } catch (ApiException var4) {
            System.err.println("Exception when calling uploadFileList");
            var4.printStackTrace();
        }
    }
}
Compile test program
javac -cp <path to openapi generated client>/target/openapi-java-client-1.0.0.jar <path>/src/main/java/io/arrayoffiles/PostArrayOfFiles.java 
Monitor POST requests in another terminal window
sudo tcpdump -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
Run test program
java -debug -cp <path to PostArrayOfFiles class>/target/classes:<path to openapi generated client>target/*:<path to other needed jars>/target/lib/*  io.arrayoffiles.PostArrayOfFiles
Produces this POST request body (seen in tcpdump window):
...
User-Agent: OpenAPI-Generator/1.0.0/java
Content-Type: multipart/form-data; boundary=616f0c35-868f-443c-b8f7-676d25a52c4b
Content-Length: 223
Host: swagger.io
Connection: Keep-Alive
Accept-Encoding: gzip

--616f0c35-868f-443c-b8f7-676d25a52c4b
Content-Disposition: form-data; name="file_list"
Content-Length: 67

Users/waltershands/junk/test1.txt,Users/waltershands/junk/test2.txt
--616f0c35-868f-443c-b8f7-676d25a52c4b--
...

There should have been two bodies with Content-Disposition that included a 'filename="test<#>.txt" and the contents of the file

ApiClient.java creates a correct body only if the instance of the formParams Object is a File, but in the test case it is a List of Files and so the correct body is not created.

@auto-labeler
Copy link

auto-labeler bot commented Feb 21, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant