Skip to content

How to accept an optional datetime query parameter which is explicitly provided as null? #11497

Closed Answered by YuriiMotov
maxispeicher asked this question in Questions
Discussion options

You must be logged in to vote

null in the http://127.0.0.1:8000/?dt=null reuest is not an empty value, but a string "null". So, if you want to let clients send this type of requests, you should include it in type annotation.
I think you can either use the approach with enum that is shown above, or you can use Literal['null']:

def root(dt: Annotated[datetime | None | Literal['null'], Query(description="I want to be able to provide `null` for this.")] = None):

You can also exlude this null from schema by using SkipJsonSchema:

def root(dt: Annotated[datetime | None | SkipJsonSchema[Literal['null']], Query(description="I want to be able to provide `null` for this.")] = None):

And by including Literal[''] you can also su…

Replies: 3 comments

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 maxispeicher
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
3 participants