Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 378
Matúš Bartko Avatar asked Feb 12 '20 16:02

Matúš Bartko


3 Answers

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

like image 191
Matúš Bartko Avatar answered Sep 18 '22 02:09

Matúš Bartko


Try something like this:

responses:
     '200':
          description: A PDF file
          content:
            application/pdf:
              schema:
                type: string
                format: binary
like image 22
starman1979 Avatar answered Sep 20 '22 02:09

starman1979


maybe you could try something like this

/myendpoint:
    get:
      tags: [myendpint]
      summary: my cool endpoint
      operationId: getStuff
      x-custom-return-type: 'java.something.stream.Stream'

than use mustache and specify it in mustache file with vendor extensions

ResponseEntity<{{#responseWrapper}}{{
    .}}<{{/responseWrapper}}{{>returnTypes}}{{#vendorExtensions.x-custom-return-type}}

I use that for interface API and maybe somehow you could use it in your case too

like image 21
hocikto Avatar answered Sep 21 '22 02:09

hocikto