Description
I have a pydantic model with Enum and Union parameters, and while they both appear in Schema, in a post route they are not displayed in the Example edit section.
I hesitated to file a bug but I;d prefer to ask if that's something that's feasible first.
I was surprised and made a small exercice, take this model:
class DisplayMe(BaseModel):
my_bool: bool
my_float: float
my_str: str
my_int: int
my_dict: dict
# my_list: list
# my_tuple: tuple
# my_set: set
# my_List_str: List[str]
# my_tuple_str_int: Tuple[str, int]
# my_dict_str_int: Dict[str, int]
# my_union_str_int: Union[str, int]
# my_enum: Enum
my_emailstr: EmailStr
my_name_email: NameEmail
my_urlstr: UrlStr
my_dsn: DSN
my_bytes: bytes
my_decimal: Decimal
my_uuid1: UUID1
my_uuid3: UUID3
my_uuid4: UUID4
my_uuid5: UUID5
my_uuid: UUID
my_filepath: FilePath
my_directorypath: DirectoryPath
my_path: Path
my_datetime: datetime
my_date: date
add this route:
@router.post("/testypes")
async def types(tt: DisplayMe):
return tt
should you uncomment any of the pydantic types commented in the DisplayMe model above, then the Edit section is empty, there's nothing to edit.
But I can see them in schema, for instance on the below screen my_tuple is visible in the Schema:

Now if I put them commented they are in the Edit section:

question is : is it possible to have the commented pydantic types in the swagger edit section, I'm mostyl interested in Union and Enum in fact :)
Sorry for the delay.
In fact, some of those types are generating errors.
I'll check this more thoroughly as it seems there's a bug in Pydantic to solve (which I probably introduced there while implementing all the JSON Schema stuff).
If you are curious, I create the API with your code, then I check the raw JSON OpenAPI endpoint, then I copy it and paste it here: https://editor.swagger.io, there I can debug it and see all the errors. You can check that if you want to see what is the actual problem in the generated schemas.
I've pushed on my fork a branch that tries to help on this (https://github.com/euri10/fastapi/tree/display_pydantic_types)
I used 2 models, Working and Failing:
the test_openapi doesn't test anything, it's more to print the json schema to paste like you said on https://editor.swagger.io
I guess most errors come from upstream in pydantic schema generation, just hope this branch helps in figuring out what types may cause issues, pydantic schema is a rather impressive topic to jump in :boom: :scream_cat: , not sure I would not where to look at ! :octopus:
Yeah, Pydantic internals are quite heavy. It even uses undocumented Python features.
I'll check it there with your examples to see where I did something wrong.
I hit this as well, both for Unions and Enum.
Here is the simple reproduction for Enum:
from enum import Enum
from fastapi import FastAPI
from pydantic import BaseModel
class ModelName(Enum):
alexnet = "alexnet"
resnet = "resnet"
lenet = "lenet"
class Model(BaseModel):
size: int
name: ModelName
app = FastAPI()
@app.post("/")
def post(model: Model):
pass
And the docs miss the name field indeed:

str as well:class ModelName(str, Enum):
alexnet = "alexnet"
resnet = "resnet"
lenet = "lenet"
(This is how pydantic docs do it BTW). That immediately made me think that there is a problem with schema, but it's not - Model.schema_json() return the following:
{
"title": "Model",
"type": "object",
"properties": {
"size": {
"title": "Size",
"type": "integer"
},
"name": {
"title": "Name",
"enum": [
"alexnet",
"resnet",
"lenet"
]
}
},
"required": [
"size",
"name"
]
}
I just realized I replied in the PR but not here.
Let me copy some sections from there:
The way to use an enum would be like:
class MyEnum(Enum):
a = "foo"
b = "bar"
and then:
my_enum: MyEnum
@haizaar about enums with str, good point. That's an issue in Swagger UI itself: https://github.com/swagger-api/swagger-ui/issues/3761
But is good to know there's a workaround for the API docs. Maybe we could use it in the FastAPI docs examples.
On the other side, about Union, they show up in Swagger UI in the schema, but not in the example, that's also Swagger UI: https://github.com/swagger-api/swagger-ui/issues/3803
I just encountered this one today too with a Union 馃槃
Are there any workarounds for getting a Union[str, List[str]] to display in the docs?
Thanks so much
Fotis
After a little more digging, I can see that FastAPI is doing the right thing here (the OpenAPI JSOn generated is correct). The problem seems to be on swagger-ui's end. Some related issues are https://github.com/swagger-api/swagger-ui/issues/4819 and https://github.com/swagger-api/swagger-ui/issues/3803
I guess we'll need to wait for this to be fixed upstream 馃槃
Cheers
Fotis