Swagger-ui: Unrecognized response type - application/pdf displayed incorrectly in response body

Created on 26 Jul 2019  路  5Comments  路  Source: swagger-api/swagger-ui

Q&A

  • Method of installation: dist
  • Swagger-UI version: e.g. 3.23.0
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Swagger/OpenAPI definition:

  /pdf/{date}/lunch.pdf:
    get:
      tags:
        - PDF
      summary: Mittagessen als PDF (Woche)
      parameters:
        - name: date
          in: path
          description: YYYY-MM-DD
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          headers:
            Content-Disposition:
              schema:
                type: string
                example: filename=\lunch.pdf\
          content:
            application/pdf:
              schema:
                type: string
                format: binary
              example: PDF-Datei

Describe the bug you're encountering


When you are calling an API in the try it out-section which returns an PDF-File the response body is not able to render the file or present it in a readable format. Instead you get the following message: Unrecognized response type; displaying content as text. - even if the correct content type is passed in the response header (Screenshot HTTP Request-Response).
Additionally the binary data will be displayed in the response body. (Screenshot Swagger-UI)

To reproduce...

Steps to reproduce the behavior:
In Swagger-UI

  1. Navigate to the specific path
  2. Click on 'Try it out'
  3. 'Execute'
  4. Inspect Response body

Expected behavior


The PDF should be rendered as an typical pdf file in the swagger-ui or you should provide a download link to extract the file.

Screenshots


HTTP Request-Response
Swagger-UI Display

Additional context or thoughts


Unfortunatly i cannot provide a running API which returns a valid PDF-File to debugging this issue
Whenever you run the command
curl -X GET "http://server.de/menupdfservice/api/pdf/pdf/2019-07-26/lunch.pdf" -H "accept: application/json
which the Swagger-UI is calling outside of Swagger-UI you get the binary data as well.
But when you try to merge this data with
curl -X GET "http://server.de/menupdfservice/api/pdf/pdf/2019-07-26/lunch.pdf" -H "accept: application/json --output lunch.pdf
in a spefic file with extension you get a properly working PDF-file

Most helpful comment

In the end it worked when I added headers to the response:

res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=labels.pdf');

All 5 comments

@fabieu have you solved that?

@fabieu have you solved that?

Actually not, but I was tired dealing with this issue

Same here. I'm using swagger-jsdoc and face this issue. My endpoint:

/**
 * @swagger
 *
 * /pdf:
 *   post:
 *     description: Get PDF
 *     produces:
 *       - application/pdf
 *     parameters:
 *       - in: body
 *         name: orderIds
 *         description: order ids 
 *         schema:
 *           type: array
 *           items:
 *             type: string
 *             format: guid
 *     responses:
 *       200:
 *         description: A PDF file
 *         content:
 *           application/pdf:
 *             schema:
 *               type: file
 *               format: binary
 */

In the end it worked when I added headers to the response:

res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=labels.pdf');

Same problem here, and besides that, The serialized string has encoding issue.
If I test from browser or JAVA client, the PDF response is correct.

Sounds to be an issue on swagger-ui to render/serialize PDF files,

Was this page helpful?
0 / 5 - 0 ratings