swagger-api / swagger-api/swagger-codegen

[BUG] For java Resttemplate Client there is no way to upload file with defined contentType

Open
#7,538 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Hello,
there is no way to do the same request (providing application/pdf) as

curl -X POST -H "content-type: multipart/form-data" -F "file=@pdf-test.pdf;type=application/pdf"

to do this java.io.File should be replaced with more generic org.springframework.http.HttpEntity

    private HttpEntity<Object> createUploadData(MediaType mimeType, InputStream attachment) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mimeType);
        return new HttpEntity<>(new org.springframework.core.io.InputStreamResource(attachment), headers);
    }

HttpEntity allows us to use filename and file mime type.

My proposition is to replace formParams.add("file", new FileSystemResource(file));
with formParams.add("file", httpEntity)

Side notes:

  1. java.io.File is very restrictive object type, you can`t extend it in a simple way.
  2. selectBody logic is misplaced. by the time we invoke ApiClient.invokeAPI(), MyGeneratedApi shpould determine whether we will use Object body or MultiValueMap<String, Object> formParams to pass form parameters

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 modules/swagger-codegen/src/main/resources/Java/libraries/resttemplate/api.mustache at the formParams.add("file", ...) call, then review ApiClient.mustache around selectBody and invokeAPI(). Done means generated RestTemplate clients can upload a file with a caller-defined MIME type such as application/pdf.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.