Search code examples
pythonfastapipydantic

optional parameters FastAPI


please help me. In the function I set the optional parameter param as a boolean value, but the docs (swagger) do not display the data type for param. How to fix it? I'm using python 3.12.1

from fastapi import FastAPI
from datetime import date


app=FastAPI()



@app.get ("/sales")
def sales(
    date_from: date, 
    date_to: date,
    param: bool | None  = None,
):
    return date_from, date_to, param

enter image description here

on version python 3.9 everything is fine


Solution

  • set the optional parameter "param" as a boolean value, but the docs do not display the data type

    Well, it's not a bool, is it? You decided to make it a bool | None instead. (Sometimes pronounced Optional[bool], same thing.)

    Let's fix that:

        param: bool = False,