Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{{>partial_header}}


class OpenApiException(Exception):
"""The base exception class for all OpenAPIExceptions"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def convert_js_args_to_python_args(fn):
"""
spec_property_naming = kwargs.get('_spec_property_naming', False)
if spec_property_naming:
kwargs = change_keys_js_to_python(kwargs, _self if isinstance(_self, type) else _self.__class__)
kwargs = change_keys_js_to_python(
kwargs, _self if isinstance(
_self, type) else _self.__class__)
return fn(_self, *args, **kwargs)
return wrapped_init

Expand All @@ -59,6 +61,7 @@ class cached_property(object):

PRIMITIVE_TYPES = (list, float, int, bool, datetime, date, str, file_type)


def allows_single_value_input(cls):
"""
This function returns True if the input composed schema model or any
Expand All @@ -83,6 +86,7 @@ def allows_single_value_input(cls):
return any(allows_single_value_input(c) for c in cls._composed_schemas['oneOf'])
return False


def composed_model_input_classes(cls):
"""
This function returns a list of the possible models that can be accepted as
Expand Down Expand Up @@ -132,7 +136,6 @@ class OpenApiModel(object):
oneof_instance = get_oneof_instance(cls, model_kwargs, kwargs, model_arg=arg)
return oneof_instance


visited_composed_classes = kwargs.get('_visited_composed_classes', ())
if (
cls.discriminator is None or
Expand Down Expand Up @@ -179,7 +182,7 @@ class OpenApiModel(object):
# call itself and update the list of visited classes, and the initial
# value must be an empty list. Hence not using 'visited_composed_classes'
new_cls = get_discriminator_class(
cls, discr_propertyname_py, discr_value, [])
cls, discr_propertyname_py, discr_value, [])
if new_cls is None:
path_to_item = kwargs.get('_path_to_item', ())
disc_prop_value = kwargs.get(
Expand Down Expand Up @@ -234,7 +237,6 @@ class OpenApiModel(object):

return new_inst


@classmethod
@convert_js_args_to_python_args
def _new_from_openapi_data(cls, *args, **kwargs):
Expand All @@ -253,7 +255,6 @@ class OpenApiModel(object):
oneof_instance = get_oneof_instance(cls, model_kwargs, kwargs, model_arg=arg)
return oneof_instance


visited_composed_classes = kwargs.get('_visited_composed_classes', ())
if (
cls.discriminator is None or
Expand Down Expand Up @@ -300,7 +301,7 @@ class OpenApiModel(object):
# call itself and update the list of visited classes, and the initial
# value must be an empty list. Hence not using 'visited_composed_classes'
new_cls = get_discriminator_class(
cls, discr_propertyname_py, discr_value, [])
cls, discr_propertyname_py, discr_value, [])
if new_cls is None:
path_to_item = kwargs.get('_path_to_item', ())
disc_prop_value = kwargs.get(
Expand Down Expand Up @@ -345,7 +346,6 @@ class OpenApiModel(object):
# new_cls it will not include the allOf validations in self
self_inst = cls._from_openapi_data(*args, **kwargs)


new_inst = new_cls._new_from_openapi_data(*args, **kwargs)
return new_inst

Expand Down Expand Up @@ -421,7 +421,8 @@ COERCION_INDEX_BY_TYPE = {
UPCONVERSION_TYPE_PAIRS = (
(str, datetime),
(str, date),
(int, float), # A float may be serialized as an integer, e.g. '3' is a valid serialized float.
# A float may be serialized as an integer, e.g. '3' is a valid serialized float.
(int, float),
(list, ModelComposed),
(dict, ModelComposed),
(str, ModelComposed),
Expand Down Expand Up @@ -574,8 +575,8 @@ def is_json_validation_enabled(schema_keyword, configuration=None):
"""

return (configuration is None or
not hasattr(configuration, '_disabled_client_side_validations') or
schema_keyword not in configuration._disabled_client_side_validations)
not hasattr(configuration, '_disabled_client_side_validations') or
schema_keyword not in configuration._disabled_client_side_validations)


def check_validations(
Expand Down Expand Up @@ -713,9 +714,9 @@ def check_validations(
not re.search(current_validations['regex']['pattern'],
input_values, flags=flags)):
err_msg = r"Invalid value for `%s`, must match regular expression `%s`" % (
input_variable_path[0],
current_validations['regex']['pattern']
)
input_variable_path[0],
current_validations['regex']['pattern']
)
if flags != 0:
# Don't print the regex flags if the flags are not
# specified in the OAS document.
Expand Down Expand Up @@ -806,6 +807,7 @@ def remove_uncoercible(required_types_classes, current_item, spec_property_namin
results_classes.append(required_type_class)
return results_classes


def get_discriminated_classes(cls):
"""
Returns all the classes that a discriminator converts to
Expand Down Expand Up @@ -1004,15 +1006,15 @@ def get_discriminator_class(model_class,
# Descendant example: mammal -> whale/zebra/Pig -> BasquePig/DanishPig
# if we try to make BasquePig from mammal, we need to travel through
# the oneOf descendant discriminators to find BasquePig
descendant_classes = model_class._composed_schemas.get('oneOf', ()) + \
descendant_classes = model_class._composed_schemas.get('oneOf', ()) + \
model_class._composed_schemas.get('anyOf', ())
ancestor_classes = model_class._composed_schemas.get('allOf', ())
possible_classes = descendant_classes + ancestor_classes
for cls in possible_classes:
# Check if the schema has inherited discriminators.
if hasattr(cls, 'discriminator') and cls.discriminator is not None:
used_model_class = get_discriminator_class(
cls, discr_name, discr_value, cls_visited)
cls, discr_name, discr_value, cls_visited)
if used_model_class is not None:
return used_model_class
return used_model_class
Expand Down Expand Up @@ -1179,9 +1181,11 @@ def is_type_nullable(input_type):
if issubclass(input_type, ModelComposed):
# If oneOf/anyOf, check if the 'null' type is one of the allowed types.
for t in input_type._composed_schemas.get('oneOf', ()):
if is_type_nullable(t): return True
if is_type_nullable(t):
return True
for t in input_type._composed_schemas.get('anyOf', ()):
if is_type_nullable(t): return True
if is_type_nullable(t):
return True
return False


Expand All @@ -1196,7 +1200,7 @@ def is_valid_type(input_class_simple, valid_classes):
bool
"""
if issubclass(input_class_simple, OpenApiModel) and \
valid_classes == (bool, date, datetime, dict, float, int, list, str, none_type,):
valid_classes == (bool, date, datetime, dict, float, int, list, str, none_type,):
return True
valid_type = input_class_simple in valid_classes
if not valid_type and (
Expand Down Expand Up @@ -1254,9 +1258,9 @@ def validate_and_convert_types(input_value, required_types_mixed, path_to_item,
input_class_simple = get_simple_class(input_value)
valid_type = is_valid_type(input_class_simple, valid_classes)
if not valid_type:
if (configuration
or (input_class_simple == dict
and not dict in valid_classes)):
if (configuration
or (input_class_simple == dict
and dict not in valid_classes)):
# if input_value is not valid_type try to convert it
converted_instance = attempt_convert_item(
input_value,
Expand Down Expand Up @@ -1349,7 +1353,11 @@ def model_to_dict(model_instance, serialize=True):
attribute_map
"""
result = {}
extract_item = lambda item: (item[0], model_to_dict(item[1], serialize=serialize)) if hasattr(item[1], '_data_store') else item

def extract_item(item): return (
item[0], model_to_dict(
item[1], serialize=serialize)) if hasattr(
item[1], '_data_store') else item

model_instances = [model_instance]
if model_instance._composed_schemas:
Expand All @@ -1369,24 +1377,24 @@ def model_to_dict(model_instance, serialize=True):
except KeyError:
used_fallback_python_attribute_names.add(attr)
if isinstance(value, list):
if not value:
# empty list or None
result[attr] = value
else:
res = []
for v in value:
if isinstance(v, PRIMITIVE_TYPES) or v is None:
res.append(v)
elif isinstance(v, ModelSimple):
res.append(v.value)
elif isinstance(v, dict):
res.append(dict(map(
extract_item,
v.items()
)))
else:
res.append(model_to_dict(v, serialize=serialize))
result[attr] = res
if not value:
# empty list or None
result[attr] = value
else:
res = []
for v in value:
if isinstance(v, PRIMITIVE_TYPES) or v is None:
res.append(v)
elif isinstance(v, ModelSimple):
res.append(v.value)
elif isinstance(v, dict):
res.append(dict(map(
extract_item,
v.items()
)))
else:
res.append(model_to_dict(v, serialize=serialize))
result[attr] = res
elif isinstance(value, dict):
result[attr] = dict(map(
extract_item,
Expand Down Expand Up @@ -1538,13 +1546,15 @@ def get_oneof_instance(cls, model_kwargs, constant_kwargs, model_arg=None):
try:
if not single_value_input:
if constant_kwargs.get('_spec_property_naming'):
oneof_instance = oneof_class._from_openapi_data(**model_kwargs, **constant_kwargs)
oneof_instance = oneof_class._from_openapi_data(
**model_kwargs, **constant_kwargs)
else:
oneof_instance = oneof_class(**model_kwargs, **constant_kwargs)
else:
if issubclass(oneof_class, ModelSimple):
if constant_kwargs.get('_spec_property_naming'):
oneof_instance = oneof_class._from_openapi_data(model_arg, **constant_kwargs)
oneof_instance = oneof_class._from_openapi_data(
model_arg, **constant_kwargs)
else:
oneof_instance = oneof_class(model_arg, **constant_kwargs)
elif oneof_class in PRIMITIVE_TYPES:
Expand Down Expand Up @@ -1722,8 +1732,8 @@ def validate_get_composed_info(constant_args, model_args, self):
var_name_to_model_instances[prop_name] = [self] + composed_instances

return [
composed_instances,
var_name_to_model_instances,
additional_properties_model_instances,
discarded_args
composed_instances,
var_name_to_model_instances,
additional_properties_model_instances,
discarded_args
]
1 change: 0 additions & 1 deletion samples/client/petstore/python/petstore_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""



class OpenApiException(Exception):
"""The base exception class for all OpenAPIExceptions"""

Expand Down
Loading