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

Update NumberConverter regex to match new Werkzeug behavior (v3) #1644

Merged
merged 2 commits into from
Feb 21, 2023
Merged
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
2 changes: 1 addition & 1 deletion connexion/frameworks/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def default(self, o):
class NumberConverter(werkzeug.routing.BaseConverter):
"""Flask converter for OpenAPI number type"""

regex = r"[+-]?[0-9]*(\.[0-9]*)?"
regex = r"[+-]?[0-9]*(?:\.[0-9]*)?"

def to_python(self, value):
return float(value)
Expand Down
5 changes: 5 additions & 0 deletions examples/helloworld/spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ paths:
schema:
type: string
example: "dave"
requestBody:
content:
application/json:
schema:
type: object
13 changes: 13 additions & 0 deletions tests/api/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ def test_path_parameter_somefloat(simple_app, arg, result):
assert resp.text == f'"{result}"\n'


@pytest.mark.parametrize(
"arg, arg2, result",
[
["-0.000000001", "0.3", "float -1e-09, 0.3"],
],
)
def test_path_parameter_doublefloat(simple_app, arg, arg2, result):
assert isinstance(arg, str) and isinstance(arg2, str) # sanity check
app_client = simple_app.test_client()
resp = app_client.get(f"/v1.0/test-float-path/{arg}/{arg2}")
assert resp.text == f'"{result}"\n'


def test_path_parameter_somefloat__bad(simple_app):
# non-float values will not match Flask route
app_client = simple_app.test_client()
Expand Down
4 changes: 4 additions & 0 deletions tests/fakeapi/hello/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ def test_get_somefloat(somefloat):
return f"{type(somefloat).__name__} {somefloat:g}"


def test_get_doublefloat(somefloat, someotherfloat):
return f"{type(somefloat).__name__} {somefloat:g}, {someotherfloat}"


def test_default_param(name):
return {"app_name": name}

Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/simple/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,24 @@ paths:
responses:
'200':
description: OK
'/test-float-path/{somefloat}/{someotherfloat}':
get:
summary: Test type casting of path parameter
operationId: fakeapi.hello.test_get_doublefloat
parameters:
- name: somefloat
in: path
required: true
schema:
type: number
- name: someotherfloat
in: path
required: true
schema:
type: number
responses:
'200':
description: OK
/test-default-query-parameter:
get:
summary: Test if default parameter is passed to function
Expand Down
17 changes: 17 additions & 0 deletions tests/fixtures/simple/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,23 @@ paths:
200:
description: OK

/test-float-path/{somefloat}/{someotherfloat}:
get:
summary: Test type casting of path parameter
operationId: fakeapi.hello.test_get_doublefloat
parameters:
- name: somefloat
in: path
type: number
required: true
- name: someotherfloat
in: path
type: number
required: true
responses:
200:
description: O

/test-default-query-parameter:
get:
summary: Test if default parameter is passed to function
Expand Down