From 44d3fd3927379ede7bae5c6250ede2bd59a1316b Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Tue, 8 Dec 2020 12:44:53 -0800 Subject: [PATCH] Bug: Prioritize concrete paths over template paths. --- openapi_core/templating/paths/finders.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/openapi_core/templating/paths/finders.py b/openapi_core/templating/paths/finders.py index bc90d45..394ce19 100644 --- a/openapi_core/templating/paths/finders.py +++ b/openapi_core/templating/paths/finders.py @@ -39,8 +39,9 @@ class PathFinder(object): raise ServerNotFound(request.full_url_pattern) def _get_paths_iter(self, full_url_pattern): + template_paths = [] for path_pattern, path in iteritems(self.spec.paths): - # simple path + # simple path. Return right away since it is always the most concrete if full_url_pattern.endswith(path_pattern): path_result = TemplateResult(path_pattern, {}) yield (path, path_result) @@ -49,7 +50,11 @@ class PathFinder(object): result = search(path_pattern, full_url_pattern) if result: path_result = TemplateResult(path_pattern, result.named) - yield (path, path_result) + template_paths.append((path, path_result)) + + # Fewer variables -> more concrete path + for path in sorted(template_paths, key=lambda p: len(p[1].variables)): + yield path def _get_operations_iter(self, request_method, paths_iter): for path, path_result in paths_iter: