OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] multipart/form-data request body does not respect date-time format

Open
#7,584 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript Issue: Bug
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
  1. create a openapi.json file with the contents above
  2. execute the open api generator command above to generate typescript code
  3. 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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.