Skip to content

Request falls if FormData attachment is File instance #293

@NooNoo1337

Description

@NooNoo1337

Hi! I got issue with file format in FormData request. Library version: 9.0.2.

Next entity is described in OpenApi format:

 FileSegmentCreate:
      type: object
      required:
        - files
      properties:
        files:
          type: array
          items:
            type: string
            format: binary
// Auto generated TS interface
export interface FileSegmentCreate {
    files: File[];
}

When I make request with file it falls because of the incorrect file format. After research I have found the trouble point in generated code, contentFormatters object.

After contentFormatters code review I found out that:

  1. it's not allowed to have multiply files in same FormData key. There is no check for array input type;
  2. there is no check for Blob or File instance.
 // contentFormatters
 [ContentType.FormData]: (input: any) =>
      Object.keys(input || {}).reduce((data, key) => {
        data.append(key, input[key]);
        return data;
      }, new FormData()),

I'm able to resolve the instance issue on my side but It's hack.

//  createFileSegment expects that "files" key has File[] type;
const files = segment.files.map((file) => new Blob([file])) as any;
    
await api.createFileSegment({ ...segment, files: files[0] });

I noticed updates in 9.2.0 lib version that Blob instance check was added. However, the request will fall again if property is File instance or if it's Blob[] or Files[].

return Object.keys(input || {}).reduce((formData, key) => {
        const property = input[key];
        formData.append(
            key,
            property instanceof Blob ?
                property :
            typeof property === "object" && property !== null ?
                JSON.stringify(property) :
            `${property}`
        );
        return formData;
      }, new FormData())

Questions

  • is it able to add code to convert attachment to Blob if attachment has instance File?
  • is it able to add check for detecting few attachments?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions