Skip to content

[BUG] typescript-fetch casts enum scalar values to Blob #11613

@fernard

Description

@fernard
Description

Currently, when the OpenAPI declaration file is being generated, the open-api-generator treats enum values (which are scalars, such as string and numbers) as complex, object-like structures in insists of casting them to Blob as a part of the multipart/form-data request payload.

openapi-generator version

5.3.1

OpenAPI declaration file content or url

This is the output from the FASTApi application, an endpoint expecting a multipart/form-data request with both file and some "metadata" describing this file.

(...)

  "components": {
    "schemas": {
      "Body_create_upload_form_data_request": {
        "title": "Body_create_upload_form_data_request",
        "required": [
          "file_type",
          "some_prop_1",
        ],
        "type": "object",
        "properties": {
          "source_file": {
            "title": "Source File",
            "type": "string",
            "format": "binary"
          },
          "file_type": {
            "$ref": "#/components/schemas/FileType"
          },
          "some_prop_1": {
            "title": "Some Prop",
            "type": "string"
          },
        }
      },

(...)
      "FileType": {
        "title": "FileType",
        "enum": [
          "MY_FILE_TYPE_1",
          "MY_FILE_TYPE_2",
          "MY_FILE_TYPE_3",

        ],
        "type": "string",
        "description": "An enumeration."
      },
(...)
Generation Details

The command-line script to run the docker container with open-api generator

docker run --add-host=host.docker.internal:host-gateway --rm \
    --user $(id -u):$(id -g) \
    -v "${PWD}:/local" openapitools/openapi-generator-cli:v5.3.1 generate \
    -i http://host.docker.internal/api/v1/openapi.json \ # all containers are run in the same docker-compose network, hence the address 
    -g typescript-fetch \
    --additional-properties=typescriptThreePlus=true \
    -o /som/dir 

The output of the generated code:

export enum FileType {
    MyFileType1 = 'MY_FILE_TYPE_1',
    MyFileType2 = 'MY_FILE_TYPE_1',
    MyFileType3 = 'MY_FILE_TYPE_1',
}

if (requestParameters.fileType !== undefined) {
   formParams.append(
       'file_type', new Blob(
       [JSON.stringify(FileTypeToJSON(requestParameters.fileType))],
       { type: "application/json" }
      )
   );
}
Steps to reproduce

Create an appropriate data structure in your code so that it leads to the OpenAPI declaration output as shown above

Suggest a fix

Treat enum value as a primitive and allow it to be directly taken as an argument for formData in multipart/form-data requests.

 if (requestParameters.fileType !== undefined) {
    formParams.append('file_type', requestParameters.fileType as any);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions