user2593400
user2593400

Reputation: 173

when using swagger codegen getting 'List<byte[]>' instead of simply 'byte[]'

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>

Upvotes: 7

Views: 30053

Answers (2)

Maksym
Maksym

Reputation: 2870

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"
        }
      }
    }
  }
}

Upvotes: 2

user2593400
user2593400

Reputation: 173

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.

Upvotes: 10

Related Questions