Skip to content

Commit

Permalink
Fix validation for polymorphic models
Browse files Browse the repository at this point in the history
  • Loading branch information
jlumpe committed Dec 15, 2016
1 parent 516d61a commit 09458d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bravado_core/marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def marshal_model(swagger_spec, model_spec, model_value):
if model_value is None:
return handle_null_value(swagger_spec, model_spec)

if not isinstance(model_value, model_type):
if not model_type._isinstance(model_value):
raise SwaggerMappingError(
'Expected model of type {0} for {1}:{2}'
.format(model_name, type(model_value), model_value))
Expand Down
25 changes: 25 additions & 0 deletions bravado_core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ def _unmarshal(cls, val):
from bravado_core.unmarshal import unmarshal_model
return unmarshal_model(cls._swagger_spec, cls._model_spec, val)

@classmethod
def _isinstance(cls, obj):
"""Check if an object is an instance of this model or a model inheriting
from it.
:param obj: Object to check.
:rtype: bool
"""
if isinstance(obj, cls):
return True

if isinstance(obj, Model):
return cls.__name__ in type(obj)._inherits_from

return False


class ModelDocstring(object):
"""Descriptor for model classes that dynamically generates docstrings.
Expand Down Expand Up @@ -344,11 +360,20 @@ def create_model_type(swagger_spec, model_name, model_spec, bases=(Model,)):
:returns: dynamic type inheriting from ``bases``.
:rtype: type
"""

inherits_from = []
if 'allOf' in model_spec:
for schema in model_spec['allOf']:
inherited_name = swagger_spec.deref(schema).get(MODEL_MARKER, None)
if inherited_name:
inherits_from.append(inherited_name)

return type(str(model_name), bases, dict(
__doc__=ModelDocstring(),
_swagger_spec=swagger_spec,
_model_spec=model_spec,
_properties=collapsed_properties(model_spec, swagger_spec),
_inherits_from=inherits_from,
))


Expand Down

0 comments on commit 09458d3

Please sign in to comment.