blog image

Java Class FileUpload to work properly in OpenAPI/Swagger UI in Quarkus

When implementing microservices, especially in the cloud environment interacting with the both cloud and on-premise world it is crucial to be able quick verify the functionality which microservice/component is providing. Components are responsible for specific isolated functions , so until it can be tested end-to-end with UI it can take a time. For this purpose a OpenAPI/Swagger UI is very useful feature, which is also available in the quarkus java framework.

Almost every IT applocation is dealing with documents.We were developing microservice which was receiving documents and uploading them to the document mgmt system. During testing we found out, that both java class File and FileUpload were not working as expected and we could not test effective with swagger UI.

When we used java class FileUpload, we could retrieve all needed property such as uploaded File Name, encoding, content type , but the Swagger UI was not of much use, see

 scrnshtswagger2

 When we used java class File  from java.io.File control was rendered correctly  , but the functionality was limited and was not serving our needs to retrieve all we needed.

 This is a documented bug in java quarkus framework and the workaround can be for the definition of the DTO :

  1. Create empty dto entity MultiPartBody
  2. reference it with the annotation @Schema(implementation = MultipartBody.class)
  3. enjoy the swagger control for the file upload 

package ch.mobi.dki.integration.entity;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
@Schema(type = SchemaType.STRING, format = "binary")
public class MultipartBody {}
package ch.mobi.dki.integration.entity;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.jboss.resteasy.reactive.PartType;
import org.jboss.resteasy.reactive.RestForm;
import org.jboss.resteasy.reactive.multipart.FileUpload;
public class MCMDto {
    @RestForm("partnerNumber")
    @PartType(MediaType.APPLICATION_JSON)
    @Schema(example = "P26173857")
    public String partnerNumber;
    @RestForm("file")
    @PartType(MediaType.APPLICATION_JSON)
    @Schema(implementation = MultipartBody.class)
    public FileUpload file;
}

 scrnshtswagger1

Contact us for the development of components and microservices and for the cloud native development ! 

Before we begin: take a look at the processing of your personal data

If you visit a site that records cookies, a small text file will be created on your computer and stored in your browser. The next time you visit the same page, it will help you connect to the web faster. Our website will offer you relevant information and make it easier for you to work.

We mainly use cookies for anonymous traffic analysis and to improve our website. If you set your browser to block cookies, it is possible that the website will slow down and some parts of the website may not work completely correctly. More info on the processing of cookies.