Swagger-ui: Downloaded files have pre/post pended underscores

Created on 5 Apr 2018  路  18Comments  路  Source: swagger-api/swagger-ui


When downloading files, the UI seems to pre and post pend underscores to the filename.

Version: Swagger 2.0 UI

Q: For example, the service returns "File01.zip". On the browser side, it downloads as "_File01.zip_". I'm running both a pretty old version of swagger that doesn't add the underscores and trying to upgrade to Swagger 2.0 both API and UI. To install the UI, I simply copied the dist folder contents to our tomcat server. Server is running RHEL 7.0 with Tomcat 7.

My yaml code for this services:

  '/export/download/{id}':
    get:
      tags:
        - "export:"
      summary: Download a zip file containing document with given ID
      description: Zip file to be downloaded with the given document ID
      produces:
        - application/zip
      parameters:
        - $ref: '#/parameters/uuidParam'
      responses:
        '200':
          description: "CSV Zipped File"
          schema:
            "type": "file"
        '400':
          $ref: "#/responses/InvalidParam"
        '500':
          $ref: "#/responses/ServerError"

The returned Response Headers are:

access-control-allow-headers: Content-Type
access-control-allow-methods: GET, POST, DELETE, PUT
access-control-allow-origin: *
content-disposition: attachment; filename="File01.zip"
content-length: {len of the file}
content-type: application/octet-stream
date: DDD, dd mmm YYYY hh:mm:ss GMT
server: Apache-Coyote/1.1

I was really surprised that this issue has not come up before and am wondering if I am doing something pretty obvious that is causing this, but I have gone over this for hours now and do not see how the underscores are being added?? Note that the older version of Swagger we are using doesn't have this issue.

It took me quite a few hours to figure out that, without the content-length, the file was not even downloading, though I could see the byte stream coming across. This one I am stumped on.
Thanks to anyone who takes the time to look at this.

lock-bot support 2.x

All 18 comments

Can you try with the latest Swagger-UI ?
Here is my test:
http://swagger-net-test.azurewebsites.net/swagger/ui/index#/Image/Image_Put
There are no pre or post pend underscores in the file

I did pull the latest 2.x release yesterday and still had the same issue. Did you mean to try using Swagger 3.x?

Yes, latest is 3.x
the 2.x is old, do not expect any bug fixes there

Does that mean I need to also rewrite my .yaml file to be OpenAPI 3.x compatible or can the Swagger 3.x UI use the .yaml file I already created using Swagger 2.0 spec?
I really appreciate the help and apologies for asking more question.

No, no need to rewrite, your definition can stay the same...
Take a look at the spec on the link I posted, I'm using:

  • Swagger 2.0
  • Swagger-UI "3.13.2"

That is a relief to hear. I'll give it a try on Monday. Thanks very much for your help. I'll close this out once I have tested this next week. Have a good weekend.

I downloaded the latest swagger-ui version and copied the dist folder to our tomcat server, put my json file in and, unfortunately, still see the pre and post pended underscores on the filename. I cannot deploy this and am hoping it can be fixed sometime soon. The main issue is the post-pended underscore, which makes the filenames _{filename}.zip_ (this window doesn't seem to want to display the underscore after the .zip, but there is one there)
Since the users are on a Windows system, it doesn't know what to do with a .zip_ filetype.
Any other help or suggestions would be greatly appreciated.
Using Swagger 2.0 and Swagger-UI 3.13.2
(I did notice that a 3.13.3 was just released 2 days ago and I might give that one a try too.)

Can you share a link to your API?

A link like this would be very helpful:
http://swagger-net-test.azurewebsites.net/swagger/ui/index#/Image/Image_Put

For the record I'm a windows user, and I can not reproduce with the details you provided

I can download the link to Swaggerhub where I developed the API. Unfortunately, the server it resides on cannot be accessed externally from our site, so I cannot send you a link to the test site.

https://app.swaggerhub.com/apis/GL_Project/Glare_Services/1.0.0

The specific service call is the /export/download/{id}

Ok so that one you are developing is not accessible externally, can you create one minimal API that reproduces your issue and host it somewhere available to all?

I hope you do see my point here... I have used the download functionality for a while and have not seen this issue you are describing, the file name come from the server just like you showed:
content-disposition: attachment; filename="File01.zip"
And the UI and browser will respect that...
By the way what browser are you using?

hmmm, I might be able to do something at home, but unfortunately, where I work doesn't provide any externally facing servers for us. I did try the Swagger Test api/image and it downloaded without the underscores. Can you send me a snippet showing the yaml or json that was used for this?
Thanks again for all your help.

Nothing much there but here it is:

      "put": {
        "tags": [
          "Image"
        ],
        "operationId": "Image_Put",
        "consumes": [],
        "produces": [
          "application/octet-stream"
        ],
        "parameters": [],
        "responses": {
          "200": {
            "description": "Download an image",
            "schema": {
              "type": "file"
            }
          }
        },
        ...

I absolutely agree with your point. I'm positive that when the file is created on the server side that it has no underscores and that the header information also shows the file without underscores. I tried this in both Firefox and Chrome with the same results. In Firefox, it shows a prompt saying "You have chosen to open: "_File01.zip_"
I understand that troubleshooting something like this without seeing the actual issue is not easy, so do appreciate your help. I was hoping if you could show me the yaml / json for your download, it might show me something I did differently.

Thanks for the snippet. I'm going to modify mine so the "produces" is a stream vs. application/zip and see what happens. I'll let you know...

Fixed! Found out it was the Content-Disposition header that was being returned. The service is for a set of Java REST services and the docs indicated the file should be returned as:
response.header("Content-Disposition", "attachment; filename=\"" + zip.getName() + "\"");
(Note that when I write this the code is actually escaping a double-quote (slash following by a double-quote, inside double-quotes) but it doesn't seem to display this correctly. They are appearing as double and triple quotes!?)

This would put quotes around the returned filename. I removed these, so the line becomes:
response.header("Content-Disposition", "attachment; filename=" + zip.getName() + ");

And the filename now downloads without the pre|postpended underscores.
Thanks Helder for all your help. Even though the fix was in the java REST service and not in Swagger, I thought you might like to know in case this issue ever comes up again.

Thanks for the update, @ringraham.

I don't understand why Swagger documentation does not cover @heldersepu and @ringraham answers and I need to dig into the Github issues to setup the file download API 馃槕

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Deraen picture Deraen  路  4Comments

ankon picture ankon  路  4Comments

andrecarlucci picture andrecarlucci  路  3Comments

LaysDragon picture LaysDragon  路  3Comments

DavidBiesack picture DavidBiesack  路  4Comments