diff --git a/bravado_core/marshal.py b/bravado_core/marshal.py index eaf7d6df..ac9aad19 100644 --- a/bravado_core/marshal.py +++ b/bravado_core/marshal.py @@ -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)) diff --git a/bravado_core/model.py b/bravado_core/model.py index cc6c018b..691da4f8 100644 --- a/bravado_core/model.py +++ b/bravado_core/model.py @@ -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. @@ -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, ))