def test_x_model_not_string(minimal_swagger_dict): minimal_swagger_dict['definitions']['Foo'] = { 'x-model': { 'x-vendor': 'Address' }, } swagger_spec = Spec(minimal_swagger_dict) assert not is_model(swagger_spec, minimal_swagger_dict['definitions']['Foo'])
def unmarshal_schema_object(swagger_spec, schema_object_spec, value): """Unmarshal the value using the given schema object specification. Unmarshaling includes: - transform the value according to 'format' if available - return the value in a form suitable for use. e.g. conversion to a Model type. :type swagger_spec: :class:`bravado_core.spec.Spec` :type schema_object_spec: dict :type value: int, float, long, string, unicode, boolean, list, dict, etc :return: unmarshaled value :rtype: int, float, long, string, unicode, boolean, list, dict, object (in the case of a 'format' conversion', or Model type """ deref = swagger_spec.deref schema_object_spec = deref(schema_object_spec) obj_type = schema_object_spec.get('type') if 'allOf' in schema_object_spec: obj_type = 'object' if not obj_type: if swagger_spec.config['default_type_to_object']: obj_type = 'object' else: return value if obj_type in SWAGGER_PRIMITIVES: return unmarshal_primitive(swagger_spec, schema_object_spec, value) if obj_type == 'array': return unmarshal_array(swagger_spec, schema_object_spec, value) if swagger_spec.config['use_models'] and \ is_model(swagger_spec, schema_object_spec): # It is important that the 'model' check comes before 'object' check. # Model specs also have type 'object' but also have the additional # MODEL_MARKER key for identification. return unmarshal_model(swagger_spec, schema_object_spec, value) if obj_type == 'object': return unmarshal_object(swagger_spec, schema_object_spec, value) if obj_type == 'file': return value raise SwaggerMappingError( "Don't know how to unmarshal value {0} with a type of {1}".format( value, obj_type))
def unmarshal_schema_object(swagger_spec, schema_object_spec, value): """Unmarshal the value using the given schema object specification. Unmarshaling includes: - transform the value according to 'format' if available - return the value in a form suitable for use. e.g. conversion to a Model type. :type swagger_spec: :class:`bravado_core.spec.Spec` :type schema_object_spec: dict :type value: int, float, long, string, unicode, boolean, list, dict, etc :return: unmarshaled value :rtype: int, float, long, string, unicode, boolean, list, dict, object (in the case of a 'format' conversion', or Model type """ deref = swagger_spec.deref schema_object_spec = deref(schema_object_spec) obj_type = schema_object_spec.get('type') if 'allOf' in schema_object_spec: obj_type = 'object' if not obj_type: if swagger_spec.config['default_type_to_object']: obj_type = 'object' else: return value if obj_type in SWAGGER_PRIMITIVES: return unmarshal_primitive(swagger_spec, schema_object_spec, value) if obj_type == 'array': return unmarshal_array(swagger_spec, schema_object_spec, value) if swagger_spec.config['use_models'] and \ is_model(swagger_spec, schema_object_spec): # It is important that the 'model' check comes before 'object' check. # Model specs also have type 'object' but also have the additional # MODEL_MARKER key for identification. return unmarshal_model(swagger_spec, schema_object_spec, value) if obj_type == 'object': return unmarshal_object(swagger_spec, schema_object_spec, value) if obj_type == 'file': return value raise SwaggerMappingError( "Don't know how to unmarshal value {0} with a type of {1}" .format(value, obj_type))
def marshal_schema_object(swagger_spec, schema_object_spec, value): """Marshal the value using the given schema object specification. Marshaling includes: - transform the value according to 'format' if available - return the value in a form suitable for 'on-the-wire' transmission :type swagger_spec: :class:`bravado_core.spec.Spec` :type schema_object_spec: dict :type value: int, long, string, unicode, boolean, list, dict, Model type :return: marshaled value :rtype: int, long, string, unicode, boolean, list, dict :raises: SwaggerMappingError """ deref = swagger_spec.deref schema_object_spec = deref(schema_object_spec) obj_type = schema_object_spec['type'] if 'int' in str(type(value)) and obj_type == 'string': schema_object_spec['type'] = 'integer' if obj_type in SWAGGER_PRIMITIVES: return marshal_primitive(swagger_spec, schema_object_spec, value) if obj_type == 'array': return marshal_array(swagger_spec, schema_object_spec, value) if is_model(swagger_spec, 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 marshal_schema_object(swagger_spec, schema_object_spec, value): """Marshal the value using the given schema object specification. Marshaling includes: - transform the value according to 'format' if available - return the value in a form suitable for 'on-the-wire' transmission :type swagger_spec: :class:`bravado_core.spec.Spec` :type schema_object_spec: dict :type value: int, long, string, unicode, boolean, list, dict, Model type :return: marshaled value :rtype: int, long, string, unicode, boolean, list, dict :raises: SwaggerMappingError """ deref = swagger_spec.deref schema_object_spec = deref(schema_object_spec) obj_type = schema_object_spec.get('type') if obj_type in SWAGGER_PRIMITIVES: return marshal_primitive(swagger_spec, schema_object_spec, value) if obj_type == 'array': return marshal_array(swagger_spec, schema_object_spec, value) if is_model(swagger_spec, 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 is_object(swagger_spec, schema_object_spec): 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 test_ref(minimal_swagger_dict, model_spec): minimal_swagger_dict['definitions']['Foo'] = model_spec swagger_spec = Spec(minimal_swagger_dict) assert is_model(swagger_spec, {'$ref': '#/definitions/Foo'})
def test_true(minimal_swagger_spec, model_spec): assert is_model(minimal_swagger_spec, model_spec)
def test_false(minimal_swagger_spec, object_spec): assert not is_model(minimal_swagger_spec, object_spec)