From aeca7775fd4eae5d805c100ccabba0a607534a1b Mon Sep 17 00:00:00 2001 From: p1c2u Date: Thu, 23 Apr 2020 00:26:17 +0100 Subject: [PATCH] Templating parser path parameter search fix --- openapi_core/templating/util.py | 23 +++++++++++++++++++++-- tests/unit/templating/test_util.py | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/unit/templating/test_util.py diff --git a/openapi_core/templating/util.py b/openapi_core/templating/util.py index 2643011..8923fd6 100644 --- a/openapi_core/templating/util.py +++ b/openapi_core/templating/util.py @@ -1,13 +1,32 @@ from parse import Parser +class ExtendedParser(Parser): + def _handle_field(self, field): + # handle as path parameter field + field = field[1:-1] + path_parameter_field = "{%s:PathParameter}" % field + return super(ExtendedParser, self)._handle_field( + path_parameter_field) + + +def parse_path_parameter(text): + return text + + +parse_path_parameter.pattern = r"[^\/]+" +parse_path_parameter.name = "PathParameter" + + def search(path_pattern, full_url_pattern): - p = Parser(path_pattern) + extra_types = {parse_path_parameter.name: parse_path_parameter} + p = ExtendedParser(path_pattern, extra_types) p._expression = p._expression + '$' return p.search(full_url_pattern) def parse(server_url, server_url_pattern): - p = Parser(server_url) + extra_types = {parse_path_parameter.name: parse_path_parameter} + p = ExtendedParser(server_url, extra_types) p._expression = '^' + p._expression return p.parse(server_url_pattern) diff --git a/tests/unit/templating/test_util.py b/tests/unit/templating/test_util.py new file mode 100644 index 0000000..bd69540 --- /dev/null +++ b/tests/unit/templating/test_util.py @@ -0,0 +1,24 @@ +from openapi_core.templating.util import search + + +class TestSearch: + + def test_endswith(self): + path_patter = '/{test}/test' + full_url_pattern = '/test1/test/test2/test' + + result = search(path_patter, full_url_pattern) + + assert result.named == { + 'test': 'test2', + } + + def test_exact(self): + path_patter = '/{test}/test' + full_url_pattern = '/test/test' + + result = search(path_patter, full_url_pattern) + + assert result.named == { + 'test': 'test', + }