Skip to content

Commit 6a859b3

Browse files
Update NumberConverter regex to match new Werkzeug behavior (v2) (#1643)
Fixes #1635 See pallets/werkzeug#2518. Apparently, this is only an issue when you have more than 1 path parameter, so added a test for that.
2 parents e840840 + 9feaf11 commit 6a859b3

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

.github/workflows/pipeline.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
python-version: [3.6, 3.7, 3.8, 3.9]
12+
python-version: [3.7, 3.8, 3.9]
1313
steps:
1414
- uses: actions/checkout@v2
1515
- name: Set up Python ${{ matrix.python-version }}

connexion/apps/flask_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def default(self, o):
172172

173173
class NumberConverter(werkzeug.routing.BaseConverter):
174174
""" Flask converter for OpenAPI number type """
175-
regex = r"[+-]?[0-9]*(\.[0-9]*)?"
175+
regex = r"[+-]?[0-9]*(?:\.[0-9]*)?"
176176

177177
def to_python(self, value):
178178
return float(value)

tests/api/test_parameters.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ def test_path_parameter_somefloat(simple_app, arg, result):
175175
assert resp.data.decode('utf-8', 'replace') == f'"{result}"\n'
176176

177177

178+
@pytest.mark.parametrize('arg, arg2, result', [
179+
['-0.000000001', '0.3', 'float -1e-09, 0.3'],
180+
])
181+
def test_path_parameter_doublefloat(simple_app, arg, arg2, result):
182+
assert isinstance(arg, str) # sanity check
183+
app_client = simple_app.app.test_client()
184+
resp = app_client.get(f'/v1.0/test-float-path/{arg}/{arg2}') # type: flask.Response
185+
assert resp.data.decode('utf-8', 'replace') == f'"{result}"\n'
186+
187+
178188
def test_path_parameter_somefloat__bad(simple_app):
179189
# non-float values will not match Flask route
180190
app_client = simple_app.app.test_client()

tests/fakeapi/hello/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ def test_get_somefloat(somefloat):
274274
return f'{type(somefloat).__name__} {somefloat:g}'
275275

276276

277+
def test_get_doublefloat(somefloat, someotherfloat):
278+
return f'{type(somefloat).__name__} {somefloat:g}, {someotherfloat}'
279+
280+
277281
def test_default_param(name):
278282
return {"app_name": name}
279283

tests/fixtures/simple/openapi.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,24 @@ paths:
528528
responses:
529529
'200':
530530
description: OK
531+
'/test-float-path/{somefloat}/{someotherfloat}':
532+
get:
533+
summary: Test type casting of path parameter
534+
operationId: fakeapi.hello.test_get_doublefloat
535+
parameters:
536+
- name: somefloat
537+
in: path
538+
required: true
539+
schema:
540+
type: number
541+
- name: someotherfloat
542+
in: path
543+
required: true
544+
schema:
545+
type: number
546+
responses:
547+
'200':
548+
description: OK
531549
/test-default-query-parameter:
532550
get:
533551
summary: Test if default parameter is passed to function

tests/fixtures/simple/swagger.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,23 @@ paths:
394394
200:
395395
description: OK
396396

397+
/test-float-path/{somefloat}/{someotherfloat}:
398+
get:
399+
summary: Test type casting of path parameter
400+
operationId: fakeapi.hello.test_get_doublefloat
401+
parameters:
402+
- name: somefloat
403+
in: path
404+
type: number
405+
required: true
406+
- name: someotherfloat
407+
in: path
408+
type: number
409+
required: true
410+
responses:
411+
200:
412+
description: O
413+
397414
/test-default-query-parameter:
398415
get:
399416
summary: Test if default parameter is passed to function

tox.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ rst-roles=class
55

66
[tox]
77
envlist =
8-
{py36}-{min,pypi,dev}
98
{py37}-{min,pypi,dev}
109
{py38}-{min,pypi,dev}
1110
{py39}-{min,pypi,dev}
@@ -17,7 +16,6 @@ envlist =
1716

1817
[gh-actions]
1918
python =
20-
3.6: py36-min,py36-pypi
2119
3.7: py37-min,py37-pypi
2220
3.8: py38-min,py38-pypi
2321
3.9: py39-min,py39-pypi,flake8,isort-check,isort-check-examples,isort-check-tests,mypy

0 commit comments

Comments
 (0)