Skip to content

Commit

Permalink
Fix OpenAPI parameters containing other parameters (#1523)
Browse files Browse the repository at this point in the history
* Fix OpenAPI parameters containing other parameters

OpenAPI parameters can be unspecified and sometimes contain other
parameters. The current behavior is to assume it's a bracket parameter
and nest the containing parameter within the contained parameter, which
breaks the schema as the original parameter now seems to contain a
nested object.

We can avert this by checking for the presence of a '[' in the
parameter.

* Trigger Github workflow

Co-authored-by: Ricardo Piro-Rael <rpiro-rael@ironox.com>
  • Loading branch information
rickpr and Ricardo Piro-Rael committed May 9, 2022
1 parent cff0310 commit 4dee786
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
7 changes: 3 additions & 4 deletions connexion/decorators/uri_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,9 @@ def _make_deep_object(self, k, v):
if k in self.param_schemas.keys():
return k, v, False
else:
for keys in self.param_schemas.keys():
if k.startswith(keys):
rest = keys.replace(k, '')
root_key = rest
for key in self.param_schemas.keys():
if k.startswith(key) and "[" in k:
root_key = key.replace(k, '')

if not root_key:
root_key = k.split("[", 1)[0]
Expand Down
33 changes: 33 additions & 0 deletions tests/decorators/test_uri_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
("letters", "d,e,f")])
QUERY2 = MultiDict([("letters", "a"), ("letters", "b|c"),
("letters", "d|e|f")])

QUERY3 = MultiDict([("letters[eq]", ["a"]), ("letters[eq]", ["b", "c"]),
("letters[eq]", ["d", "e", "f"])])
QUERY4 = MultiDict([("letters[eq]", "a"), ("letters[eq]", "b|c"),
("letters[eq]", "d|e|f")])
QUERY5 = MultiDict([("letters[eq]", "a"), ("letters[eq]", "b,c"),
("letters[eq]", "d,e,f")])

QUERY6 = MultiDict([("letters_eq", "a")])
PATH1 = {"letters": "d,e,f"}
PATH2 = {"letters": "d|e|f"}
CSV = "csv"
Expand Down Expand Up @@ -135,3 +138,33 @@ class Request:
p = parser_class(parameters, body_defn)
res = p(lambda x: x)(request)
assert res.query["letters[eq]"] == expected

@pytest.mark.parametrize("parser_class, expected, query_in, collection_format", [
(OpenAPIURIParser, ['a'], QUERY6, CSV),
(Swagger2URIParser, ['a'], QUERY6, CSV),
(FirstValueURIParser, ['a'], QUERY6, CSV),
(AlwaysMultiURIParser, ['a'], QUERY6, CSV),
(Swagger2URIParser, ['a'], QUERY6, MULTI),
(FirstValueURIParser, ['a'], QUERY6, MULTI),
(AlwaysMultiURIParser, ['a'], QUERY6, MULTI),
(Swagger2URIParser, ['a'], QUERY6, PIPES),
(FirstValueURIParser, ['a'], QUERY6, PIPES),
(AlwaysMultiURIParser, ['a'], QUERY6, PIPES)])
def test_uri_parser_query_params_with_underscores(parser_class, expected, query_in, collection_format):
class Request:
query = query_in
path_params = {}
form = {}

request = Request()
parameters = [
{"name": "letters",
"in": "query",
"type": "string",
"items": {"type": "string"},
"collectionFormat": collection_format}
]
body_defn = {}
p = parser_class(parameters, body_defn)
res = p(lambda x: x)(request)
assert res.query["letters_eq"] == expected

0 comments on commit 4dee786

Please sign in to comment.