def test_api_register_converter_before_and_after_init( self, app, openapi_version): api = Api() blp = Blueprint('test', 'test', url_prefix='/test') class CustomConverter_1(BaseConverter): pass class CustomConverter_2(BaseConverter): pass app.url_map.converters['custom_str_1'] = CustomConverter_1 app.url_map.converters['custom_str_2'] = CustomConverter_2 api.register_converter(CustomConverter_1, 'custom string 1') api.init_app(app) api.register_converter(CustomConverter_2, 'custom string 2') @blp.route('/1/<custom_str_1:val>') def test_func_1(val): pass @blp.route('/2/<custom_str_2:val>') def test_func_2(val): pass api.register_blueprint(blp) spec = api.spec.to_dict() parameter_1 = spec['paths']['/test/1/{val}']['parameters'][0] parameter_2 = spec['paths']['/test/2/{val}']['parameters'][0] if 'openapi_version' == '2.0': assert parameter_1['type'] == 'custom string 1' assert parameter_2['type'] == 'custom string 2' else: assert parameter_1['schema']['type'] == 'custom string 1' assert parameter_2['schema']['type'] == 'custom string 2'
def test_api_register_converter( self, app, view_type, custom_format, openapi_version): api = Api(app) blp = Blueprint('test', 'test', url_prefix='/test') class CustomConverter(BaseConverter): pass app.url_map.converters['custom_str'] = CustomConverter api.register_converter(CustomConverter, 'custom string', custom_format) if view_type == 'function': @blp.route('/<custom_str:val>') def test_func(val): return jsonify(val) else: @blp.route('/<custom_str:val>') class TestMethod(MethodView): def get(self, val): return jsonify(val) api.register_blueprint(blp) spec = api.spec.to_dict() schema = {'type': 'custom string'} # If custom_format is None (default), it does not appear in the spec if custom_format is not None: schema['format'] = 'custom' parameter = {'in': 'path', 'name': 'val', 'required': True} if 'openapi_version' == '2.0': parameter.update(schema) else: parameter['schema'] = schema assert spec['paths']['/test/{val}']['get']['parameters'] == [parameter]
def test_api_register_converter(self, app, view_type, custom_format, name): api = Api(app) blp = Blueprint('test', 'test', url_prefix='/test') class CustomConverter(BaseConverter): pass app.url_map.converters['custom_str'] = CustomConverter api.register_converter( CustomConverter, 'custom string', custom_format, name=name) if view_type == 'function': @blp.route('/<custom_str:val>') def test_func(val): return jsonify(val) else: @blp.route('/<custom_str:val>') class TestMethod(MethodView): def get(self, val): return jsonify(val) api.register_blueprint(blp) spec = api.spec.to_dict() # If custom_format is None (default), it does not appear in the spec if custom_format is not None: parameters = [{'in': 'path', 'name': 'val', 'required': True, 'type': 'custom string', 'format': 'custom'}] else: parameters = [{'in': 'path', 'name': 'val', 'required': True, 'type': 'custom string'}] assert spec['paths']['/test/{val}']['get']['parameters'] == parameters # Converter is registered in the app iff name it not None if name is not None: assert api._app.url_map.converters[name] == CustomConverter else: assert name not in api._app.url_map.converters