while working on Quarkus with the RestEasy framework, I have the functionality to upload a file using MultipartFormDataInput
. This functionality is working as expected, but I am unable to provide proper open API annotation for swagger UI. I have tried multiple options and combinations, but it did not bear fruit. Please help me. I provide below the sample code.
@Operation(summary = "Upload a single file", description = "Upload a single file")
@APIResponses({
@APIResponse(responseCode = "200", description = "Upload file successfully"),
@APIResponse(name = "500", responseCode = "500", description = "Internal service error") })
@RequestBody(content = @Content(
mediaType = MediaType.MULTIPART_FORM_DATA,
schema = @Schema(type = SchemaType.STRING, format = "binary"),
encoding = @Encoding(name = "attachment", contentType = "application/octet-stream")))
@POST
@Path("/singleFile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response handleFileUpload(@MultipartForm MultipartFormDataInput input) {
String fileName = null;
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
// Get file data to save
List<InputPart> inputParts = uploadForm.get("attachment");
for (InputPart inputPart : inputParts) {
try {
MultivaluedMap<String, String> header = inputPart.getHeaders();
fileName = getFileName(header);
InputStream inputStream = inputPart.getBody(InputStream.class, null);
byte[] bytes = IOUtils.toByteArray(inputStream);
File customDir = new File(UPLOAD_DIR);
if (!customDir.exists()) {
customDir.mkdir();
}
fileName = customDir.getCanonicalPath() + File.separator + fileName;
Files.write(Paths.get(fileName), bytes, StandardOpenOption.CREATE);
return Response.status(200).entity("Uploaded file name : " + fileName).build();
} catch (Exception e) {
e.printStackTrace();
}
}
return Response.status(200).entity("Uploaded file name : " + fileName).build();
}
I have also referred to the following links.
https://github.com/swagger-api/swagger-core/issues/3050
I am able to generate swagger UI if I create a separate class called MultipartBody
with @Schema(type = SchemaType.STRING, format = "binary") and @PartType(MediaType.APPLICATION_OCTET_STREAM)
annotation. But my requirement is to use only MultipartFormDataInput
.
You were almost there :) Just use a dedicated class in your RequestBody/Schema and tell OpenAPI ignore the params of your method.
Two things to note:
@Parameter(hidden = true)
tells Smallrye OpenAPI to not consider yourMultipartFormDataInput
when generating Schema model. Then you need to tell describe the schema explicitly, using@RequestBody
, whereMultipartBody
is a class that describes all your input params(you can add more params there if you want to for example pass other props along with the file payload)Just make sure that the
@FormParam
annotated fields in theMultipartBody
match the ,,parts" that you expect to find in yourMultipartFormDataInput
- e.g. in your case the file attribute should have@FormParam("attachment")