swagger-api / swagger-api/swagger-codegen

[Java][CXF] Include the file name and the proper content type in a file download

Open
#6,953 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: General Server: Java
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

This is a feature request. I have the necessity to include the file name in the response as well as to change the content type of the response to match the one of the file. Currently, I am not able to generate a file download API that can stand on its own without the need of additional coding on the client side.

Swagger-codegen version

2.3.0

Swagger declaration file content or url

For example, to define a REST API that downloads a file you need to define something like the following in the YAML:

paths:
  /v1/files:
    get:
      summary: "get"
      produces:
      - "application/octet-stream"
      parameters:
      - name: "file"
        in: "query"
        required: true
        type: "string"
      responses:
        200:
          description: "Status 200"
          schema:
            type: "string"
            format: "binary"

This definition generates the following CXF Java server stub interface:

    @GET
    @Path("/v1/files")
    @Consumes({ "application/json" })
    @Produces({ "application/octet-stream" })
    @ApiOperation(value = "get", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Status 200", response = byte[].class) })
    public byte[] v1FilesGet(@QueryParam("file") @NotNull String file);

And the following CXF Java server stub implementation:

    public byte[] v1FilesGet(String file) {
        // TODO: Implement...
        
        return null;
    }

But, the return type byte[] does not allow to include the file name in the response and to change the content type of the response to match the one of the file.

Command line used for generation

..

Steps to reproduce

..

Related issues/PRs

..

Suggest a fix/enhancement

I propose that the CXF Java server stub implementation outputs a code similar to the following:

    public Response v1FilesGet(String file) {       
        byte[] fileContent = null; // TODO: Implement file content load...
        String fileName = null; // TODO: Implement file name load...

        ResponseBuilder response = Response.ok(fileContent);
        response.header("Content-Disposition", "attachment; filename=" + fileName);
        response.header("Content-length", fileContent.length);
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(fileName);
        response.header("content-type", mimeType + ";charset=UTF-8");
        return response.build();
    }

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 the Swagger YAML example and the generated CXF Java server stub shown in the issue, then trace the CXF Java server templates that select byte[] for binary responses. Review how generated responses are tested, if applicable. Done means the generated server code can return a download filename and matching content type without client-side additions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.