swagger-api / swagger-api/swagger-ui
Encoding issues with the "Download file" links
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)
- OS: Ubuntu
- Browser: Chrome 102.0.5005.115
- Method of installation: Bundled version included in https://github.com/domaindrivendev/Swashbuckle.AspNetCore; Installed with nuget
- Swagger-UI version: 4.10.3
- Swagger/OpenAPI version: Swagger 2.0, OpenAPI 3.0
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:
- Clone https://github.com/LukasKaufmannRelaxdays/SwaggerUiBugDemo
- Follow the Readme.md there.
- After starting the application and navigating to the page, use "Try it out" for the only available endpoint
- Now click the "Download file" link
- Notice how the downloaded file has a size of 3 bytes (EF BF BD)
- 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
- 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

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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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