Skip to content

Commit

Permalink
Added detype_patterns() with @cache.
Browse files Browse the repository at this point in the history
Avoids excessive calls to `detype_pattern()` with the same arguments.

Co-authored-by: T. Franzel <tfranzel@users.noreply.github.com>
  • Loading branch information
ngnpope and tfranzel committed Mar 20, 2022
1 parent 52d534b commit fd22244
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ def modify_for_versioning(patterns, method, path, view, requested_version):
elif issubclass(view.versioning_class, versioning.NamespaceVersioning):
try:
view.request.resolver_match = get_resolver(
urlconf=tuple(detype_pattern(p) for p in patterns)
urlconf=detype_patterns(tuple(patterns)),
).resolve(path)
except Resolver404:
error(f"namespace versioning path resolution failed for {path}. Path will be ignored.")
Expand Down Expand Up @@ -976,6 +976,12 @@ def analyze_named_regex_pattern(path):
return result


@cache
def detype_patterns(patterns):
"""Cache detyped patterns due to the expensive nature of rebuilding URLResolver."""
return tuple(detype_pattern(pattern) for pattern in patterns)


def detype_pattern(pattern):
"""
return an equivalent pattern that accepts arbitrary values for path parameters.
Expand Down

0 comments on commit fd22244

Please sign in to comment.