7

A byte[] is modelled in swagger file as an Array of byte[]. When using swagger codegen we are getting List<byte[]> instead of simply byte[]

Swagger.json

"document": {
    "type": "array",
    "items": 
    {
        "type": "string",
        "format": "byte"
    }
}

pom.xml

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/swagger.json</inputSpec>
                <language>java</language>
                <configOptions>
                   <sourceFolder>src/gen/java/main</sourceFolder>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
user2593400
  • 163
  • 1
  • 1
  • 8

2 Answers2

9

The issue is in generating the swagger.json file ie the maven plugin swagger-maven-plugin. The correct swagger.json file for a byte[] should look like:

"document": {
        "type": "string",
        "format": "byte"
 }

In order to achieve this we have to add custom ModelConvertors exactly as shown in below link: https://github.com/kongchen/swagger-maven-plugin/issues/422

Also add ModelConvertors tag in the project pom file with the path to the location of your custom modelconvertor.

Note: There is no change in swagger-codegen-maven-plugin.

user2593400
  • 163
  • 1
  • 1
  • 8
  • 1
    What do we have to do in case of a `StreamingResponseBody`? See https://stackoverflow.com/questions/64080432/how-to-declare-binary-data-response-content-type-correctly – Stefan Falk Sep 26 '20 at 17:20
0

If you are using swagger-codegen-maven-plugin for OpenAPI v.2 json file then response with byte[] return type looks like this:

"responses": {
  "200": {
    "description": "byte[] return example for swagger: 2.0",
    "schema": {
      "type": "string",
      "format": "byte"
    }
  }
}

And response for OpenAPI v.3 looks like this:

"responses": {
  "200": {
    "description": "byte[] return example for openapi: 3.0",
    "content": {
      "*/*": {
        "schema": {
          "type": "string",
          "format": "byte"
        }
      }
    }
  }
}
Maksym
  • 2,650
  • 3
  • 32
  • 50