Skip to content

Commit 1e01c38

Browse files
[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
1 parent 93dd4a5 commit 1e01c38

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

modules/openapi-generator/src/main/resources/python/python-experimental/model_utils.mustache

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ COERCION_INDEX_BY_TYPE = {
7474
ModelComposed: 0,
7575
ModelNormal: 1,
7676
ModelSimple: 2,
77-
none_type: 3,
77+
none_type: 3, # The type of 'None'.
7878
list: 4,
7979
dict: 5,
8080
float: 6,
@@ -83,7 +83,7 @@ COERCION_INDEX_BY_TYPE = {
8383
datetime: 9,
8484
date: 10,
8585
str: 11,
86-
file_type: 12,
86+
file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type.
8787
}
8888

8989
# these are used to limit what type conversions we try to do
@@ -352,11 +352,11 @@ def order_response_types(required_types):
352352

353353
Args:
354354
required_types (list/tuple): collection of classes or instance of
355-
list or dict with classs information inside it
355+
list or dict with class information inside it.
356356

357357
Returns:
358358
(list): coercion order sorted collection of classes or instance
359-
of list or dict with classs information inside it
359+
of list or dict with class information inside it.
360360
"""
361361

362362
def index_getter(class_or_instance):
@@ -373,7 +373,9 @@ def order_response_types(required_types):
373373
elif (inspect.isclass(class_or_instance)
374374
and issubclass(class_or_instance, ModelSimple)):
375375
return COERCION_INDEX_BY_TYPE[ModelSimple]
376-
return COERCION_INDEX_BY_TYPE[class_or_instance]
376+
elif class_or_instance in COERCION_INDEX_BY_TYPE:
377+
return COERCION_INDEX_BY_TYPE[class_or_instance]
378+
raise ApiValueError("Unsupported type: %s" % class_or_instance)
377379

378380
sorted_types = sorted(
379381
required_types,
@@ -391,7 +393,7 @@ def remove_uncoercible(required_types_classes, current_item, from_server,
391393
these should be ordered by COERCION_INDEX_BY_TYPE
392394
from_server (bool): a boolean of whether the data is from the server
393395
if false, the data is from the client
394-
current_item (any): the current item to be converted
396+
current_item (any): the current item (input data) to be converted
395397

396398
Keyword Args:
397399
must_convert (bool): if True the item to convert is of the wrong

samples/client/petstore/python-experimental/petstore_api/model_utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def __eq__(self, other):
336336
ModelComposed: 0,
337337
ModelNormal: 1,
338338
ModelSimple: 2,
339-
none_type: 3,
339+
none_type: 3, # The type of 'None'.
340340
list: 4,
341341
dict: 5,
342342
float: 6,
@@ -345,7 +345,7 @@ def __eq__(self, other):
345345
datetime: 9,
346346
date: 10,
347347
str: 11,
348-
file_type: 12,
348+
file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type.
349349
}
350350

351351
# these are used to limit what type conversions we try to do
@@ -614,11 +614,11 @@ def order_response_types(required_types):
614614
615615
Args:
616616
required_types (list/tuple): collection of classes or instance of
617-
list or dict with classs information inside it
617+
list or dict with class information inside it.
618618
619619
Returns:
620620
(list): coercion order sorted collection of classes or instance
621-
of list or dict with classs information inside it
621+
of list or dict with class information inside it.
622622
"""
623623

624624
def index_getter(class_or_instance):
@@ -635,7 +635,9 @@ def index_getter(class_or_instance):
635635
elif (inspect.isclass(class_or_instance)
636636
and issubclass(class_or_instance, ModelSimple)):
637637
return COERCION_INDEX_BY_TYPE[ModelSimple]
638-
return COERCION_INDEX_BY_TYPE[class_or_instance]
638+
elif class_or_instance in COERCION_INDEX_BY_TYPE:
639+
return COERCION_INDEX_BY_TYPE[class_or_instance]
640+
raise ApiValueError("Unsupported type: %s" % class_or_instance)
639641

640642
sorted_types = sorted(
641643
required_types,
@@ -653,7 +655,7 @@ def remove_uncoercible(required_types_classes, current_item, from_server,
653655
these should be ordered by COERCION_INDEX_BY_TYPE
654656
from_server (bool): a boolean of whether the data is from the server
655657
if false, the data is from the client
656-
current_item (any): the current item to be converted
658+
current_item (any): the current item (input data) to be converted
657659
658660
Keyword Args:
659661
must_convert (bool): if True the item to convert is of the wrong

samples/openapi3/client/petstore/python-experimental/petstore_api/model_utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def __eq__(self, other):
336336
ModelComposed: 0,
337337
ModelNormal: 1,
338338
ModelSimple: 2,
339-
none_type: 3,
339+
none_type: 3, # The type of 'None'.
340340
list: 4,
341341
dict: 5,
342342
float: 6,
@@ -345,7 +345,7 @@ def __eq__(self, other):
345345
datetime: 9,
346346
date: 10,
347347
str: 11,
348-
file_type: 12,
348+
file_type: 12, # 'file_type' is an alias for the built-in 'file' or 'io.IOBase' type.
349349
}
350350

351351
# these are used to limit what type conversions we try to do
@@ -614,11 +614,11 @@ def order_response_types(required_types):
614614
615615
Args:
616616
required_types (list/tuple): collection of classes or instance of
617-
list or dict with classs information inside it
617+
list or dict with class information inside it.
618618
619619
Returns:
620620
(list): coercion order sorted collection of classes or instance
621-
of list or dict with classs information inside it
621+
of list or dict with class information inside it.
622622
"""
623623

624624
def index_getter(class_or_instance):
@@ -635,7 +635,9 @@ def index_getter(class_or_instance):
635635
elif (inspect.isclass(class_or_instance)
636636
and issubclass(class_or_instance, ModelSimple)):
637637
return COERCION_INDEX_BY_TYPE[ModelSimple]
638-
return COERCION_INDEX_BY_TYPE[class_or_instance]
638+
elif class_or_instance in COERCION_INDEX_BY_TYPE:
639+
return COERCION_INDEX_BY_TYPE[class_or_instance]
640+
raise ApiValueError("Unsupported type: %s" % class_or_instance)
639641

640642
sorted_types = sorted(
641643
required_types,
@@ -653,7 +655,7 @@ def remove_uncoercible(required_types_classes, current_item, from_server,
653655
these should be ordered by COERCION_INDEX_BY_TYPE
654656
from_server (bool): a boolean of whether the data is from the server
655657
if false, the data is from the client
656-
current_item (any): the current item to be converted
658+
current_item (any): the current item (input data) to be converted
657659
658660
Keyword Args:
659661
must_convert (bool): if True the item to convert is of the wrong

0 commit comments

Comments
 (0)