swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.04k stars 6.03k forks source link

[PYTHON] Error when receiving .zip file octet-stream response from API endpoint #12311

Open NA-Dev opened 10 months ago

NA-Dev commented 10 months ago
Description

I generated a client to consume an API. One of the endpoints returns an octet-stream of a zipped file with .zip extension. When receiving this file, I get the following error trace. I get that this is to do with encoding, but I have no idea how to make the codegen generate code that works. When I try chardet it says {'encoding': None, 'confidence': 0.0, 'language': None}. When I fetch the report in Postman, I can save the raw response as a zip file and extract a csv file with no issues.

  File "/home/user/dev/my_sdk/api_client.py", line 328, in call_api
    return self.__call_api(resource_path, method,
  File "/home/user/dev/my_sdk/api_client.py", line 156, in __call_api
    response_data = self.request(
  File "/home/user/dev/my_sdk/api_client.py", line 350, in request
    return self.rest_client.GET(url,
  File "/home/user/dev/my_sdk/rest.py", line 257, in GET
    return self.request("GET", url,
  File "/home/user/dev/my_sdk/rest.py", line 226, in request
    r.data = r.data.decode('utf8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in position 10: invalid start byte
Swagger-codegen version

3.0.30

Swagger declaration file content or url
openapi: "3.0.0"
info:
  title: My API 
  description: >-
    Contact Support:
     Name: No Contact
     Email: email@example.com
  version: 1.0.0
servers:
  - url: '{server}'
    variables:
      server:
        default: https://myserver.com
components:
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic    
security:
  - basicAuth: []
paths:
  /fetchZipFile:
    get:
      operationId: getZipFile
      summary: getZipFile
      parameters:
        - name: Accept
          in: header
          schema:
            type: string
          example: application/octet-stream
        - name: Content-Type
          in: header
          schema:
            type: string
          example: application/octet-stream
        - name: date
          in: query
          schema:
            type: integer
          example: '09122023'
      responses:
        '200':
          description: OK
          headers:
            Accept-Ranges:
              schema:
                type: string
                example: bytes
            Content-Disposition:
              schema:
                type: number
                example: >-
                  attachment;filename=Report_2023-09-14.zip
            Content-Type:
              schema:
                type: string
                example: application/octet-stream
            Content-Length:
              schema:
                type: integer
                example: '189'
            Cache-Control:
              schema:
                type: string
                example: max-age=0, no-cache, no-store
            Date:
              schema:
                type: string
                example: Wed, 09 Aug 2023 18:43:13 GMT
            Connection:
              schema:
                type: string
                example: keep-alive
          content:
            application/octet-stream:
              schema:
                type: file
Command line used for generation

java -jar swagger-codegen-cli.jar generate \ -i openapi-spec.yaml \ -l python \ -o generated_code \ -c config.json

Steps to reproduce

Use the generated client to request the zip file, and you get the error

Related issues/PRs
Suggest a fix/enhancement
NA-Dev commented 10 months ago

I think this is a file that needs to be saved without decoding. Can I generate a client that does that, or do I need to set _preload_content to False, and handle it all manually?