Fastapi: Is pydantic pedantic enough? / json false is saved as "False"

Created on 25 Dec 2019  路  3Comments  路  Source: tiangolo/fastapi

I was mucking about with fastapi and tried sending the json value false to a str field.
Imagine my surprise when "False" was saved in my database instead of getting a 4xx response.

Now, I might have done something wrong, though I've just copied/pasted the docs - which just use the default settings.

The data passed into pydantic really is a False

> request._json
{'text': False, 'completed': True}

See https://github.com/samuelcolvin/pydantic/issues/1130 as well.

I'm wondering if the default settings are pedantic is enough or if fastapi should ship with a StrictModel instead?

question

All 3 comments

There is a StrictStr type in pydantic if you want to disable this behavior. Note that that would also prevent coercion from numeric types as well.

(See the linked pydantic issue discussion for a more lengthy response about the tradeoffs involved with changing this behavior.)

I'm talking about expected behavior from a consumer/user's standpoint.
As a consumer it makes no sense that a False it converted into "False" and saved in the DB.

That is why I brought this up to see if fastapi should take an opinionated stance on how strict it's endpoints should be. Maybe it should use a StrictModel implementation by default.

I'm not going to pursue using fastapi any further, so please do whatever makes sense.
Have a good one, see you on the flipside!

Yeah, that's how Python itself works. bool is a subclass of int and so on. Pydantic is quite Pythonic and it doesn't enforce what Python wouldn't. Also, there are several cases where you would depend on automatic string conversion. E.g. path, query, cookie, and header parameters. The specs deal only with strings, there are no other types. And still, there are the strict versions when you need to enforce them.

Anyway, hope you find a framework or language that suits your needs.

As I understand you already decided about your original question, I'll close this issue now.

Was this page helpful?
0 / 5 - 0 ratings