Esempio n. 1
0
def to_wire(swagger_spec, primitive_spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation if it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :param value: primitive to convert to wire representation
    :type value: int, long, float, boolean, string, unicode, object, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    :raises: SwaggerMappingError when format.to_wire raises an exception
    """
    if value is None or not schema.has_format(swagger_spec, primitive_spec):
        return value
    format_name = schema.get_format(swagger_spec, primitive_spec)
    formatter = swagger_spec.get_format(format_name)

    try:
        return formatter.to_wire(value) if formatter else value
    except Exception as e:
        raise SwaggerMappingError(
            'Error while marshalling value={} to type={}{}.'.format(
                value, primitive_spec['type'],
                '/{}'.format(primitive_spec['format']) if 'format' in primitive_spec else '',
            ),
            e,
        )
Esempio n. 2
0
def to_wire(swagger_spec, primitive_spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation if it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :param value: primitive to convert to wire representation
    :type value: int, long, float, boolean, string, unicode, object, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    :raises: SwaggerMappingError when format.to_wire raises an exception
    """
    if value is None or not schema.has_format(swagger_spec, primitive_spec):
        return value
    format_name = schema.get_format(swagger_spec, primitive_spec)
    formatter = swagger_spec.get_format(format_name)

    try:
        return formatter.to_wire(value) if formatter else value
    except Exception as e:
        raise SwaggerMappingError(
            'Error while marshalling value={} to type={}{}.'.format(
                value, primitive_spec['type'],
                '/{}'.format(primitive_spec['format']) if 'format' in primitive_spec else '',
            ),
            e,
        )
Esempio n. 3
0
def to_python(spec, value):
    """Converts a value in wire format to its python representation given
    the 'format' in the given spec.

    :param spec: spec for a primitive type as a dict
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, object, etc
    """
    if value is None or not schema.has_format(spec):
        return value
    formatter = get_format(schema.get_format(spec))
    return formatter.to_python(value) if formatter else value
Esempio n. 4
0
def to_python(swagger_spec, primitive_spec, value):
    """Converts a value in wire format to its python representation if
     it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, object, etc
    """
    if value is None or not schema.has_format(primitive_spec):
        return value
    formatter = swagger_spec.get_format(schema.get_format(primitive_spec))
    return formatter.to_python(value) if formatter else value
Esempio n. 5
0
def to_python(swagger_spec, primitive_spec, value):
    """Converts a value in wire format to its python representation if
     it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, object, etc
    """
    if value is None or not schema.has_format(primitive_spec):
        return value
    formatter = swagger_spec.get_format(schema.get_format(primitive_spec))
    return formatter.to_python(value) if formatter else value
Esempio n. 6
0
def to_wire(spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation given the 'format' in the given spec.

    :param spec: spec for a primitive type as a dict
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    """
    if value is None or not schema.has_format(spec):
        return value

    to_wire, _, _ = _formatters[schema.get_format(spec)]
    return to_wire(value)
Esempio n. 7
0
def to_wire(swagger_spec, primitive_spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation if it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :param value: primitive to convert to wire representation
    :type value: int, long, float, boolean, string, unicode, object, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    """
    if value is None or not schema.has_format(swagger_spec, primitive_spec):
        return value
    format_name = schema.get_format(swagger_spec, primitive_spec)
    formatter = swagger_spec.get_format(format_name)
    return formatter.to_wire(value) if formatter else value
Esempio n. 8
0
def to_wire(swagger_spec, primitive_spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation if it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :param value: primitive to convert to wire representation
    :type value: int, long, float, boolean, string, unicode, object, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    """
    if value is None or not schema.has_format(swagger_spec, primitive_spec):
        return value
    format_name = schema.get_format(swagger_spec, primitive_spec)
    formatter = swagger_spec.get_format(format_name)
    return formatter.to_wire(value) if formatter else value
Esempio n. 9
0
def _unmarshaling_method_primitive_type(swagger_spec, object_schema):
    # type: (Spec, JSONDict) -> UnmarshalingMethod
    """
    Determine the unmarshaling method needed for a schema of a primitive type.

    The method will be responsible for the identification of the eventual :class:`bravado_core.formatter.SwaggerFormat`
    transformation to apply.

    :param swagger_spec: Spec object
    :param object_schema: Schema of the primitive type
    """
    format_name = schema.get_format(swagger_spec, object_schema)
    swagger_format = swagger_spec.get_format(
        format_name) if format_name is not None else None
    if swagger_format is not None:
        return swagger_format.to_python
    else:
        return _no_op_unmarshaling
Esempio n. 10
0
def test_ref_not_found(minimal_swagger_dict, int_spec):
    minimal_swagger_dict['definitions']['Int'] = int_spec
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    assert get_format(swagger_spec, {'$ref': '#/definitions/Int'}) is None
Esempio n. 11
0
def test_ref_found(minimal_swagger_dict, int32_spec):
    minimal_swagger_dict['definitions']['Int32'] = int32_spec
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    assert 'int32' == get_format(swagger_spec, {'$ref': '#/definitions/Int32'})
Esempio n. 12
0
def test_not_found(minimal_swagger_spec, int_spec):
    assert get_format(minimal_swagger_spec, int_spec) is None
Esempio n. 13
0
def test_found(minimal_swagger_spec, int32_spec):
    assert 'int32' == get_format(minimal_swagger_spec, int32_spec)