question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][Java][Spring] multiple files upload

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What’s the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

use open api spec 3 to define api to upload multiple files, but openapi generator generate incorrect, following openapi spec

https://swagger.io/specification/#mediaTypeObject section: “To upload multiple files, a multipart media type MUST be used:” image

default ResponseEntity<Document> createDocument(@ApiParam(value = "file detail") @Valid @RequestPart("file") MultipartFile file,@ApiParam(value = "") @RequestParam(value="metadata", required=false)  Object metadata)
should be array: MultipartFile[] file

default ResponseEntity<Document> createDocument(@ApiParam(value = "file detail") @Valid @RequestPart("file") MultipartFile[] file,@ApiParam(value = "") @RequestParam(value="metadata", required=false)  Object metadata)
openapi-generator version
    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator</artifactId>
        <version>4.2.2</version>
    </dependency>
OpenAPI declaration file content or url
openapi: "3.0.2"
info:
  version: "1.0.0"
  title: "File Management API"
  description: >-
    File Management

paths:
  /api/v1/documents:
    post:
      tags:
        - Manage Files
      summary: "Upload new file"
      operationId: "createDocument"
      requestBody:
        content:
          multipart/form-data:
            schema:
              properties:
                file:
                  type: array
                  items:
                    type: string
                    format: binary
                metadata:
                  type: object
      responses:
        201:
          description: "Document created, return generated document information"
        404:
          description: "Not Found"
        409:
          description: "Conflict"
        500:
          description: "Internal Server Error"
Command line used for generation
Steps to reproduce
Related issues/PRs

looks like there are similar issues, not sure if it fixed or not.

https://github.com/OpenAPITools/openapi-generator/issues/2210 https://github.com/ga4gh/workflow-execution-service-schemas/issues/43

Suggest a fix

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dvdvdmtcommented, Jul 6, 2020

How can I solve the same problem with multiple files upload for typescript-axios generator? I tried @openapitools/openapi-generator-cli@cli-5.0.0-beta on this scheme:

...
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              required:
                - files
              properties:
                files:
                  type: array
                  items: { type: string, format: binary }
              type: object
...

It don’t take into account the array of files and generates this code:

     const localVarFormParams = new FormData();

      if (files) {
        localVarFormParams.append('files', files.join(COLLECTION_FORMATS.csv));
      }

It would be great if it produce something like:

     const localVarFormParams = new FormData();

      if (files) {
        files.forEach((file) => {
          localVarFormParams.append('files[]', file)
        });
      }

Or maybe I’m doing something wrong?

0reactions
jorgerodcommented, Jun 26, 2020

Hello,

I have the same problem. Java client not work for me.

I used the previously defined file

    public ResponseEntity<Void> createDocumentWithHttpInfo(List<File> file, Object metadata) throws RestClientException {
        Object postBody = null;
        
        String path = apiClient.expandPath("/api/v1/documents", Collections.<String, Object>emptyMap());

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<String, String>();
        final MultiValueMap formParams = new LinkedMultiValueMap();

        if (file != null)
             //Not compile. FileSystemResource constructor wait a File, not a List<File>
            formParams.put("file", new FileSystemResource(file));
        if (metadata != null)
            formParams.add("metadata", metadata);

        final String[] accepts = { };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { 
            "multipart/form-data"
        };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] {  };

        ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {};
        return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
    }

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to upload multiple files in Java Spring Boot - BezKoder
In this tutorial, I will show you how to upload multiple files and download with a Spring Boot Rest APIs. We also use...
Read more >
Uploading multiple files does not work in spring boot controller
I can upload one file to a spring boot ...
Read more >
Uploading Multiple Files with Spring Boot - Daily Code Buffer
Multipart-file requests break large files into smaller chunks which makes it efficient for file uploads. More information about it can be found here....
Read more >
Upload Multiple Files in Spring Boot using JPA, Thymeleaf ...
In this article, we will learn how to upload multiple files to the server with the help of Spring boot. So to make...
Read more >
Spring Boot Upload Multiple Files Example - CodeJava.net
In this post, I will share some code examples demonstrating uploading multiple files in a Java web application based on Spring Boot.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found