swagger-ui
swagger-ui copied to clipboard
Encoding issues with the "Download file" links
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