Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] response.data has type "str" but file opened with "wb" mode, need be "w" #10948

Closed
goDeni opened this issue Mar 23, 2021 · 0 comments
Closed

Comments

@goDeni
Copy link

goDeni commented Mar 23, 2021

Description
Swagger-codegen version

3.0.25

Swagger declaration file content or url
swagger: '2.0'
info:
  title: Some API
  description: Some API
  version: "1.0.0"
schemes:
  - http
produces:
  - application/json
paths:
  /export:
    get:
      operationId: export_op
      x-swagger-router-controller: source
      summary: Some export
      tags:
        - Sources
      responses:
        200:
          description: done
          schema:
            type: file
Command line used for generation
docker run --rm \
    -v $(pwd)/some.yml:/some.yml \
    -v $(pwd)/result:/tmp/result \
    swaggerapi/swagger-codegen-cli-v3:3.0.25 \
    generate -i /some.yml -l python -o /tmp/result
Steps to reproduce

Just execute command

Suggest a fix/enhancement

I'll try fix this

BUG

in result/swagger_client/api_client.py file
in __deserialize_file method:

def __deserialize_file(self, response):
        """Deserializes body to file

        Saves response body into a file in a temporary folder,
        using the filename from the `Content-Disposition` header if provided.

        :param response:  RESTResponse.
        :return: file path.
        """
        fd, path = tempfile.mkstemp(dir=self.configuration.temp_folder_path)
        os.close(fd)
        os.remove(path)

        content_disposition = response.getheader("Content-Disposition")
        if content_disposition:
            filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',
                                 content_disposition).group(1)
            path = os.path.join(os.path.dirname(path), filename)
        
        with open(path, "wb") as f:
            f.write(response.data)

        return path

Error in block with writing to file
response.data has type "str" but file opened with "wb" mode, need be "w"

@goDeni goDeni changed the title [Python] on respose type "file", generated code got "str" type [Python] response.data has type "str" but file opened with "wb" mode, need be "w" Mar 24, 2021
@HugoMario HugoMario mentioned this issue Mar 28, 2021
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant