-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
The float converter does not recognise whole numbers #1645
Comments
For accepting an integer, just add another route decorator that is the same except with the int converter. I don't have a solution to the other though |
It's deliberate that the converters accept one representation for each type. If you need something else, use a custom converter: from werkzeug.routing import BaseConverter
class IntFloatConverter(BaseConverter):
regex = r"-?(?:\d+(?:\.(?:\d+)?)?|\.\d+)"
def to_python(self, value):
try:
return int(value)
except ValueError:
return float(value)
app.url_map.converters["number"] = IntFloatConverter |
Adding another route accepting an Yes, I saw the "one representation" line before, but that is neither achievable nor desirable. These are a few different representations of the same number:
What exactly is gained by excluding the first two in that list? The downside is obvious in that we now have to explain to all clients that they have to make sure that whatever they use to format the arguments into a URL uses a pattern that forces at least one digit after the decimal point (and you'd better unit test that). We can't even make a good error message since it'll just come out as @davidism yes, I guess making a |
The
float
converter does not allow integers (123
) or floats with trailing periods (123.
) but either of those are likely to be created by both machine and human clients of an API when they are told to enter a number or even a float.This is also causing our APIs created with Connexion from an OpenApi spec to break that spec since OpenApi does not require values of the
number
type to have a period followed by a number and the routing is delegated to Flask. See spec-first/connexion#1041I propose a solution similar to #729, adding an
allow_int
argument to thefloat
converter which would relax the regular expression and isFalse
by default. I'm willing to make that pull request if I think it might in principle be merged.The text was updated successfully, but these errors were encountered: