swagger-api / swagger-api/swagger-codegen

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

Open
#12,311 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the generated rest.py at the request path where r.data is decoded as UTF-8, then trace how api_client.py handles the response for the octet-stream endpoint. Verify that binary .zip data is returned without UnicodeDecodeError while normal text responses still behave as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.