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
More liberal flask number converters for float and int in paths #1306
Conversation
Hi @logi, thanks for this PR. We switched to the |
db3d97c
to
2a7c5fd
Compare
These don't try to enforce a "single representation" of paths but instead try to convert the numbers that callers pass in. Addresses spec-first#1040 and spec-first#1041
@RobbeSneyders AFAICT that's done now |
tests/fakeapi/hello/__init__.py
Outdated
|
||
|
||
def test_get_somefloat(somefloat): | ||
return type(somefloat).__name__ | ||
return '%s %g' % (type(somefloat).__name__, somefloat) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use "new" Python f-strings (f"{x} {y}"
)
tests/api/test_parameters.py
Outdated
app_client = simple_app.app.test_client() | ||
resp = app_client.get('/v1.0/test-int-path/123') # type: flask.Response | ||
assert resp.data.decode('utf-8', 'replace') == '"int"\n' | ||
resp = app_client.get('/v1.0/test-int-path/'+arg) # type: flask.Response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resp = app_client.get('/v1.0/test-int-path/'+arg) # type: flask.Response | |
resp = app_client.get(f"/v1.0/test-int-path/{arg}") # type: flask.Response |
👍 |
This change installs more liberal flask converters for
float
andint
. These don't try to enforce a "single representation" of paths but instead try to convert the numbers that callers pass in.This is the minimal change to get this effect and installs the custom number converters where the custom JSON converter is being installed already. An alternative would be to register the new converters as
integer
andnumber
in Flask and update thePATH_PARAMETER_CONVERTERS
mapping, but that's a larger change and would change the generated flask paths which could affect existing code.Fixes #1040
Fixes #1041
The tests for numbers in paths are modified to return not just
int
orfloat
but also the value seen by the handler so we can ascertain that the conversion works and not just the routing. Then a few number formats are added.