Skip to content
Discussion options

You must be logged in to vote

Ran into this once, too.

This is not really an issue with FastAPI nor Pydantic. Query string parameters are always strings, meaning everything you pass here will always end up being a string before being parsed by FastAPI or Pydantic.

You could argue that for boolean values this problem is easy to evade as you only have three possible values here: true, false and null. However this would only be a fix for this particular case and would create some strange inconsistencies inside the way things work here.

Lets' change this to being a str and see what that changes:

@app.get("/")
def home(q: Optional[str] = "foo"):
    return {"Hello": q}

How could we now pass None here?

  • /: Must be the defau…

Replies: 6 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
5 participants
Converted from issue

This discussion was converted from issue #4708 on February 28, 2023 15:56.