Skip to content

Commit

Permalink
Simplify _marshal_model
Browse files Browse the repository at this point in the history
  • Loading branch information
macisamuele committed May 28, 2019
1 parent 3e8d0d2 commit 2d0b416
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions bravado_core/marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import typing
from six import iteritems
from six import iterkeys

from bravado_core import _decorators
from bravado_core import schema
Expand Down Expand Up @@ -252,7 +251,7 @@ def _marshaling_method_file(swagger_spec, object_schema):
return _no_op_marshaling


def _marshal_model(
def _marshal_object(
swagger_spec, # type: Spec
model_type, # type: typing.Union[typing.Type[JSONDict], typing.Type[Model]]
properties_to_marshaling_function, # type: typing.Dict[typing.Text, MarshalingMethod]
Expand Down Expand Up @@ -294,35 +293,21 @@ def _marshal_model(
return marshaling_function(model_value)

marshaled_value = dict()
properties_to_marshal = set(iterkeys(properties_to_marshaling_function))
if additional_properties_marshaling_function is not None:
if isinstance(model_value, Model):
properties_to_marshal.update(model_value._additional_props)
else:
properties_to_marshal.update(iterkeys(model_value))

for property_name in properties_to_marshal:
for property_name in model_value:
property_value = model_value[property_name]
property_marshaling_function = properties_to_marshaling_function.get(
property_name, additional_properties_marshaling_function,
)
try:
property_value = model_value[property_name]
except KeyError:
property_value = _NOT_FOUND

if property_value not in (_NOT_FOUND, None):
marshaled_value[property_name] = property_marshaling_function(property_value)
elif (
property_name in required_properties or
(
property_value is None and property_name in nullable_properties
)
if (
property_value is None
and property_name not in required_properties
and property_name not in nullable_properties
and property_name in properties_to_marshaling_function
):
marshaled_value[property_name] = None
elif property_name in properties_to_marshaling_function:
pass
elif additional_properties_marshaling_function:
marshaled_value[property_name] = additional_properties_marshaling_function(property_value)
continue

marshaled_value[property_name] = property_marshaling_function(property_value)

return marshaled_value


Expand Down Expand Up @@ -388,7 +373,7 @@ def _marshaling_method_object(swagger_spec, object_schema):
}

return partial(
_marshal_model,
_marshal_object,
swagger_spec,
model_type if model_type and swagger_spec.config['use_models'] else dict,
properties_to_marshaling_function,
Expand Down

0 comments on commit 2d0b416

Please sign in to comment.