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][Spring] multiple files upload #4803

Closed
5 of 6 tasks
purple-dragon opened this issue Dec 16, 2019 · 9 comments
Closed
5 of 6 tasks

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

purple-dragon opened this issue Dec 16, 2019 · 9 comments

Comments

@purple-dragon
Copy link

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.

#2210
ga4gh/workflow-execution-service-schemas#43

Suggest a fix
@surajgautam
Copy link

How did you fix it? I am also facing the same issue. :(

@purple-dragon
Copy link
Author

@surajgautam no solution for now :(

@surajgautam
Copy link

Hi there,

I managed to generate MultipartFile[] files by using:

content:
multipart/form-data:
schema:
type: object
properties:
files[]:
type: array
items:
type: string
format: binary

@ericdariel
Copy link

Hello,

I have the same problem, impossible to generate a MultipartFile[] in Spring Boot with openapi-generator 4.2.2

Anyone knows how to do in Open API v3 ?

@purple-dragon
Copy link
Author

@ericdariel pls check @surajgautam commets

@jorgerod
Copy link
Contributor

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

wing328 pushed a commit that referenced this issue Jul 2, 2020
* gh-4803: Fix bug java client multiple files upload

* gh-4803: Fix bug java client multiple files upload

* gh-4803: Fix bug java client multiple files upload

* gh-4803: Fix bug java client multiple files upload

* gh-4803: Fix bug java client multiple files upload
@wing328 wing328 closed this as completed Jul 2, 2020
jimschubert added a commit that referenced this issue Jul 3, 2020
* master: (142 commits)
  update python samples
  clarify direction of py client side validation flag (#6850)
  fix erronous cmd arg example for docker in readme (#6846)
  [BUG] [JAVA] Fix multiple files upload (#4803) (#6808)
  [kotlin][client] fix retrofit dependencies (#6836)
  [PowerShell] add more fields to be customized (#6835)
  [Java][WebClient]remove the dead code from java ApiClient.mustache (#6556)
  [PHP] Better handling of invalid data (array) (#6760)
  Make ApiClient in retrofit2 be able to use own OkHttpClient (#6699)
  mark python2 support in flask as deprecated (#6653)
  update samples
  [Java][jersey2] Add a getter for the User-Agent header value (#6831)
  Provides a default nil value for optional init parameters (#6827)
  [Java] Deprecate feignVersion option (#6824)
  [R] Enum R6Class Support, closes #3367 (#5728)
  [Rust][Client] Unify sync/async client structure (#6753)
  [php-ze-ph] Set required PHP version to ^7.2 (#6763)
  [Java][client][native][Gradle] Add missing jackson-databind-nullable (#6802)
  Improve sttpOpenApiClient generator (#6684)
  Update docker-tag-latest-release.yml
  ...
@dvdvdmt
Copy link

dvdvdmt commented 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?

@tobiashochguertel
Copy link

tobiashochguertel commented Jun 11, 2023

This issue isn't yet solved @wing328 it's still not working for the Spring generator.

          multipart/form-data:
            schema:
              type: object
              properties:
                files:
                  type: array
                  items:
                    type: string
                    format: binary

grafik

@afjord
Copy link

afjord commented Aug 2, 2023

@tobiashochguertel In my case this problem disappeared when I removed delegatePattern option.

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

8 participants