From 1e01c380e85cb9fe6449528bf75287dbce07f548 Mon Sep 17 00:00:00 2001 From: Sebastien Rosset Date: Thu, 23 Apr 2020 22:59:31 -0700 Subject: [PATCH] [python-experimental] Minor doc update, code comments and exception handling (#5945) * add support for any type, i.e. when 'type' attribute is not specified in OAS schema * fix typos, add code comments * Handle case when 'type' attribute is not present in the OAS schema * fix python formatting rule * fix python formatting rule * remove 'object' as a type --- .../python-experimental/model_utils.mustache | 14 ++++++++------ .../petstore_api/model_utils.py | 14 ++++++++------ .../petstore_api/model_utils.py | 14 ++++++++------ 3 files changed, 24 insertions(+), 18 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 8b60618c1da..58da408a19e 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 @@ -74,7 +74,7 @@ COERCION_INDEX_BY_TYPE = { ModelComposed: 0, ModelNormal: 1, ModelSimple: 2, - none_type: 3, + none_type: 3, # The type of 'None'. list: 4, dict: 5, float: 6, @@ -83,7 +83,7 @@ COERCION_INDEX_BY_TYPE = { datetime: 9, date: 10, str: 11, - file_type: 12, + file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type. } # these are used to limit what type conversions we try to do @@ -352,11 +352,11 @@ def order_response_types(required_types): Args: required_types (list/tuple): collection of classes or instance of - list or dict with classs information inside it + list or dict with class information inside it. Returns: (list): coercion order sorted collection of classes or instance - of list or dict with classs information inside it + of list or dict with class information inside it. """ def index_getter(class_or_instance): @@ -373,7 +373,9 @@ def order_response_types(required_types): elif (inspect.isclass(class_or_instance) and issubclass(class_or_instance, ModelSimple)): return COERCION_INDEX_BY_TYPE[ModelSimple] - return COERCION_INDEX_BY_TYPE[class_or_instance] + elif class_or_instance in COERCION_INDEX_BY_TYPE: + return COERCION_INDEX_BY_TYPE[class_or_instance] + raise ApiValueError("Unsupported type: %s" % class_or_instance) sorted_types = sorted( required_types, @@ -391,7 +393,7 @@ def remove_uncoercible(required_types_classes, current_item, from_server, these should be ordered by COERCION_INDEX_BY_TYPE from_server (bool): a boolean of whether the data is from the server if false, the data is from the client - current_item (any): the current item to be converted + current_item (any): the current item (input data) to be converted Keyword Args: must_convert (bool): if True the item to convert is of the wrong 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 2f6b690424b..b905f7d347a 100644 --- a/samples/client/petstore/python-experimental/petstore_api/model_utils.py +++ b/samples/client/petstore/python-experimental/petstore_api/model_utils.py @@ -336,7 +336,7 @@ COERCION_INDEX_BY_TYPE = { ModelComposed: 0, ModelNormal: 1, ModelSimple: 2, - none_type: 3, + none_type: 3, # The type of 'None'. list: 4, dict: 5, float: 6, @@ -345,7 +345,7 @@ COERCION_INDEX_BY_TYPE = { datetime: 9, date: 10, str: 11, - file_type: 12, + file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type. } # these are used to limit what type conversions we try to do @@ -614,11 +614,11 @@ def order_response_types(required_types): Args: required_types (list/tuple): collection of classes or instance of - list or dict with classs information inside it + list or dict with class information inside it. Returns: (list): coercion order sorted collection of classes or instance - of list or dict with classs information inside it + of list or dict with class information inside it. """ def index_getter(class_or_instance): @@ -635,7 +635,9 @@ def order_response_types(required_types): elif (inspect.isclass(class_or_instance) and issubclass(class_or_instance, ModelSimple)): return COERCION_INDEX_BY_TYPE[ModelSimple] - return COERCION_INDEX_BY_TYPE[class_or_instance] + elif class_or_instance in COERCION_INDEX_BY_TYPE: + return COERCION_INDEX_BY_TYPE[class_or_instance] + raise ApiValueError("Unsupported type: %s" % class_or_instance) sorted_types = sorted( required_types, @@ -653,7 +655,7 @@ def remove_uncoercible(required_types_classes, current_item, from_server, these should be ordered by COERCION_INDEX_BY_TYPE from_server (bool): a boolean of whether the data is from the server if false, the data is from the client - current_item (any): the current item to be converted + current_item (any): the current item (input data) to be converted Keyword Args: must_convert (bool): if True the item to convert is of the wrong 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 2f6b690424b..b905f7d347a 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 @@ -336,7 +336,7 @@ COERCION_INDEX_BY_TYPE = { ModelComposed: 0, ModelNormal: 1, ModelSimple: 2, - none_type: 3, + none_type: 3, # The type of 'None'. list: 4, dict: 5, float: 6, @@ -345,7 +345,7 @@ COERCION_INDEX_BY_TYPE = { datetime: 9, date: 10, str: 11, - file_type: 12, + file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type. } # these are used to limit what type conversions we try to do @@ -614,11 +614,11 @@ def order_response_types(required_types): Args: required_types (list/tuple): collection of classes or instance of - list or dict with classs information inside it + list or dict with class information inside it. Returns: (list): coercion order sorted collection of classes or instance - of list or dict with classs information inside it + of list or dict with class information inside it. """ def index_getter(class_or_instance): @@ -635,7 +635,9 @@ def order_response_types(required_types): elif (inspect.isclass(class_or_instance) and issubclass(class_or_instance, ModelSimple)): return COERCION_INDEX_BY_TYPE[ModelSimple] - return COERCION_INDEX_BY_TYPE[class_or_instance] + elif class_or_instance in COERCION_INDEX_BY_TYPE: + return COERCION_INDEX_BY_TYPE[class_or_instance] + raise ApiValueError("Unsupported type: %s" % class_or_instance) sorted_types = sorted( required_types, @@ -653,7 +655,7 @@ def remove_uncoercible(required_types_classes, current_item, from_server, these should be ordered by COERCION_INDEX_BY_TYPE from_server (bool): a boolean of whether the data is from the server if false, the data is from the client - current_item (any): the current item to be converted + current_item (any): the current item (input data) to be converted Keyword Args: must_convert (bool): if True the item to convert is of the wrong