swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

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

Open mike-res opened this issue 1 year ago • 1 comments

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.

mike-res avatar Feb 18 '24 23:02 mike-res

I switched to using swagger-codegen-cli-2.4.39.jar so that I could generate client code automatically as part of our CI/CD flow, and the issues cited above are present in swagger-codegen-cli-2.4.39.jar .. hopefully that fills in some of the blanks for which versions of the code generator have this issue..

mike-res avatar Feb 20 '24 04:02 mike-res