Carapacik / swagger_parser

Dart package that takes an OpenApi definition file and generates REST clients based on retrofit and data classes for your project.
https://pub.dev/packages/swagger_parser
MIT License
87 stars 33 forks source link

Ability to download file #242

Open dlabs-matic-leva opened 2 weeks ago

dlabs-matic-leva commented 2 weeks ago

Use case

I'd like to consume endpoint that generates .pdf file. This is OpenAPI schema for such endpoint:

    "/api/transfers/export.pdf": {
      "get": {
        "operationId": "exportPdf",
        "summary": "",
        "parameters": [
          {
            "name": "filter",
            "required": false,
            "in": "query",
            "style": "deepObject",
            "explode": true,
            "schema": {
              "$ref": "#/components/schemas/ExportTransfersFilters"
            }
          }
        ],
        "responses": {
          "200": {
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            },
            "description": ""
          }
        },
        "tags": [
          "Transfers"
        ],
        "security": [
          {
            "oauth2": [

            ]
          }
        ]
      }
    },

Currently swagger_parser generates such method:

  @GET('/api/transfers/export.pdf')
  Future<String> exportPdf({
    @Query('filter') ExportTransfersFilters? filter,
  });

which in turn generates Dio.fetch call with ResponseType == ResponseType.json. In case of PDF, this yields corrupted file.

Proposal

Such Retrofit definition will generate correct Dio.fetch call:

  @GET('/api/transfers/export.pdf')
  @DioResponseType(ResponseType.bytes)
  Future<HttpResponse> exportPdf({
    @Query('filter') ExportTransfersFilters? filter,
  });