def test_composition(minimal_swagger_dict, address_spec, address,
                     business_address_spec, business_address):
    minimal_swagger_dict['definitions']['Address'] = address_spec
    minimal_swagger_dict['definitions'][
        'BusinessAddress'] = business_address_spec
    swagger_spec = Spec.from_dict(minimal_swagger_dict)

    expected_spec_1 = address_spec['properties']['street_name']
    result_1 = get_spec_for_prop(
        swagger_spec,
        address_spec,
        address,
        'street_name',
    )
    assert expected_spec_1 == result_1

    expected_spec_2 = business_address_spec['allOf'][1]['properties'][
        'company']
    result_2 = get_spec_for_prop(
        swagger_spec,
        business_address_spec,
        business_address,
        'company',
    )
    assert expected_spec_2 == result_2
def test_composition(minimal_swagger_dict, address_spec, address,
                     business_address_spec, business_address):
    minimal_swagger_dict['definitions']['Address'] = address_spec
    minimal_swagger_dict['definitions']['BusinessAddress'] = business_address_spec
    swagger_spec = Spec.from_dict(minimal_swagger_dict)

    expected_spec_1 = address_spec['properties']['street_name']
    result_1 = get_spec_for_prop(
        swagger_spec, address_spec, address, 'street_name')
    assert expected_spec_1 == result_1

    expected_spec_2 = business_address_spec['allOf'][1]['properties']['company']
    result_2 = get_spec_for_prop(
        swagger_spec, business_address_spec, business_address, 'company')
    assert expected_spec_2 == result_2
Exemplo n.º 3
0
def unmarshal_object(swagger_spec, object_spec, object_value):
    """Unmarshal a jsonschema type of 'object' into a python dict.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type object_spec: dict
    :type object_value: dict
    :rtype: dict
    :raises: SwaggerMappingError
    """
    deref = swagger_spec.deref

    if not is_dict_like(object_value):
      # This is the workaround for Alert Profile API tests, measureParam is
      # defined as 'object', which will raise exception here.
        return
#        raise SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
#            type(object_value), object_value))

    result = {}
    for k, v in iteritems(object_value):
        prop_spec = get_spec_for_prop(
            swagger_spec, 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
    properties = deref(object_spec).get('properties', {})
    for prop_name, prop_spec in iteritems(properties):
        if prop_name not in result:
            result[prop_name] = None
    return result
Exemplo n.º 4
0
def marshal_object(swagger_spec, object_spec, object_value):
    """Marshal a python dict to json dict.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type object_spec: dict
    :type object_value: dict

    :rtype: dict
    :raises: SwaggerMappingError
    """
    deref = swagger_spec.deref

    if not is_dict_like(object_value):
        raise SwaggerMappingError("Expected dict like type for {0}:{1}".format(type(object_value), object_value))

    result = {}
    for k, v in iteritems(object_value):

        # Values cannot be None - skip them entirely!
        if v is None:
            continue

        prop_spec = get_spec_for_prop(swagger_spec, deref(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

    return result
def test_get_spec_for_prop_with_x_nullable_and_reference(minimal_swagger_dict):
    # TODO: remove is_nullable support once https://github.com/Yelp/bravado-core/issues/335 is addressed
    minimal_swagger_dict['definitions'] = {
        'referenced': {
            'type': 'string',
        },
        'model': {
            'type': 'object',
            'properties': {
                'property': {
                    'x-nullable': True,
                    '$ref': '#/definitions/referenced',
                },
            },
        },
    }
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    assert {
        'x-nullable': True,
        'type': 'string'
    } == get_spec_for_prop(
        swagger_spec,
        minimal_swagger_dict['definitions']['model'],
        None,
        'property',
    )
Exemplo n.º 6
0
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_core.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 SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
            type(object_value), object_value))

    result = {}
    for k, v in iteritems(object_value):

        # 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

    return result
Exemplo n.º 7
0
def test_object_is_ref(minimal_swagger_dict, address_spec, address):
    minimal_swagger_dict['definitions']['Address'] = address_spec
    address_ref_spec = {'$ref': '#/definitions/Address'}
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    result = get_spec_for_prop(
        swagger_spec, address_ref_spec, address, 'street_type')
    assert address_spec['properties']['street_type'] == result
Exemplo n.º 8
0
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_core.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

    return result
Exemplo n.º 9
0
def test_additionalProperties_with_spec(address_spec, address):
    address_spec['additionalProperties'] = {'type': 'string'}
    expected_spec = {'type': 'string'}
    # 'city' is not a declared property so it gets classified under
    # additionalProperties
    result = get_spec_for_prop(address_spec, address, 'city')
    assert expected_spec == result
Exemplo n.º 10
0
def unmarshal_object(swagger_spec, object_spec, object_value):
    """Unmarshal a jsonschema type of 'object' into a python dict.

    :type swagger_spec: :class:`bravado_core.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 SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
            type(object_value), object_value))

    result = {}
    for k, v in iteritems(object_value):
        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 iteritems(object_spec.get('properties', {})):
        if prop_name not in result:
            result[prop_name] = None
    return result
Exemplo n.º 11
0
def unmarshal_object(swagger_spec, object_spec, object_value):
    """Unmarshal a jsonschema type of 'object' into a python dict.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type object_spec: dict
    :type object_value: dict
    :rtype: dict
    :raises: SwaggerMappingError
    """
    deref = swagger_spec.deref

    if not is_dict_like(object_value):
        raise SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
            type(object_value), object_value))

    result = {}
    for k, v in iteritems(object_value):
        prop_spec = get_spec_for_prop(swagger_spec, 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
    properties = deref(object_spec).get('properties', {})
    for prop_name, prop_spec in iteritems(properties):
        if prop_name not in result:
            result[prop_name] = None
    return result
Exemplo n.º 12
0
def test_additionalProperties_with_spec(minimal_swagger_spec, address_spec,
                                        address):
    address_spec['additionalProperties'] = {'type': 'string'}
    expected_spec = {'type': 'string'}
    # 'city' is not a declared property so it gets classified under
    # additionalProperties
    result = get_spec_for_prop(
        minimal_swagger_spec, address_spec, address, 'city')
    assert expected_spec == result
def test_declared_property(minimal_swagger_spec, address_spec, address):
    expected_spec = address_spec['properties']['street_name']
    result = get_spec_for_prop(
        minimal_swagger_spec,
        address_spec,
        address,
        'street_name',
    )
    assert expected_spec == result
Exemplo n.º 14
0
def test_properties_not_present_and_additionalProperties_False(
        minimal_swagger_spec, address):
    object_spec = {
        'type': 'object',
        'additionalProperties': False
    }
    result = get_spec_for_prop(
        minimal_swagger_spec, object_spec, address, 'street_name')
    assert result is None
Exemplo n.º 15
0
def unmarshal_object(swagger_spec, object_spec, object_value):
    """Unmarshal a jsonschema type of 'object' into a python dict.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type object_spec: dict
    :type object_value: dict
    :rtype: dict
    :raises: SwaggerMappingError
    """
    deref = swagger_spec.deref

    if object_value is None:
        return handle_null_value(swagger_spec, object_spec)

    if not is_dict_like(object_value):
        raise SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
            type(object_value), object_value))

    object_spec = deref(object_spec)
    required_fields = object_spec.get('required', [])

    result = {}
    for k, v in iteritems(object_value):
        prop_spec = get_spec_for_prop(swagger_spec, object_spec, object_value,
                                      k)
        if v is None and k not in required_fields and prop_spec:
            if schema.has_default(swagger_spec, prop_spec):
                result[k] = schema.get_default(swagger_spec, prop_spec)
            else:
                result[k] = None
        elif prop_spec:
            # Zohar: Ugly hack to fix handling of unicode type, which were recognized as objects
            if type(v) == type(u''):
                result[k] = unmarshal_primitive(swagger_spec, prop_spec, v)
            # elif type(v) == type(list()):
            #     result[k] = unmarshal_array(swagger_spec, prop_spec, v)
            else:
                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

    properties = collapsed_properties(deref(object_spec), swagger_spec)
    for prop_name, prop_spec in iteritems(properties):
        if prop_name not in result and swagger_spec.config[
                'include_missing_properties']:
            result[prop_name] = None
            if schema.has_default(swagger_spec, prop_spec):
                result[prop_name] = schema.get_default(swagger_spec, prop_spec)

    return result
Exemplo n.º 16
0
def unmarshal_object(swagger_spec, object_spec, object_value):
    """Unmarshal a jsonschema type of 'object' into a python dict.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type object_spec: dict
    :type object_value: dict
    :rtype: dict
    :raises: SwaggerMappingError
    """
    deref = swagger_spec.deref

    if object_value is None:
        return handle_null_value(swagger_spec, object_spec)

    if not is_dict_like(object_value):
        raise SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
            type(object_value), object_value))

    object_spec = deref(object_spec)
    required_fields = object_spec.get('required', [])
    properties = collapsed_properties(object_spec, swagger_spec)

    result = {}
    for k, v in iteritems(object_value):
        prop_spec = get_spec_for_prop(
            swagger_spec, object_spec, object_value, k, properties)
        if v is None and k not in required_fields and prop_spec:
            if schema.has_default(swagger_spec, prop_spec):
                result[k] = schema.get_default(swagger_spec, prop_spec)
            else:
                result[k] = None
        elif 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

    for prop_name, prop_spec in iteritems(properties):
        if prop_name not in result and swagger_spec.config['include_missing_properties']:
            result[prop_name] = None
            if schema.has_default(swagger_spec, prop_spec):
                result[prop_name] = schema.get_default(swagger_spec, prop_spec)

    return result
Exemplo n.º 17
0
def test_property_is_ref(minimal_swagger_dict, address):
    street_type_spec = {
        'type': 'string',
        'enum': ['Street', 'Avenue', 'Boulevard']
    }

    address_spec = {
        'type': 'object',
        'properties': {
            'street_type': {
                '$ref': '#/definitions/StreetType'
            }
        }
    }

    minimal_swagger_dict['definitions']['StreetType'] = street_type_spec
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    result = get_spec_for_prop(
        swagger_spec, address_spec, address, 'street_type')
    assert street_type_spec == result
Exemplo n.º 18
0
def marshal_object(swagger_spec, object_spec, object_value):
    """Marshal a python dict to json dict.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type object_spec: dict
    :type object_value: dict

    :rtype: dict
    :raises: SwaggerMappingError
    """
    deref = swagger_spec.deref

    if object_value is None:
        return handle_null_value(swagger_spec, object_spec)

    if not is_dict_like(object_value):
        raise SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
            type(object_value), object_value))

    object_spec = deref(object_spec)
    required_fields = object_spec.get('required', [])
    properties = collapsed_properties(object_spec, swagger_spec)

    result = {}
    for k, v in iteritems(object_value):

        prop_spec = get_spec_for_prop(
            swagger_spec, object_spec, object_value, k, properties)

        if not prop_spec:
            # Don't marshal when a spec is not available - just pass through
            result[k] = v
            continue

        if v is None and k not in required_fields:
            if not is_prop_nullable(swagger_spec, prop_spec):
                continue

        result[k] = marshal_schema_object(swagger_spec, prop_spec, v)

    return result
Exemplo n.º 19
0
def marshal_object(swagger_spec, object_spec, object_value):
    """Marshal a python dict to json dict.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type object_spec: dict
    :type object_value: dict

    :rtype: dict
    :raises: SwaggerMappingError
    """
    deref = swagger_spec.deref

    if object_value is None:
        return handle_null_value(swagger_spec, object_spec)

    if not is_dict_like(object_value):
        raise SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
            type(object_value), object_value))

    object_spec = deref(object_spec)
    required_fields = object_spec.get('required', [])
    properties = collapsed_properties(object_spec, swagger_spec)

    result = {}
    for k, v in iteritems(object_value):

        prop_spec = get_spec_for_prop(
            swagger_spec, object_spec, object_value, k, properties)

        if not prop_spec:
            # Don't marshal when a spec is not available - just pass through
            result[k] = v
            continue

        if v is None and k not in required_fields:
            if not is_prop_nullable(swagger_spec, prop_spec):
                continue

        result[k] = marshal_schema_object(swagger_spec, prop_spec, v)

    return result
Exemplo n.º 20
0
def unmarshal_object(swagger_spec, object_spec, object_value):
    """Unmarshal a jsonschema type of 'object' into a python dict.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type object_spec: dict
    :type object_value: dict
    :rtype: dict
    :raises: SwaggerMappingError
    """
    deref = swagger_spec.deref

    if object_value is None:
        return handle_null_value(swagger_spec, object_spec)

    if not is_dict_like(object_value):
        raise SwaggerMappingError('Expected dict like type for {0}:{1}'.format(
            type(object_value), object_value))

    object_spec = deref(object_spec)
    required_fields = object_spec.get('required', [])

    result = {}
    for k, v in iteritems(object_value):
        prop_spec = get_spec_for_prop(
            swagger_spec, object_spec, object_value, k)
        if v is None and k not in required_fields:
            result[k] = None
        elif 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
    properties = deref(object_spec).get('properties', {})
    for prop_name, prop_spec in iteritems(properties):
        if prop_name not in result:
            result[prop_name] = None
    return result
Exemplo n.º 21
0
def test_declared_property(address_spec, address):
    expected_spec = address_spec['properties']['street_name']
    result = get_spec_for_prop(address_spec, address, 'street_name')
    assert expected_spec == result
Exemplo n.º 22
0
def test_properties_not_present_and_additionalProperties_True(address):
    spec = {'type': 'object', 'additionalProperties': True}
    result = get_spec_for_prop(spec, address, 'street_name')
    assert result is None
Exemplo n.º 23
0
def test_additionalProperties_not_dict_like(minimal_swagger_spec, address_spec,
                                            address):
    address_spec['additionalProperties'] = 'i am not a dict'
    with pytest.raises(SwaggerMappingError) as excinfo:
        get_spec_for_prop(minimal_swagger_spec, address_spec, address, 'city')
    assert "Don't know what to do" in str(excinfo.value)
Exemplo n.º 24
0
def test_declared_property(minimal_swagger_spec, address_spec, address):
    expected_spec = address_spec['properties']['street_name']
    result = get_spec_for_prop(
        minimal_swagger_spec, address_spec, address, 'street_name')
    assert expected_spec == result