swagger-api / swagger-api/swagger-codegen
[BUG] For java Resttemplate Client there is no way to upload file with defined contentType
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:
java.io.Fileis very restrictive object type, you can`t extend it in a simple way.- selectBody logic is misplaced. by the time we invoke ApiClient.invokeAPI(), MyGeneratedApi shpould determine whether we will use
Object bodyorMultiValueMap<String, Object> formParamsto pass form parameters
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 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