Search code examples
angularswagger-codegen

How to specify OpenAPI for binary download in angular


I have a method to download a binary file. This currently has the following spec in 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: {}

The generated typescript-angular code contains this call to httpClient:

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

This way, angular seems unable to receive the binary content. I had to change the httpClient, use the non-type-variable signature of post and add responseType: blob to the parameters

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

Is this a bug / missing feature in Swagger codegen or should I change the openapi definition to get working angular code?


Solution

  • To indicate that the response is a binary file, use a schema with type: string with format: binary.

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