OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.04k stars 6.38k forks source link

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

Open abillingsley opened 3 years ago

abillingsley commented 3 years ago

Bug Report Checklist

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);
} 
auto-labeler[bot] commented 3 years ago

šŸ‘ Thanks for opening this issue! šŸ· I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.