OpenAPITools / OpenAPITools/openapi-generator
[BUG] [TypeScript-Angular] Issue with filename="blob" in multipart requests using Blob for JSON data
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
We are facing an issue with the OpenAPI Generator client (TypeScript/Angular) when sending JSON data as a Blob via multipart/form-data. The issue occurs because when a Blob is used, the browser automatically adds a filename="blob" to the Content-Disposition header, even though we do not intend to send a file.
This causes problems on the server side (Spring in our case) as the server interprets this part of the request as a file due to the filename="blob" field, which is not the desired behavior for sending simple JSON data.
openapi-generator version
7.11.0
OpenAPI declaration file content or url
the relevant part is
"post": {
"tags": [
"Allegato"
],
"operationId": "create_1",
"parameters": [
{
"name": "idAnagrafica",
"in": "query",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"allegato",
"file"
],
"type": "object",
"properties": {
"allegato": {
"$ref": "#/components/schemas/AllegatoDto"
},
"file": {
"type": "string",
"format": "binary"
}
}
}
}
}
}
Generation Details
simply run the command:
openapi-generator-cli generate -i apidocs.yaml -g typescript-angular -o src/app/api
Steps to reproduce
- Use OpenAPI Generator to generate a TypeScript client for a Spring backend.
- Create an API method that sends both a file and a JSON object in a multipart/form-data request. (e.g. the third exposed in springdoc F.A.Q).
- When using Blob for the JSON payload, the browser adds a filename="blob" in the request.
- The Spring server interprets this as a file instead of just a JSON payload (show parseRequest method in this file,) causing issues in processing the request.
Actual Output:
The generator generate this :
if (allegato !== undefined) {
localVarFormParams = localVarFormParams.append('allegato', localVarUseForm ? new Blob([JSON.stringify(allegato)], { type: 'application/json' }) : allegato) || localVarFormParams;
}
Problem
The filename="blob" is automatically added when using a Blob, even though we do not intend to send a file.
Spring's StandardMultipartHttpServletRequest attempts to handle it as a file, even though it should be treated as a regular JSON payload.
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/15771
Suggest a fix
A configuration option in the OpenAPI Generator could be introduced to disable the filename handling when sending JSON via Blob. This would allow the server to receive the data correctly without the filename="blob" interfering with the parsing process.
Why This Change is Needed
- Compatibility with server-side frameworks like Spring, which incorrectly interpret Blob data as file data.
- No need for custom handling on the server side: The Spring server can process the data directly without needing to be modified to handle filename="blob".
Additional Context
- This issue is particularly relevant when using OpenAPI Generator to generate clients for applications that need to send JSON data along with other files. By default, OpenAPI Generator uses Blob to send the JSON data, which results in incorrect filename="blob" handling.
- This issue impacts projects that rely on multipart/form-data to send both files and JSON data to backend services (e.g., Spring-based backends).
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 the typescript-angular generator output shown in the issue and trace the multipart form assembly produced by openapi-generator-cli generate -g typescript-angular. Compare the behavior with related issue #15771 and the Spring StandardMultipartHttpServletRequest parsing reference. Done means the generated client can send JSON in multipart/form-data without an unintended filename="blob", while preserving file uploads.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, spring, typescript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 32/100