【问题标题】:Openapi generator angular not generating multipart formdata endpoint correctly. (useForm is false by default)Openapi 生成器角度未正确生成多部分表单数据端点。 (useForm 默认为 false)
【发布时间】:2021-11-09 15:16:18
【问题描述】:

这是我拥有的端点:

@PostMapping(value = "/file-upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public List<FileReference> handleFileUpload(
        @RequestPart(value = "file", name = "file") MultipartFile[] file, @ApiIgnore HttpSession session) {
    return service.handleFileUpload(
            Arrays.stream(file).map(MultipartFileWithUUID::new).collect(Collectors.toList()),
            session);
}

这是在 swagger.json (swagger 2.0) 中生成的端点:

...
  "post": {
        "tags": [
          "damage-report-controller"
        ],
        "summary": "handleFileUpload",
        "operationId": "handleFileUploadUsingPOST",
        "consumes": [
          "multipart/form-data"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "file",
            "in": "formData",
            "required": false,
            "type": "array",
            "items": {
              "type": "file"
            },
            "collectionFormat": "multi"
          }
        ],
...

这是生成的函数:

 public handleFileUploadUsingPOST(file?: Array<Blob> ...) {

let headers = this.defaultHeaders;

header settings etc...

        // to determine the Content-Type header
        const consumes: string[] = [
            'multipart/form-data'
        ];


        const canConsumeForm = this.canConsumeForm(consumes);

        let formParams: { append(param: string, value: any): any; };
        let useForm = false;

...
        if (useForm) {
            formParams = new FormData();
        } else {
            formParams = new HttpParams({encoder: this.encoder});
        }
...
}

我的错误是415: Unsupported media type.

我不知道它应该如何正确生成,但我将 let useForm; 更改为 true 并且它可以工作, 所以我猜let useForm = canConsumeForm(consumes) 因为canConsumeForm 返回一个布尔值。

我应该改变什么才能正确生成?

【问题讨论】:

    标签: java angular spring typescript swagger


    【解决方案1】:

    如果有人读到这篇文章,我还没有找到使用 swagger 2.0 的正确解决方案,所以我更新到 openapi 3.0 并解决了问题。

    Apparently swagger 2.0 doesn't support uploading an array of files,尽管唯一的问题是生成的服务没有正确使用现有功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多