Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'multipart/form-data' formatter isn't recursive #462

Open
Clovel opened this issue Nov 22, 2022 · 3 comments
Open

'multipart/form-data' formatter isn't recursive #462

Clovel opened this issue Nov 22, 2022 · 3 comments

Comments

@Clovel
Copy link

Clovel commented Nov 22, 2022

When using a multipart/form-data content type, the generated API code provides a private function to format certain types : contentFormatters.

Currently on version 12.0.2 the function is :

private contentFormatters: Record<ContentType, (input: any) => any> = {
  [ContentType.Json]: (input: any) =>
    input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
  [ContentType.FormData]: (input: any) =>
    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()),
  [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
};

If we concentrate on the ContentType.FormData section of the formatter, we have

(input: any) =>
    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()),

This section is no recursive on objects.

For example, if my backend expects the following type for the payload

interface PayloadWithData {
  items: {
    name: string;
    content: Blob;
    appSpecificData: '1234567896',
  }[];
}

The files aren't correctly set in the fetch request because they aren't serializable.

This should be updated to support File in subfields of the payload type.

EDIT 1 : Indentation
EDIT 2 : this looks like it's the continuation of the work done here : #293
EDIT 3 : Typo

@thomsocialbrothers
Copy link

Also, it doesn't support arrays.. Maybe adding object-to-formdata as a dependency would be a good idea?

@Clovel
Copy link
Author

Clovel commented Feb 7, 2023

Any news on this ? Has anyone taken a look at how this would be fixed ?

@MKraust
Copy link

MKraust commented Jul 1, 2024

I think it would be okay to make contentFormatters property at least protected to be able to override it easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants