def test_matching_with_full_nested_list_resource(): schema = SchemaFactory(paths={ '/get/main/': { 'get': {}, }, '/get/main/{id}': { 'get': { 'parameters': [{ 'name': 'id', 'in': PATH, 'type': STRING, 'required': True, }], }, }, '/get/main/{id}/nested/': { 'get': { 'parameters': [ { 'name': 'id', 'in': PATH, 'type': STRING, 'required': True, }, ], }, }, }, ) paths = schema['paths'] path = match_path_to_api_path( path_definitions=paths, target_path='/get/main/1234/nested/', ) assert path == '/get/main/{id}/nested/'
def test_match_target_path_missing_base_path(): target_path = '/path' base_path = '/api' schema = SchemaFactory( paths={ target_path: {}, }, basePath=base_path, ) with pytest.raises(LookupError): match_path_to_api_path( path_definitions=schema['paths'], target_path=target_path, base_path=base_path, )
def test_match_target_path_to_api_path(path, schema_path): schema = SchemaFactory( paths={ '/path': {}, '/path.json': {}, '/get/{id}': { 'parameters': [{ 'name': 'id', 'in': PATH, 'type': INTEGER, 'required': True, }], }, '/post': {}, }, basePath='/api', ) paths = schema['paths'] base_path = schema['basePath'] path = match_path_to_api_path( path_definitions=paths, target_path=path, base_path=base_path, ) assert path == schema_path
def test_matching_with_no_path_when_base_path_is_not_just_a_slash(): # Since the base_path isn't /, we do expect the specification # to be correct. So, this is expected to fail # The target path that should work is /foo/foo/get/main schema = SchemaFactory(base_path='/foo', paths={ '/foo/get/main': { 'get': {}, }, }) with pytest.raises(LookupError): match_path_to_api_path( path_definitions=schema['paths'], target_path='/foo/get/main', base_path=schema['base_path'], )
def test_matching_with_no_path_when_base_path_is_not_just_a_slash(): # Since the base_path isn't /, we do expect the specification # to be correct. So, this is expected to fail # The target path that should work is /foo/foo/get/main schema = SchemaFactory( base_path='/foo', paths={ '/foo/get/main': { 'get': {}, }, } ) with pytest.raises(LookupError): match_path_to_api_path( path_definitions=schema['paths'], target_path='/foo/get/main', base_path=schema['base_path'], )
def test_matching_target_path_with_multiple_slashes(): schema = SchemaFactory(base_path='/', paths={ '/get/main': { 'get': {}, }, }) path = match_path_to_api_path( path_definitions=schema['paths'], target_path='/get/////main', base_path=schema['base_path'], ) assert path == '/get/main'
def test_matching_target_path_with_simple_comparison_proceeding(): schema = SchemaFactory(base_path='/', paths={ '/api/v1/search/': { 'get': {}, }, '/api/v1/{longer_id}/': { 'post': {}, } }) path = match_path_to_api_path(path_definitions=schema['paths'], target_path='/api/v1/search/', base_path=schema['base_path']) assert path == '/api/v1/search/'
def test_matching_target_path_with_multiple_slashes(): schema = SchemaFactory( base_path='/', paths={ '/get/main': { 'get': {}, }, } ) path = match_path_to_api_path( path_definitions=schema['paths'], target_path='/get/////main', base_path=schema['base_path'], ) assert path == '/get/main'
def validate_path_to_api_path(path, paths, basePath='', context=None, **kwargs): """ Given a path, find the api_path it matches. """ if context is None: context = {} try: api_path = match_path_to_api_path( path_definitions=paths, target_path=path, base_path=basePath, context=context, ) except LookupError as err: raise ValidationError(str(err)) except MultiplePathsFound as err: raise ValidationError(str(err)) return api_path
def test_matching_with_full_nested_parametrized_resource(): schema = SchemaFactory( paths={ '/get/main/':{ 'get':{}, }, '/get/main/{id}': { 'get': { 'parameters': [{ 'name': 'id', 'in': PATH, 'type': STRING, 'required': True, }], }, }, '/get/main/{id}/nested/{other_id}': { 'get': { 'parameters': [ { 'name': 'id', 'in': PATH, 'type': STRING, 'required': True, }, { 'name': 'other_id', 'in': PATH, 'type': STRING, 'required': True, }, ], }, }, }, ) paths = schema['paths'] path = match_path_to_api_path( path_definitions=paths, target_path='/get/main/1234/nested/5678', ) assert path == '/get/main/{id}/nested/{other_id}'
def test_matching_target_path_with_simple_comparison_proceeding(): schema = SchemaFactory( base_path='/', paths={ '/api/v1/search/' : { 'get': {}, }, '/api/v1/{longer_id}/' : { 'post': {}, } } ) path = match_path_to_api_path( path_definitions=schema['paths'], target_path='/api/v1/search/', base_path=schema['base_path'] ) assert path == '/api/v1/search/'
def test_match_path_with_parameter_defined_in_operation(): schema = SchemaFactory(paths={ '/get/{id}': { 'get': { 'parameters': [{ 'name': 'id', 'in': PATH, 'type': INTEGER, 'required': True, }], }, }, }, ) paths = schema['paths'] path = match_path_to_api_path( path_definitions=paths, target_path='/get/1234', ) assert path == '/get/{id}'
def test_matching_with_nested_path(): schema = SchemaFactory( paths={ '/get/{id}': { 'get': { 'parameters': [{ 'name': 'id', 'in': PATH, 'type': STRING, 'required': True, }], }, }, '/get/{id}/nested/{other_id}': { 'get': { 'parameters': [ { 'name': 'id', 'in': PATH, 'type': STRING, 'required': True, }, { 'name': 'other_id', 'in': PATH, 'type': STRING, 'required': True, }, ], }, }, }, ) paths = schema['paths'] path = match_path_to_api_path( path_definitions=paths, target_path='/get/1234/nested/5678', ) assert path == '/get/{id}/nested/{other_id}'
def test_match_path_with_parameter_defined_in_operation(): schema = SchemaFactory( paths={ '/get/{id}': { 'get': { 'parameters': [{ 'name': 'id', 'in': PATH, 'type': INTEGER, 'required': True, }], }, }, }, ) paths = schema['paths'] path = match_path_to_api_path( path_definitions=paths, target_path='/get/1234', ) assert path == '/get/{id}'
def validate_path_to_api_path(path, paths, basePath='', parameters=None, **kwargs): """ Given a path, find the api_path it matches. """ if parameters is None: parameters = {} try: api_path = match_path_to_api_path( path_definitions=paths, target_path=path, base_path=basePath, global_parameters=parameters, ) except LookupError as err: raise ValidationError(str(err)) except MultiplePathsFound as err: raise ValidationError(str(err)) return api_path