swagger-api / swagger-api/swagger-codegen
[PYTHON] Two patches in python-client/swagger_client/api_client.py
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, theApiClientshould 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 theclient_side_validationproperty from the Config instance that was used to instantiate theApiClient. This is useful, for example, if one wants to setConfiguration.client_side_validationto 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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