openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [java-micronaut-server] Binary file to download has a wrong object type

Open marosrojis opened this issue 2 years ago • 2 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
Description

If the Micronaut endpoint returns a binary file, micronaut uses object types SystemFile or StreamedFile. But the problem is, if the Openapi definition has format: binary, the generated endpoint returns object CompletedFileUpload. This object is uses to upload file using multipart, but not to download the file.

openapi-generator version

I use the latest version OpenAPI Generator with maven plugin v6.6.0.

OpenAPI declaration file content or url
/download:
  get:
    tags:
      - download
    description: Download file
    operationId: downloadFile
    responses:
      "200":
        description: Response with encrypted ZIP file includes all application log files
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
Generation Details
@Get(uri="/download")
@Produces(value = {"application/octet-stream"})
public HttpResponse<CompletedFileUpload> downloadFileApi() {
    return downloadFile();
}
Steps to reproduce
  1. Create an endpoint with content type application/octet-stream and schema format binary.
  2. Generate code with java-micronaut-server.
  3. Check the generated controller.
Related issues/PRs

Related ticket to generate OpenAPI definition from the implementation and swagger annotations - https://github.com/micronaut-projects/micronaut-openapi/issues/441

marosrojis avatar Jun 06 '23 06:06 marosrojis

This is almost the same issue with the spring-kotlin generator. However, it uses Springs Resource class, that is what you want when downloading the file, but it does not work when uploading. I managed to get it to work by using a typemapping from the Resource class to the MultipartFile class, but then that broke downloading, which must use Resource...

bushwakko avatar Jun 14 '23 13:06 bushwakko

Use type mapping for it, like this:

    openapi {
        server(file("swagger.yml")) {
            typeMapping = [
                    file : "StreamingFileUpload"
            ]
        }
    }

Or set StreamingFileUpload as schema type:

multipart/form-data:
           schema:
             type: object
             properties:
               file:
                 type: StreamingFileUpload
                 format: binary

Just use official micronaut generator for java and kotlin by micronaut-opeanpi gradle or maven plugin from this repo: https://github.com/micronaut-projects/micronaut-openapi

Look to this guide: https://guides.micronaut.io/latest/micronaut-openapi-generator-server.html

Also, please describe problems and suggestions here: https://github.com/micronaut-projects/micronaut-openapi/issues

altro3 avatar Aug 27 '24 07:08 altro3