Search code examples
pythonswaggerfastapiswagger-uipydantic

Swagger API : should not accept Null


I have added the following to the swagger definition for the api.

value : BooleanEnum = Query ( False, alias="value")

class BooleanEnum(str, Enum):
    true = "true"
    false = "false"
    @classmethod
    def _missing_(cls, value):
        return cls.__members__.get(value.lower(), None)

The Swagger docs is still accepting a null value and showing it as one of the options of selection. Though, the default is set to False.

How can I limit the selection to just True and False, so the user is mandated to select one of the option and provide no choice to accept a Null response.


Solution

  • It is a common behaviour of FastAPI because you set a default value for a query parameter. Therefore, your API accepts three values true, false and None which is replaced by false. If you remove the default value it and put instead Query(..., alias="value") it will make a user to choose of one of the two enum values and won't allow None value to be sent.