8

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>

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

What do we have to do in case of a StreamingResponseBody? See stackoverflow.com/questions/64080432/…
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.