-
Notifications
You must be signed in to change notification settings - Fork 9k
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
OAS 3: Support for uploading an array of files in multipart requests #4600
Comments
I'm having the same issue here on swaggerhub. |
Works for me actually. |
@cedric-c84-eu It looks like file array upload is not implemented yet. The file inputs are displayed now, but the actual request contains the string
|
I dd in laravel the result of this and get ...characters">[object File],[object File]"... |
same as @malutanpetronel |
It is failing in curlify.js if (v instanceof win.File) {
curlified.push( `"${k}=@${v.name}${v.type ? `;type=${v.type}` : ""}"` )
} else {
curlified.push( `"${k}=${v}"` )
} curlify is not taking the array into account |
probably it is better to take a blob |
Is there any news??? |
Hello is there any progress ? its opened more than year ... |
As always, PRs are welcome. |
It will be more than welcome when it is ready |
I did everything following documentation and it did work and did send files like this:
However, my backend app (Node.js and Multer for uploading files) was unable to see it. I tried to upload files with javascript using FormData class like this:
And it worked as expected. I'm not sure if this header swagger ui sends is right or wrong but I think this is the reason. Sending single files works perfectly |
same issue .... I'm stuck on this ...did anyone find any solution ?? |
Hi, any update on this? |
Any news? |
Please follow our contribution guidelines about voting to see how to make a more effective impact. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Same problem. I get "[object File],[object File]" instead files |
Same issue for me. Single file upload works perfectly ! |
Same here... |
same here |
Any news? |
Any news? Single file upload works. Multiple file upload does not. package com.riskcontrollimited.ratingsengine.rest.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.Callable;
import static java.util.Objects.requireNonNull;
@RestController
@RequestMapping(value = "/fileupload")
@PropertySource("classpath:application.properties")
public class FileUploadController {
private final File uploadDirRoot;
private final UserService userService;
private final HttpServletRequest request;
public FileUploadController(@Value("${file.upload.dir}") String uploadDir,
final UserService userService,
final HttpServletRequest request) {
this.uploadDirRoot = new File(uploadDir);
this.userService = userService;
this.request = request;
}
@Operation(
summary = "Upload a single file.",
description = "Upload a single file.", tags = {"fileupload"},
requestBody = @RequestBody(content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE))
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "successful operation",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = MessageResponse.class)))),
@ApiResponse(responseCode = "404", description = "entity not found",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiError.class)))),
@ApiResponse(responseCode = "403", description = "forbidden",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiError.class)))),
@ApiResponse(responseCode = "500", description = "internal server error",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiError.class))))})
@PostMapping(
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
Callable<ResponseEntity<MessageResponse>> uploadFile(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
final Optional<String> token = WebUtil.getTokenFromRequest(request);
return () -> token
.map(this.userService::getUserFromToken)
.map(user -> this.uploadUserFiles(user, new MultipartFile[] {file}))
.orElseThrow(() -> new RuntimeException("Unable to upload file."));
}
@Operation(
summary = "Upload multiple files.",
description = "Upload multiple files.", tags = {"fileupload"},
requestBody = @RequestBody(content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE))
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "successful operation",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = MessageResponse.class)))),
@ApiResponse(responseCode = "404", description = "entity not found",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiError.class)))),
@ApiResponse(responseCode = "403", description = "forbidden",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiError.class)))),
@ApiResponse(responseCode = "500", description = "internal server error",
content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiError.class))))})
@PostMapping(
value = "/multiple",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
Callable<ResponseEntity<MessageResponse>> uploadMultipleFiles(@RequestParam(value = "files", required= true) MultipartFile[] files) throws Exception {
final Optional<String> token = WebUtil.getTokenFromRequest(request);
return () -> token
.map(this.userService::getUserFromToken)
.map(user -> this.uploadUserFiles(user, files))
.orElseThrow(() -> new RuntimeException("Unable to upload file."));
}
private ResponseEntity<MessageResponse> uploadUserFiles(User user, MultipartFile[] files) {
Arrays.asList(files).forEach(file ->
{
File fileForUser;
try {
fileForUser = uploadPath(user, file);
} catch (IOException e) {
throw new RuntimeException(e);
}
try (InputStream in = file.getInputStream(); OutputStream out = new FileOutputStream(fileForUser)) {
FileCopyUtils.copy(in, out);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
return ResponseEntity.ok(MessageResponse.builder()
.status(HttpStatus.OK)
.message("File(s) uploaded successfully.")
.timestamp(LocalDateTime.now()).createApiResponse());
}
private File uploadPath(User e, MultipartFile file) throws IOException {
File uploadPath = Paths.get(this.uploadDirRoot.getPath(), e.getId().toString()).toFile();
if (!uploadPath.exists()) {
uploadPath.mkdirs();
}
return new File(uploadPath.getAbsolutePath(), requireNonNull(file.getOriginalFilename()));
}
} |
There seems to be a PR connected to this #5999 that has already been merged. Has anyone tested this? |
I just tried PR #5999 but still does not work. The input type is now text instead of file, so you can't choose files. If I change it to file it just posts a list of filenames, but no files. |
@anzoman @dfeinzeig Uploading arrays of files is not supported yet. |
@hkosova , can you point me at any relevant parts of the code that would need to be updated to support it? |
|
Any news ? |
@hkosova @anzoman @dfeinzeig @webhacking multiple file upload using OAS3.0 and arrays is now properly supported in the most recent SwaggerClient release (v3.25.4). Here's my sample definition: {
"openapi": "3.0.0",
"servers": [
{
"url": "http://localhost:3300/api/v1"
}
],
"info": {
"version": "1",
"title": "MULTI PART TEST - OAS3",
"description": ""
},
"paths": {
"/land/content/ViewOfAuthOwner": {
"post": {
"summary": "",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"hhlContent:sort": {
"description": "",
"default": "id",
"type": "string",
"enum": [
"id",
"title"
]
},
"hhlContent:order": {
"description": "",
"default": "desc",
"type": "string",
"enum": [
"asc",
"desc"
]
},
"email[]": {
"description": "The list of emails.",
"type": "array",
"items": {
"type": "string"
}
}
}
},
"encoding": {
"style": "pipeDelimited",
"explode": false
}
}
}
},
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
}
}
},
"/land/content/uploadImage": {
"post": {
"summary": "upload image(s)",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"imageId": {
"description": "",
"default": "id",
"type": "string"
},
"images[]": {
"description": "The list of files",
"type": "array",
"items": {
"type": "file",
"format": "binary"
}
}
}
}
}
}
},
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
}
}
}
}
}
|
What version of I am currently working with version 1.3.4.
Regards, FO |
@tim-lai OAS3 does not have openapi: 3.0.0
servers:
- url: 'http://localhost:3300/api/v1'
info:
version: '1'
title: MULTI PART TEST - OAS3
paths:
/land/content/uploadImage:
post:
summary: upload image(s)
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
imageId:
description: ''
default: id
type: string
'images[]':
description: The list of files
type: array
items:
type: string # <----------------
format: binary
responses:
'200':
description: '' The |
@tim-lai What is the maven dependency you use for SwaggerClient release (v3.25.4) |
@Mahalaxmi-13 We don't supply any maven dependencies that wrap our javascript projects. |
this dependency should solve my problem:
|
hi, i am facing the same problem in flask_swager_ui , I can not upload array of files files from swagger , when doing request.files i am getting an empty dictionary but i m sending the data from Swagger-UI . |
Swagger UI 3.16.0 added support for binary file upload using multipart requests. It works if a multipart request uses files as specific named fields, but doesn't work if the request uses an array of files.
Q&A (please complete the following information)
Content & configuration
This spec is based on an example from the OpenAPI 3.0.1 Specification:
Is your feature request related to a problem?
Yes, clicking "try it out" shows "😱 Could not render this component, see the console." The console errors are:
Describe the solution you'd like
Clicking "try it out" presents a form where we can upload multiple files.
Describe alternatives you've considered
N/a
Additional context
This is (sort of) related to #3641 "OAS 3.0 Support Backlog".
The text was updated successfully, but these errors were encountered: