OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-fetch] multipart/form-data request body does not respect date-time format
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 using openapi-generator-cli to generate typescript-fetch code based on an openapi 3.x spec generated by Swashbuckle. When the specification contains a path with a request body content type of multipart/form-data the resulting generated code will look like
if (requestParameters.someDate !== undefined) {
formParams.append('someDate', requestParameters.someDate as any);
}
regardless of any format values provided in the specification. When the content type is application/json format values are honored. For example a property with a date-time format will call value.someDate.toISOString() when constructing the request as part of the <ModelName>ToJSON function
openapi-generator version
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "4.3.1"
}
}
OpenAPI declaration file content or url
{
"openapi": "3.0.1",
"info": {
"title": "Swashbuckle",
"description": "Swagger document by Swashbuckle",
"version": "v1"
},
"servers": [
{
"url": "https://someapi.com"
}
],
"paths": {
"/api/example/": {
"post": {
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/ExampleBody"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ExampleResponse"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ExampleBody": {
"type": "object",
"properties": {
"someBoolean": {
"type": "boolean"
},
"someDate": {
"type": "string",
"format": "date-time"
},
"someNumber": {
"type": "number",
"format": "double"
},
"someUuid": {
"type": "string",
"format": "uuid"
},
"someBinary": {
"type": "string",
"format": "binary",
"nullable": true
}
},
"additionalProperties": false
},
"ExampleResponse": {
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"someBoolean": {
"type": "boolean"
},
"someDate": {
"type": "string",
"format": "date-time"
},
"someNumber": {
"type": "number",
"format": "double"
},
"someUuid": {
"type": "string",
"format": "uuid"
},
"someBinary": {
"type": "string",
"format": "binary",
"nullable": true
}
},
"additionalProperties": false
}
}
}
}
Generation Details
openapi-generator-cli generate -i openapi.json -o ./src --generator-name typescript-fetch --additional-properties=typescriptThreePlus=true,supportsES6=true
Steps to reproduce
- create a openapi.json file with the contents above
- execute the open api generator command above to generate typescript code
- observe the generated code constructs form params with code like
formParams.append('someDate', requestParameters.someDate as any);for the someDate property even though someDate has a format attribute.
Related issues/PRs
Suggest a fix
The when appending form data properties the generated code should inspect the format attribute if provided and provide any necessary transforms
conceptually
switch(format) {
case 'date-time':
formParams.append({propertyName} requestParameters.{propertyName}.toISOString());
break;
case 'other-format':
...
default:
formParams.append({propertyName} requestParameters.{propertyName} as any);
}
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
Run the provided openapi-generator-cli command with the embedded openapi.json and inspect the generated typescript-fetch multipart request code. Trace the typescript-fetch generator's multipart form-parameter handling and add coverage for the date-time field; done means generated form data serializes someDate with the expected date-time representation while other fields remain correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100