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

More liberal flask number converters for float and int in paths #1306

Merged
merged 3 commits into from Jul 14, 2021

Conversation

logi
Copy link
Contributor

@logi logi commented Oct 16, 2020

This change installs more liberal flask converters for float and int. 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 and number in Flask and update the PATH_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 or float 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.

@coveralls
Copy link

coveralls commented Oct 16, 2020

Coverage Status

Coverage increased (+0.01%) to 96.999% when pulling 465cdf5 on Belgingur:master into 443bd20 on zalando:main.

@RobbeSneyders RobbeSneyders changed the base branch from master to main July 14, 2021 12:44
@RobbeSneyders
Copy link
Member

Hi @logi, thanks for this PR. We switched to the main branch, could you rebase / merge?

@logi logi force-pushed the master branch 2 times, most recently from db3d97c to 2a7c5fd Compare July 14, 2021 17:27
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
@logi
Copy link
Contributor Author

logi commented Jul 14, 2021

@RobbeSneyders AFAICT that's done now



def test_get_somefloat(somefloat):
return type(somefloat).__name__
return '%s %g' % (type(somefloat).__name__, somefloat)
Copy link
Contributor

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}")

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

@hjacobs hjacobs added this to the 2.9 milestone Jul 14, 2021
@hjacobs
Copy link
Contributor

hjacobs commented Jul 14, 2021

👍

@hjacobs hjacobs merged commit a8375a1 into spec-first:main Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants