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

Question: is there a way to override the generated Blob type to be File? #1825

Open
rossmacarthur opened this issue Oct 11, 2023 · 8 comments

Comments

@rossmacarthur
Copy link

rossmacarthur commented Oct 11, 2023

I have a {"type": "string", "format": "binary"} in my OpenAPI spec and it is being generated like this

export type UploadFileRequest = {
  file: Blob
}

I'd like this to be

export type UploadFileRequest = {
  file: File
}

Is there anyway to override this, perhaps by changing something in the OpenAPI spec?

@rossmacarthur rossmacarthur changed the title Question: is there a way to override the generated type? Question: is there a way to override the generated Blob type to be File? Oct 11, 2023
@mbergen
Copy link

mbergen commented Oct 18, 2023

Shouldn't you be able to just pass any File object there, as File inherits from Blob?

@rossmacarthur
Copy link
Author

Shouldn't you be able to just pass any File object there, as File inherits from Blob?

Yes, but I want to require a file because the server requires a filename and content-type.

@mbergen
Copy link

mbergen commented Oct 19, 2023

To pass a filename, you will need to use multipart upload, as described here: https://swagger.io/docs/specification/describing-request-body/file-upload/

@tajnymag
Copy link
Contributor

tajnymag commented Mar 2, 2024

@mbergen At the document you linked, single file uploads are described as:

requestBody:
  content:
    multipart/form-data:
      schema:
        filename:
          type: string
          format: binary

and multi file uploads as:

requestBody:
  content:
    multipart/form-data:
      schema:
        type: array
        items:
          filename:
            type: string
            format: binary

Both of which are described to correspond to an HTTP request like this:

POST /upload
Host: example.com
Content-Length: 2740
Content-Type: multipart/form-data; boundary=abcde12345
--abcde12345
Content-Disposition: form-data; name="fileName"; filename="file1.txt"
Content-Type: text/plain

Without openapi-typescript-codegen supporting sending File objects, filename and Content-Type really cant be included in the request as @rossmacarthur said.

As I need to upload file in my API, I have only three options:

  1. fork the tool and fix the issue myself, making me lose the deadline
  2. use the official java based generator
  3. single out file handling options into their own manually written functions

@tajnymag
Copy link
Contributor

tajnymag commented Mar 2, 2024

@rossmacarthur It's been 5 months. What did you use in the end?

@mbergen
Copy link

mbergen commented Mar 2, 2024

@mbergen At the document you linked, single file uploads are described as:

requestBody:
  content:
    multipart/form-data:
      schema:
        filename:
          type: string
          format: binary

and multi file uploads as:

requestBody:
  content:
    multipart/form-data:
      schema:
        type: array
        items:
          filename:
            type: string
            format: binary

Both of which are described to correspond to an HTTP request like this:

These are both the versions that specify to take a additional filename property in the spec, which the original comment here did not include (that's also documented in the link, the first example)

@tajnymag
Copy link
Contributor

tajnymag commented Mar 2, 2024

@mbergen Sorry, embarrassed I missed that, you are right.

My point still stands though. I've tried the example specifications and the generated client only accepts Blob either way.

/hosted/core/files/{path}:
    post:
      operationId: uploadContent
      parameters:
        - $ref: '#/components/parameters/path'
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                filename:
                  type: string
                  format: binary

||
V

public static uploadContent(
        formData: {
            filename?: Blob;
        },
        path: string = '',
    ): CancelablePromise<(FileMetadata | DirectoryMetadata)> {
        return __request(OpenAPI, {
            method: 'POST',
            url: '/hosted/core/files/{path}',
            path: {
                'path': path,
            },
            formData: formData,
            mediaType: 'multipart/form-data',
            errors: {
                409: `Path already exists`,
            },
        });
    }

@jordanshatford
Copy link

Check out our fork of this repository @hey-api/openapi-ts. We have updated the type to be Blob | File (since File extends Blob). If you need to narrow the type, you can do so in your code. Please let us know if you run into any further issue by creating an issue in our repository. Thanks.

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

4 participants