swagger-api / swagger-api/swagger-ui

Encoding issues with the "Download file" links

Open
#8,070 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

Q&A (please complete the following information)
Content & configuration

I have created a small demo repo: https://github.com/LukasKaufmannRelaxdays/SwaggerUiBugDemo
You can start it with docker or directly as a dotnet application.
As the Swashbuckle-Framework handles all the config for me, I have omitted these details

Describe the bug you're encountering

When downloading a non UTF-16 encoded text file with the "Download file" link, the user receives a file that is different from the file the API returned.

To reproduce...

Steps to reproduce the behavior:

  1. Clone https://github.com/LukasKaufmannRelaxdays/SwaggerUiBugDemo
  2. Follow the Readme.md there.
  3. After starting the application and navigating to the page, use "Try it out" for the only available endpoint
  4. Now click the "Download file" link
  5. Notice how the downloaded file has a size of 3 bytes (EF BF BD)
  6. Now try accessing the endpoint directly by going to either https://localhost:7245/Test or http://localhost:7245/Test depending on the way you ran the example project
  7. Notice that the second file is only 1 byte (DF)
Expected behavior

The "Download file" link should initiate a download, that produces a file with exactly the same bytes as the API originally returned.

Screenshots

image

Additional context or thoughts

This problem has to do with JavaScript and how it handles the conversion from blob to string. This conversion is not symmetric, but I think swagger-ui assumes that it is, when generating the ObjectURL with a blob generated from the text representation of the response body in https://github.com/swagger-api/swagger-ui/blob/cc700f034b09424c25ea908b53d5057c036f186b/src/core/components/response-body.jsx#L71

The issue https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2087 also contains valuable information on this topic, but I think this is more of an issue with swagger-ui itself. Otherwise I would be happy if you could give me some pointers. With binary contentTypes like application/pdf the issue goes away, so maybe it is also just a configuration issue.

In my opinion it shouldn't be possible for the link to return a different file as this is very confusing for everyone working with special encoding.

Keywords: utf-8, latin-1, ISO 8859-15, cp1252

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/core/components/response-body.jsx around the ObjectURL generation cited in the issue, then reproduce the behavior using the SwaggerUiBugDemo README and its /Test endpoint. Compare the downloaded bytes with the direct response for the non-UTF-16 file. Done means the Download file link preserves exactly the bytes returned by the API.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.