Skip to content

Retrofit2: Support "File" Response type #6406

Description

@craffael
Description

If we use the Retrofit2 generator with a swagger endpoint that returns a file, the corresponding retrofit method will return a java.io.file which is not supported by Retrofit: It throws a com.google.gson.stream.MalformedJsonException.

Swagger-codegen version

I've used the latest master branch, 2.3.0-SNAPSHOT.

Swagger declaration file content or url
# C:\temp\swagger.yaml
swagger: "2.0"
info:
  
  version: "1.0.0"
  title: "ImageRetrieval"
host: "boston2016.apistrat.com"
schemes:
- "http"
paths:
  /images/{name}:
    get:
      summary: "Add a new pet to the store"
      produces:
      - "image/png"
      parameters:
      - in: path
        name: name
        required: true
        type: string
      responses:
        200:
          description: Image found
          schema:
            type: file # !!!
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i C:/temp/swagger.yaml -o C:/temp/swagger/ -l java --library retrofit2 --additional-properties useRxJava2=true

Steps to reproduce
  1. Create the file C:\temp\swagger.yaml and copy the content from above into it.
  2. java -jar swagger-codegen-cli.jar generate -i C:/temp/swagger.yaml -o C:/temp/swagger/ -l java --library retrofit2 --additional-properties useRxJava2=true
  3. Open the file C:\Temp\swagger\src\test\java\io\swagger\client\api\DefaultApiTest.java and insert the following test case:
    @Test
    public void imagesNameGetTest2() {
        String name = "sponsor-logo-swagger.png";
        File response = api.imagesNameGet(name).blockingLast();
    }
  1. Run the test case and observe the error message :(
Related issues/PRs

I think this is the first time this issue has been reported.

Suggest a fix/enhancement

A small change in the respective template fixes the bug. I will commit a PR soon.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions