ferdikoomen / openapi-typescript-codegen

NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification
MIT License
2.92k stars 519 forks source link

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

Open rossmacarthur opened 1 year ago

rossmacarthur commented 1 year ago

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?

mbergen commented 12 months ago

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

rossmacarthur commented 12 months ago

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 commented 12 months ago

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 commented 7 months ago

@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 commented 7 months ago

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

mbergen commented 7 months ago

@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 commented 7 months ago

@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 commented 6 months ago

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.