swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.61k stars 8.96k forks source link

Unrecognized response type; displaying content as text. #10128

Open HarshChakalasiya opened 2 months ago

HarshChakalasiya commented 2 months ago

Q&A (please complete the following information)

Content & configuration

Swagger/OpenAPI definition:

{
  "downloadFile": {
    "get": {
      "tags": [
        "Download Data File"
      ],
      "summary": "Get Download File",
      "operationId": "v1_download_data_file",
      "produces": [
        "application/zip"
      ],
      "parameters": [
        {
          "name": "Content-Type",
          "in": "header",
          "required": true,
          "schema": {
            "type": "string",
            "example": "application/json"
          }
        },
        {
          "name": "Accept",
          "in": "header",
          "required": true,
          "schema": {
            "type": "string",
            "example": "application/zip"
          }
        },
        {
          "required": true,
          "schema": {
            "type": "string",
            "example": "54e72396-57ed-4d35-9066-b3b3b58b0b06"
          },
          "name": "attachment-id",
          "in": "path"
        }
      ],
      "responses": {
        "200": {
          "description": "Successful Response",
          "headers": {
            "Content-Encoding": {
              "name": "Content-Encoding",
              "description": "A Encoding Algorithm used for encoding response",
              "in": "header",
              "schema": {
                "type": "string"
              }
            },
            "Content-Disposition": {
              "name": "Content-Disposition",
              "in": "header",
              "schema": {
                "type": "string"
              }
            }
          },
          "content": {
            "application/zip": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            },
            "application/octet-stream": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            }
          }
        },
        "400": {
          "description": "Failure Response",
          "content": {
            "application/json": {
              "schema": {
                "type": "object"
              }
            }
          }
        },
        "403": {
          "description": "Authorization information is missing or invalid.",
          "content": {
            "application/json": {
              "schema": {
                "type": "object"
              }
            }
          }
        }
      }
    }
  }
}

Swagger-UI configuration options:

SwaggerUI({
  dom_id: '#swaggerUIApp',
      url: this.state.doc,
      deepLinking: true,
})

How can we help?

Hello,

I am hitting one api which gives zip file as response with Content-Disposition, Content-Type response headers. When I am trying to hit that api from postman, I will get the file response and I can download the zip file but in swagger, I am receiving the file response as text with Unrecognized response type; displaying content as text. this error and it is downloaded as text file when I am trying to download it.

Here I pasted the json config file of that swagger route.

I am receiving following response headers values.

  1. Content-Disposition :- attachment; filename=output.zip
  2. Content-Type :- application/zip

So can anyone help to figure it out.

Screenshot from 2024-09-10 13-02-27

Rico0308 commented 5 days ago

Hi @HarshChakalasiya I fixed that error modifying my endpoint and the documentation of Swagger definition:

In the endpoint set a properties on res, for example:

res.set({
  "Content-Type": "application/pdf",
  "Content-Length": pdfBuffer.length, // If have the length of the PDF
  "Access-Control-Expose-Headers": "Content-Disposition",
  "Content-Disposition": "attachment; filename=nameFile.pdf",
});

After, modify the Swagger definition, for example:

    '200':
      description: PDF success.
      content:
        application/pdf:
          schema:
            type: string
            format: binary

With that changes in the Swagger UI the Response Body show a Button to Download the pdf.