Fastapi: Change default value for pydantic custom property in swagger docs

Created on 17 Aug 2020  路  4Comments  路  Source: tiangolo/fastapi

I have built custom types to validate with pydantic in keeping with https://pydantic-docs.helpmanual.io/usage/types/#custom-data-types

I want to have the defaults for these types show up as something compatible in the swagger docs.
Below is an example of what I have currently.

  "info": {
    "date_of_birth": "string",
    "gender": "string",
    "weight": 0,
    "height": 0,
    "phone": "string"
}

for "phone" I have a custom type defined, the pydantic model is like this

class UserInfo(BaseModel):
    date_of_birth: Optional[str] = None
    gender: Optional[str] = None
    weight: Optional[int] = None
    height: Optional[int] = None
    phone: PhoneNumber
    address: Address

How do I change the default text "string" to something else in the docs? I could do this

    class Config:
        schema_extra = {
            "example": {
                            ....
            }
        }

In the model but that wouldn't change it in all cases, just here, and I don't need to overwrite all the defaults necessarily, just the phone one.

Thanks!

Most helpful comment

Works great, you're the best, thanks a lot. I've been somehow googling the wrong stuff for forever.
I wasn't even aware of Field from pydantic, I'll go look at the docs on it

All 4 comments

Is that what you want? Field from pydantic has the example field.

    phone: PhoneNumber = Field(..., example=<your number>)

Works great, you're the best, thanks a lot. I've been somehow googling the wrong stuff for forever.
I wasn't even aware of Field from pydantic, I'll go look at the docs on it

You're welcome. 馃槼

Thanks for the help here @Kludex ! :clap: :bow:

Thanks for reporting back and closing the issue @floriansaul :+1:

Was this page helpful?
0 / 5 - 0 ratings