def test_defaults_to_csv(empty_swagger_spec, array_spec): del array_spec['collectionFormat'] assert [1, 2, 3] == unmarshal_collection_format( empty_swagger_spec, array_spec, '1,2,3', )
def test_formats(empty_swagger_spec, array_spec): for fmt, sep in COLLECTION_FORMATS.items(): array_spec['collectionFormat'] = fmt param_value = sep.join(['1', '2', '3']) assert [1, 2, 3] == unmarshal_collection_format(empty_swagger_spec, array_spec, param_value)
def test_array_is_none_and_nullable(empty_swagger_spec, array_spec): array_spec['required'] = True array_spec['x-nullable'] = True assert unmarshal_collection_format( empty_swagger_spec, array_spec, value=None, ) is None
def test_formats_empty_list(empty_swagger_spec, array_spec, format_name, separator): array_spec['collectionFormat'] = format_name param_value = separator.join([]) assert [] == unmarshal_collection_format( empty_swagger_spec, array_spec, param_value, )
def test_multi_no_op_because_handled_by_http_client_lib( empty_swagger_spec, array_spec, ): array_spec['collectionFormat'] = 'multi' assert [1, 2, 3] == unmarshal_collection_format( empty_swagger_spec, array_spec, [1, 2, 3], )
def test_ref(minimal_swagger_dict, array_spec): for fmt, sep in COLLECTION_FORMATS.items(): array_spec['collectionFormat'] = fmt minimal_swagger_dict['parameters'] = {'BizIdsParam': array_spec} ref_spec = {'$ref': '#/parameters/BizIdsParam'} swagger_spec = Spec(minimal_swagger_dict) param_value = sep.join(['1', '2', '3']) assert [1, 2, 3] == unmarshal_collection_format(swagger_spec, ref_spec, param_value)
def test_ref(minimal_swagger_dict, array_spec): for fmt, sep in COLLECTION_FORMATS.items(): array_spec['collectionFormat'] = fmt minimal_swagger_dict['parameters'] = { 'BizIdsParam': array_spec } ref_spec = {'$ref': '#/parameters/BizIdsParam'} swagger_spec = Spec(minimal_swagger_dict) param_value = sep.join(['1', '2', '3']) assert [1, 2, 3] == unmarshal_collection_format( swagger_spec, ref_spec, param_value)
async def unmarshal_param(param, request): """Unmarshal the given parameter from the passed in request like object. :type param: :class:`bravado_core.param.Param` :type request: :class:`bravado_core.request.IncomingRequest` :return: value of parameter """ swagger_spec = param.swagger_spec deref = swagger_spec.deref param_spec = deref(get_param_type_spec(param)) location = param.location param_type = deref(param_spec.get('type')) cast_param = partial(cast_request_param, param_type, param.name) default_value = schema.get_default(swagger_spec, param_spec) if location == Location.path: raw_value = cast_param(request.match_info.get(param.name, None)) elif location == Location.query: raw_value = cast_param(request.query.get(param.name, default_value)) elif location == Location.header: raw_value = cast_param(request.headers.get(param.name, default_value)) elif location == Location.form_data: if param_type == 'file': raw_value = request.files.get(param.name, None) else: raw_value = cast_param(request.form.get(param.name, default_value)) elif location == Location.body: # TODO: verify content-type header try: raw_value = request.json() except ValueError as json_error: raise SwaggerMappingError( "Error reading request body JSON: {0}".format(str(json_error))) else: raise SwaggerMappingError( "Don't know how to unmarshal_param with location {0}".format( location)) if raw_value is None and not schema.is_required(swagger_spec, param_spec): return None if param_type == 'array' and location != Location.body: raw_value = unmarshal_collection_format(swagger_spec, param_spec, raw_value) if swagger_spec.config['validate_requests']: validate_schema_object(swagger_spec, param_spec, raw_value) value = unmarshal_schema_object(swagger_spec, param_spec, raw_value) return value
def test_array_is_none_and_required(empty_swagger_spec, array_spec): array_spec['required'] = True with pytest.raises(SwaggerMappingError) as excinfo: unmarshal_collection_format(empty_swagger_spec, array_spec, value=None) assert 'is a required value' in str(excinfo.value)
def test_array_is_none_and_nullable(empty_swagger_spec, array_spec): array_spec['required'] = True array_spec['x-nullable'] = True assert unmarshal_collection_format(empty_swagger_spec, array_spec, value=None) is None
def test_array_is_none_and_not_required(empty_swagger_spec, array_spec): assert unmarshal_collection_format( empty_swagger_spec, array_spec, value=None) is None
def test_array_is_none_and_not_required(empty_swagger_spec, array_spec): assert unmarshal_collection_format(empty_swagger_spec, array_spec, value=None) is None
def test_multi_no_op_because_handled_by_http_client_lib(empty_swagger_spec, array_spec): array_spec['collectionFormat'] = 'multi' assert [1, 2, 3] == unmarshal_collection_format( empty_swagger_spec, array_spec, [1, 2, 3])
def test_formats(empty_swagger_spec, array_spec): for fmt, sep in COLLECTION_FORMATS.items(): array_spec['collectionFormat'] = fmt param_value = sep.join(['1', '2', '3']) assert [1, 2, 3] == unmarshal_collection_format( empty_swagger_spec, array_spec, param_value)
def test_defaults_to_csv(empty_swagger_spec, array_spec): del array_spec['collectionFormat'] assert [1, 2, 3] == unmarshal_collection_format( empty_swagger_spec, array_spec, '1,2,3')