From 19e14237aa3de53facbb65800fcfb3c7f8bb53f6 Mon Sep 17 00:00:00 2001 From: Sebastien Rosset Date: Fri, 22 May 2020 09:44:15 -0700 Subject: [PATCH] [python-experimental] Add support for pep 3134, attach cause of exception (#6388) * Mustache template should use invokerPackage tag to generate import * Add exception cause * using six module for exception chaining in Python 3.x --- .../python/python-experimental/model_utils.mustache | 10 +++++----- .../python-experimental/petstore_api/model_utils.py | 10 +++++----- .../python-experimental/petstore_api/model_utils.py | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/model_utils.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/model_utils.mustache index 7c566933a5c..c1c3c934aa3 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/model_utils.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/model_utils.mustache @@ -725,14 +725,14 @@ def deserialize_primitive(data, klass, path_to_item): # '7' -> 7.0 -> '7.0' != '7' raise ValueError('This is not a float') return converted_value - except (OverflowError, ValueError): + except (OverflowError, ValueError) as ex: # parse can raise OverflowError - raise ApiValueError( + six.raise_from(ApiValueError( "{0}Failed to parse {1} as {2}".format( additional_message, repr(data), get_py3_class_name(klass) ), path_to_item=path_to_item - ) + ), ex) def get_discriminator_class(model_class, @@ -1220,7 +1220,7 @@ def get_allof_instances(self, model_args, constant_args): allof_instance = allof_class(**kwargs) composed_instances.append(allof_instance) except Exception as ex: - raise ApiValueError( + six.raise_from(ApiValueError( "Invalid inputs given to generate an instance of '%s'. The " "input data was invalid for the allOf schema '%s' in the composed " "schema '%s'. Error=%s" % ( @@ -1229,7 +1229,7 @@ def get_allof_instances(self, model_args, constant_args): self.__class__.__name__, str(ex) ) - ) + ), ex) return composed_instances diff --git a/samples/client/petstore/python-experimental/petstore_api/model_utils.py b/samples/client/petstore/python-experimental/petstore_api/model_utils.py index 03012553a6a..9070305650a 100644 --- a/samples/client/petstore/python-experimental/petstore_api/model_utils.py +++ b/samples/client/petstore/python-experimental/petstore_api/model_utils.py @@ -992,14 +992,14 @@ def deserialize_primitive(data, klass, path_to_item): # '7' -> 7.0 -> '7.0' != '7' raise ValueError('This is not a float') return converted_value - except (OverflowError, ValueError): + except (OverflowError, ValueError) as ex: # parse can raise OverflowError - raise ApiValueError( + six.raise_from(ApiValueError( "{0}Failed to parse {1} as {2}".format( additional_message, repr(data), get_py3_class_name(klass) ), path_to_item=path_to_item - ) + ), ex) def get_discriminator_class(model_class, @@ -1487,7 +1487,7 @@ def get_allof_instances(self, model_args, constant_args): allof_instance = allof_class(**kwargs) composed_instances.append(allof_instance) except Exception as ex: - raise ApiValueError( + six.raise_from(ApiValueError( "Invalid inputs given to generate an instance of '%s'. The " "input data was invalid for the allOf schema '%s' in the composed " "schema '%s'. Error=%s" % ( @@ -1496,7 +1496,7 @@ def get_allof_instances(self, model_args, constant_args): self.__class__.__name__, str(ex) ) - ) + ), ex) return composed_instances diff --git a/samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py b/samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py index 03012553a6a..9070305650a 100644 --- a/samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py +++ b/samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py @@ -992,14 +992,14 @@ def deserialize_primitive(data, klass, path_to_item): # '7' -> 7.0 -> '7.0' != '7' raise ValueError('This is not a float') return converted_value - except (OverflowError, ValueError): + except (OverflowError, ValueError) as ex: # parse can raise OverflowError - raise ApiValueError( + six.raise_from(ApiValueError( "{0}Failed to parse {1} as {2}".format( additional_message, repr(data), get_py3_class_name(klass) ), path_to_item=path_to_item - ) + ), ex) def get_discriminator_class(model_class, @@ -1487,7 +1487,7 @@ def get_allof_instances(self, model_args, constant_args): allof_instance = allof_class(**kwargs) composed_instances.append(allof_instance) except Exception as ex: - raise ApiValueError( + six.raise_from(ApiValueError( "Invalid inputs given to generate an instance of '%s'. The " "input data was invalid for the allOf schema '%s' in the composed " "schema '%s'. Error=%s" % ( @@ -1496,7 +1496,7 @@ def get_allof_instances(self, model_args, constant_args): self.__class__.__name__, str(ex) ) - ) + ), ex) return composed_instances