OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.57k stars 6.52k forks source link

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

Open marosrojis opened 1 year ago

marosrojis commented 1 year ago

Bug Report Checklist

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

bushwakko commented 1 year ago

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...

altro3 commented 1 month ago

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