Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: "ignored_view_names" arg on "add_pyramid_paths" function has no effect #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions pyramid_apispec/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def add_pyramid_paths(
request_method=None,
operations=None,
autodoc=True,
ignored_view_names=None,
**kwargs
):
"""
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down
70 changes: 70 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py36,py37,py38,py39,pre-commit
envlist = py36,py37,py38,py39,py310,pre-commit

[testenv]
extras = dev
Expand Down