Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:type swagger_spec: :class:`bravado.mapping.spec.Spec`
:type model_spec: dict or jsonref.JsonRef
:type model_value: dict
:rtype: Model instance
:raises: TypeError
"""
model_name = model_spec[MODEL_MARKER]
model_type = swagger_spec.definitions.get(model_name, None)
if model_type is None:
raise TypeError(
'Unknown model {0} when trying to unmarshal {1}'
.format(model_name, model_value))
if not is_dict_like(model_value):
raise TypeError(
"Expected type to be dict for value {0} to unmarshal to a {1}."
"Was {1} instead."
.format(model_value, model_type, type(model_value)))
model_as_dict = unmarshal_object(swagger_spec, model_spec, model_value)
model_instance = model_type(**model_as_dict)
return model_instance
:type value: int, long, string, unicode, boolean, list, dict, Model type
:return: marshaled value
:rtype: int, long, string, unicode, boolean, list, dict
"""
obj_type = schema_object_spec['type']
if obj_type in SWAGGER_PRIMITIVES:
return marshal_primitive(schema_object_spec, value)
if obj_type == 'array':
return marshal_array(swagger_spec, schema_object_spec, value)
if is_model(schema_object_spec):
# Allow models to be passed in as dicts for flexibility.
if is_dict_like(value):
return marshal_object(swagger_spec, schema_object_spec, value)
# It is important that the 'model' check comes before 'object' check
# below. Model specs are of type 'object' but also have a MODEL_MARKER
# key for identification.
return marshal_model(swagger_spec, schema_object_spec, value)
if obj_type == 'object':
return marshal_object(swagger_spec, schema_object_spec, value)
if obj_type == 'file':
return value
raise SwaggerMappingError('Unknown type {0} for value {1}'.format(
obj_type, value))
def descend(fragment):
if is_dict_like(fragment):
for k, v in fragment.iteritems():
if k == '$ref' and v in model_names:
fragment[k] = "#/definitions/{0}".format(v)
descend(v)
elif is_list_like(fragment):
for element in fragment:
descend(element)
def marshal_object(swagger_spec, object_spec, object_value):
"""Marshal a jsonschema type of 'object' into a json-like dict.
:type swagger_spec: :class:`bravado.mapping.spec.Spec`
:type object_spec: dict or jsonref.JsonRef
:type object_value: dict
:rtype: dict
:raises: SwaggerMappingError
"""
if not is_dict_like(object_value):
raise TypeError('Expected dict like type for {0}:{1}'.format(
type(object_value), object_value))
result = {}
for k, v in object_value.iteritems():
# Values cannot be None - skip them entirely!
if v is None:
continue
prop_spec = get_spec_for_prop(object_spec, object_value, k)
if prop_spec:
result[k] = marshal_schema_object(swagger_spec, prop_spec, v)
else:
# Don't marshal when a spec is not available - just pass through
result[k] = v
def unmarshal_object(swagger_spec, object_spec, object_value):
"""Unmarshal a jsonschema type of 'object' into a python dict.
:type swagger_spec: :class:`bravado.mapping.spec.Spec`
:type object_spec: dict or jsonref.JsonRef
:type object_value: dict
:rtype: dict
:raises: TypeError
"""
if not is_dict_like(object_value):
raise TypeError('Expected dict like type for {0}:{1}'.format(
type(object_value), object_value))
result = {}
for k, v in object_value.iteritems():
prop_spec = get_spec_for_prop(object_spec, object_value, k)
if prop_spec:
result[k] = unmarshal_schema_object(swagger_spec, prop_spec, v)
else:
# Don't marshal when a spec is not available - just pass through
result[k] = v
# re-introduce and None'ify any properties that weren't passed
for prop_name, prop_spec in object_spec.get('properties', {}).iteritems():
if prop_name not in result:
result[prop_name] = None