Swagger codegen to Java Spring generates incorrect file response entity from OpenAPI component of binary format

Matúš Bartko :

I am using swagger-codegen-maven-plugin to generate Spring interface from OpenAPI file (OpenAPI 3.0.2)

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.14</version>

The response of one rest API should be PDF file

components:
    schemas:
        contractFile:
            type: string
            format: binary

Generated Java REST interface then contains the following method

default ResponseEntity<File> getContract(@ApiParam(value = "File to download",required=true) @PathVariable("uid") String uid) {
...
}

File class represents the path to the filesystem, but I don't have the file on the filesystem, just bytes in java memory and I don't want to save them as the file to disk.

I want getContract to return some StreamResource or some other representation of file from Stream/bytes in memory. Is it possible this via swagger-codegen-maven-plugin or some other option?

Matúš Bartko :

In the end, I switched from

<plugin>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.14</version>
  ...
</plugin>

to

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>4.2.3</version>
  ...
</plugin>

This plugin got it right and generates Resource instead of ResponseEntity

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=4273&siteId=1