diff --git a/pyramid_apispec/helpers.py b/pyramid_apispec/helpers.py index 536ec76..d176ada 100644 --- a/pyramid_apispec/helpers.py +++ b/pyramid_apispec/helpers.py @@ -74,6 +74,7 @@ def add_pyramid_paths( request_method=None, operations=None, autodoc=True, + ignored_view_names=None, **kwargs ): """ @@ -92,6 +93,8 @@ def add_pyramid_paths( Operations dict that will be used instead of introspection :param autodoc: Include information about endpoints without markdown docstring + :param ignored_view_names: + List of strings with the view names to be ignored :param kwargs: Additional kwargs for predicate matching :return: @@ -104,7 +107,7 @@ def add_pyramid_paths( introspector = registry.introspector route = introspector.get("routes", route_name) introspectables = introspector.related(route) - ignored_view_names = kwargs.pop("ignored_view_names", None) + # needs to be rewritten to internal name if request_method: kwargs["request_methods"] = request_method @@ -114,7 +117,7 @@ def add_pyramid_paths( if ( not is_view(maybe_view) or not check_methods_matching(maybe_view, **kwargs) - or should_ignore_view(maybe_view, ignored_views=ignored_view_names) + or should_ignore_view(maybe_view, ignored_view_names) ): continue @@ -135,13 +138,12 @@ def is_view(introspectable): return introspectable.category_name == "views" -def should_ignore_view(introspectable, **kwargs): - to_ignore = kwargs.get("ignored_view_names") - if to_ignore is None: - to_ignore = ["cornice.pyramidhook._fallback_view"] +def should_ignore_view(introspectable, ignored_view_names): + if ignored_view_names is None: + ignored_view_names = ["cornice.pyramidhook._fallback_view"] - for name in to_ignore: - if name in introspectable.title: + for name in ignored_view_names: + if name in introspectable.get('route_name'): return True return False diff --git a/tests/tests.py b/tests/tests.py index 334daf4..0bfdd1e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -229,6 +229,76 @@ def hola(request): assert "/greet/es" in spec._paths assert spec._paths["/greet/es"]["get"]["description"] == "Spanish greeting" + def test_ignored_view_names(self, spec, config): + + def get_answer(request): + """Return the Answer to the Ultimate Question of Life, the Universe, and Everything. + + --- + description: Answer + responses: + 200: + description: Success + + """ + return Response("42") + + def should_panic(request): + """Return if you have to panic. + + --- + description: have to panic + responses: + 200: + description: Success + + """ + return Response("No") + + def get_towel_color(request): + """Return the color of your towel. + + --- + description: Towel color + responses: + 200: + description: Success + + """ + return Response("Blue with yellow stripes") + + config.add_route("give_the_answer", "/answer") + config.add_view(get_answer, route_name="give_the_answer", request_method=["get"]) + + config.add_route("ask_for_panic", "/panic") + config.add_view(should_panic, route_name="ask_for_panic", request_method=["get"]) + + config.add_route("towel_color", "/towel") + config.add_view(get_towel_color, route_name="towel_color", request_method=["get"]) + + config.make_wsgi_app() + + # Normally this list of routes would be caught from the introspector + # set manually here just for the test + ROUTE_LIST = [ + 'give_the_answer', + 'ask_for_panic', + 'towel_color' + ] + + IGNORED_VIEWS = [ + 'ask_for_panic', + 'towel' # Should ignore even if partial name is passed + ] + + for route_name in ROUTE_LIST: + add_pyramid_paths(spec, route_name, ignored_view_names=IGNORED_VIEWS) + + assert "/answer" in spec._paths + assert "/panic" not in spec._paths + assert "/towel" not in spec._paths + + def test_integration_with_docstring_introspection(self, spec, config): def hello(): """A greeting endpoint. diff --git a/tox.ini b/tox.ini index 1ba5060..25bf963 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py36,py37,py38,py39,pre-commit +envlist = py36,py37,py38,py39,py310,pre-commit [testenv] extras = dev