多部分请求的 OpenAPI-Generator 实现不适用于多文件上传

Tim*_*ilt 6 multifile-uploader swagger typescript openapi openapi-generator

我们使用 OpenAPI 3.0.0 架构构建 API,并生成用于应用程序前端部分的打字稿代码。我们定义了一个端点,它应该能够使用多个文件并将它们上传到后端。这是端点的 requestbody 的样子:

requestBody:
  content:
    multipart/form-data:
      schema:
        properties:
          images:
            type: array
            items:
              type: string
              format: binary
        required:
          - images
Run Code Online (Sandbox Code Playgroud)

在有关文件上传的 OpenAPI 网页以及 Stackoverflow 上的多个帖子中也找到了类似的定义。

生成 API 时,images预计类型为Array<Blob>。当文件输入元素的输入在前端发生更改时,将调用端点。更具体地说,端点的调用方式如下:

const onInputChange = useCallback(() => {
  const files = inputRef.current.files; // React.useRef that's passed in as ref for the html input-element
  uploadImages(Array.from(files)); // uploadImages is the function that calls the endpoint
}, [inputRef, api]);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,API 验证始终返回状态代码为 400 - 错误请求的 HTTP 响应。查看 OpenAPI 生成器生成的实现,-propertyimages被附加到表单中,如下所示:

const onInputChange = useCallback(() => {
  const files = inputRef.current.files; // React.useRef that's passed in as ref for the html input-element
  uploadImages(Array.from(files)); // uploadImages is the function that calls the endpoint
}, [inputRef, api]);
Run Code Online (Sandbox Code Playgroud)

当我们现在查看此代码生成的 HTTP 请求时,我们可以在以下内容中看到类似的内容Form Data

------WebKitFormBoundaryItlGaGOpZu9sgAgS
Content-Disposition: form-data; name="images"

[object File],[object File]
------WebKitFormBoundaryItlGaGOpZu9sgAgS--
Run Code Online (Sandbox Code Playgroud)

[object File]其中为在选择文件对话框中选择的每个图像附加一个实例。

该端点之前可以用于单图像上传。在本例中,将图像附加到请求的生成代码是:

if (requestParameters.images) {
    formParams.append('images', requestParameters.images.join(runtime.COLLECTION_FORMATS["csv"]));
}
Run Code Online (Sandbox Code Playgroud)

Form Data请求中的内容如下所示:

------WebKitFormBoundaryIDujfCP9peNvyEE0
Content-Disposition: form-data; name="file"; filename="IMAGENAME.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryIDujfCP9peNvyEE0--
Run Code Online (Sandbox Code Playgroud)

在多文件情况下我们调用端点是否错误?此功能是否无法与 Typescript 的 OpenAPI 生成器一起正常工作?

如果需要,我很乐意提供更多信息。