[BUG] [java-micronaut-server] Binary file to download has a wrong object type
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
- Create an endpoint with content type
application/octet-streamand schema formatbinary. - Generate code with
java-micronaut-server. - 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
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...
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