0

I'm using swagger 2.0.X and akka-http. I want to upload file via swagger ui, but don't know how to describe it into the code.I've tried to use request body with multipart content, but it doesn't works

  @POST
  @Path("/photo/upload")
  @Operation(
    summary = "Upload photo",
    parameters = Array(
      new Parameter(
        name = "doctype",
        description = "HEADER, describes uploaded document type",
        in = ParameterIn.HEADER,
        required = true,
        schema = new Schema(implementation = classOf[String])
      )
    ),
    requestBody = new RequestBody(
      description = "file",
      content =
        Array(new Content(mediaType = "multipart/form-data", schema = new Schema(`type` = "string", format = "binary")))
    )
  )
  def startPhotoUpload: Route = post {
        headersMap { headers =>
            fileUpload("filename") {
              case (_, fileStream) =>
                val is = fileStream.runWith(StreamConverters.asInputStream())
                handleRequest(
                  documentUploadServiceActorProps,
                  SendPhotoToFurtherUpload(is, headers)
                )
            }
        }
    }
HudsonHornet
  • 89
  • 1
  • 9

2 Answers2

1

This is kinda interesting situation. Swagger specification has information about file upload. But I couldn't find a way to write code that would allow me to upload files from Swagger UI using multipart/form-data. It works if you change

mediaType = "multipart/form-data"

to

mediaType = "application/octet-stream"

or even more specific type like "image/png"

The downside of this method is that fileUpload directive doesn't work with streams, it expects multipart data. You'll have to use extractDataBytes instead and it's not that good if you also need file metadata, so you'll need to supply that info differently

Smerk
  • 166
  • 1
  • 4
0

i have tried this with some success :

swagger-akka-http 2.x Set List of Objects in the response

mainly the trick is to add a case class as :

 //just for swagger
  case class FileUpload(@Schema(`type` = "string", format = "binary", description = "file") file: File)
Flow
  • 57
  • 5