如何在角度中为二进制下载指定OpenAPI

时间:2018-11-14 22:05:18

标签: angular swagger-codegen

我有一种下载二进制文件的方法。目前,openapi.yaml中具有以下规范:

  /processing/getZippedReports:
    post:
      tags:
      - ProcessingRest
      description: Get the reports
      operationId: getZippedReports
      requestBody:
        description: The list of items for which to get the HTML
        content:
          application/json:
            schema:
              type: array
              items:
                type: string
        required: true
      responses:
        200:
          description: file with zipped reports
          content:
            application/octet-stream: {}

生成的typescript-角度代码包含对httpClient的此调用:

return this.httpClient.post<any>(`${this.configuration.basePath}/processing/getZippedReports`,
    requestBody,
    {
        withCredentials: this.configuration.withCredentials,
        headers: headers,
        observe: observe,
        reportProgress: reportProgress
    }
);

这样,angular似乎无法接收二进制内容。我不得不更改httpClient,使用post的非类型变量签名,并将responseType: blob添加到参数

return this.httpClient.post(`${this.configuration.basePath}/processing/getZippedReports`,
    requestBody,
    {
        withCredentials: this.configuration.withCredentials,
        headers: headers,
        observe: 'response',
        responseType: 'blob',
        reportProgress: reportProgress
    }
);

这是Swagger Codegen中的错误/缺失功能吗?还是应该更改openapi定义以获得有效的角度代码?

2 个答案:

答案 0 :(得分:1)

要指示响应是二进制文件,请使用带有type: stringformat: binary的架构。

          content:
            application/octet-stream:
              schema:
                type: string
                format: binary

答案 1 :(得分:1)

似乎有一个responseType供应商扩展,我在源代码中找到了它,但是没有关于它的文档。这会导致controllerService.ts

中的正确 "responses": { "default": { "x-is-file": true, "description": "default response", "content": { "application/octet-stream": { "schema": { "type": "file" } } } } }, 定义
{{1}}