Skip to content

MediaType is not encoded properly in multipart/form-data (application/octet-stream is not supported) #869

@usersina

Description

@usersina

What are the steps to reproduce this issue?

Given the following schema, generate the client types

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: 'http://localhost:8080'
    description: Generated server url
paths:
  /test:
    post:
      tags:
        - hello-controller
      operationId: saveDocu2ment
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                personDTO:
                  $ref: '#/components/schemas/PersonDTO'
                file:
                  type: string
                  format: binary
            encoding:
              personDTO:
                contentType: application/json
      responses:
        '200':
          description: OK
components:
  schemas:
    PersonDTO:
      type: object
      properties:
        email:
          type: string
        firstName:
          type: string
        lastName:
          type: string

What happens?

For the upload function using client: "react-query", this is what's generated

export const useUploadFilesHook = () => {
  const uploadFiles = useCustomInstance<UploadFiles200>();

  return (uploadFilesBody: UploadFilesBody) => {
    const formData = new FormData();
    formData.append("personDTO", JSON.stringify(uploadFilesBody.personDTO));
    formData.append("file", uploadFilesBody.file);

    return uploadFiles({  
      url: "/test",
      method: "post",
      headers: { "Content-Type": "multipart/form-data" },
      data: formData,
    });
  };
};

However, this is broken and will result in the following error

{
  "status": 415,
  "error": "Unsupported Media Type",
  "message": "Content type 'application/octet-stream' not supported"
}

The reason for this, is that the personDTO field is expected to be encoded in application/json and not in the default application/octet-stream.

What were you expecting to happen?

Instead, this is what works if I manually update the file

export const useUploadFilesHook = () => {
  const uploadFiles = useCustomInstance<UploadFiles200>();

  return (uploadFilesBody: UploadFilesBody) => {
    const formData = new FormData();
    formData.append("personDTO", new Blob([JSON.stringify(uploadFilesBody.personDTO)], { type: "application/json" }));
    formData.append("file", uploadFilesBody.file);

    return uploadFiles({  
      url: "/test",
      method: "post",
      headers: { "Content-Type": "multipart/form-data" },
      data: formData,
    });
  };
};

Any other comments?

This issue is very closely related.

What versions are you using?

Package Version: ^6.15.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    workaroundA workaround has been provided

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions