Matúš Bartko
Matúš Bartko

Reputation: 2457

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?

Upvotes: 7

Views: 3855

Answers (3)

Mat&#250;š Bartko
Mat&#250;š Bartko

Reputation: 2457

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

Upvotes: 2

hocikto
hocikto

Reputation: 981

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

Upvotes: 1

starman1979
starman1979

Reputation: 1084

Try something like this:

responses:
     '200':
          description: A PDF file
          content:
            application/pdf:
              schema:
                type: string
                format: binary

Upvotes: 1

Related Questions