Starlette allows NaN as input JSON, but not as return JSON #1518
Replies: 3 comments
-
Okay - we're not actually catching and handling malformed JSON automatically in any case, so that's probably a more pressing issue prior to this. But yes, I'd be happy to have nan on input end up with exactly the same behavior as any other malformed JSON. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply Tom, I'll read the internals a bit more and try to draft something to hear your thoughts 😀 |
Beta Was this translation helpful? Give feedback.
-
I'm going to convert this to a discussion, since it's marginal. I think we'd consider pull requests here that switched the JSON parsing to be actually JSON compliant, rather than "okay sure but also here's a coupla constants that we also think are okay. lol". Here's the kinda thing you'd need... >>> def invalid_constant(value):
... raise ValueError("Invalid JSON {value!r}")
...
>>> json.loads("NaN", parse_constant=invalid_constant) Although really we'd want any change in behaviour there to raise |
Beta Was this translation helpful? Give feedback.
-
Although Starlette (correctly) does not allow
NaN
to be returned as part of a JSON response, it does allowNaN
fields when receiving input data.For example, the following would fail:
If that's something that we'd like to address, I'm happy to open a PR, which can do either of these things:
HTTPException(400, "NaN, +Inf, -Inf are not allowed")
**kwargs
torequest.json()
which would then be passed tojson.loads()
under the hoodBeta Was this translation helpful? Give feedback.
All reactions