swagger-api / swagger-api/swagger-codegen

[PYTHON] Two patches in python-client/swagger_client/api_client.py

Open
#12,345 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

The code in python-client/swagger_client/api_client.py contains code for the ApiClient class.

I have found that two methods in the generated ApiClient class need to be patched in order ot ensure reliable operation:

  • In ApiClient::__deserialize_primitive(self, data, klass), there are only exception handler cases for two exception types, UnicodeEncodeError and TypeError. I have found it necessary to add a third case, to handle "all other errors". The code that I used for this is the following (the last two lines are the change).
        try:
            return klass(data)
        except UnicodeEncodeError:
            return six.text_type(data)
        except TypeError:
            return data
        except:
            return data
  • In ApiClient::__deserialize_model(self, data, klass): When a response calls to a model in order to deserialize it, the ApiClient should add its self.configuration to the kwargs that it passes to the constructor of the object that handles the deserialization. For example, this will allow the model to inherit the client_side_validation property from the Config instance that was used to instantiate the ApiClient. This is useful, for example, if one wants to set Configuration.client_side_validation to false in order to inhibit strict type-checking. I nade the following change in ApiClient::__deserialize_model in order to have deserialixation models inherit the ApiClient's Config instance.
         kwargs = {}
        if klass.swagger_types is not None:
           # ***** ADDED THE FOLLOWING LINE *****
            kwargs["_configuration"] = self.configuration
            for attr, attr_type in six.iteritems(klass.swagger_types):
                if (data is not None and
                        klass.attribute_map[attr] in data and
                        isinstance(data, (list, dict))):
                    value = data[klass.attribute_map[attr]]
                    kwargs[attr] = self.__deserialize(value, attr_type)
Swagger-codegen version

Unclear which version ie being used. I was using https://editor.swagger.io/) and https://editor-next.swagger.io/. Both of the online editors have the same issue.

Swagger declaration file content or url

No swagger: '2.0' code provided .. this issue is present in any code that the online python client generator produces.

Command line used for generation

used online editors (https://editor.swagger.io/) and https://editor-next.swagger.io/)

Steps to reproduce

Thiese issues are present in any python client produced by either https://editor.swagger.io/) and https://editor-next.swagger.io/.

Related issues/PRs
Suggest a fix/enhancement

Examples of preferred resulting code in the ApiClient::__deserialize_primitive(self, data, klass) and ApiClient::__deserialize_model(self, data, klass) methods appear above.

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 with python-client/swagger_client/api_client.py and inspect ApiClient.__deserialize_primitive and ApiClient.__deserialize_model. Confirm how the generated client handles unexpected deserialization errors and passes configuration into model deserialization. Done means both reported behaviors are covered in the generator's output and verified against a representative generated Python client.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.