Describe the bug
I have a post method with the body as pydantic object, with one attribute as Enum object; All validation works perfectly; However, two independent (?) issues occur:
dict(), but would be nice to have something that flattens the whole entity;complaint_type attribute does not show up on the swaggerTo Reproduce
example coe snippet:
class ComplaintType(Enum):
other = 'other'
commercial = "commercial"
house = "house"
park = "park"
residential = "residential"
street = "street"
vehicle = "vehicle"
class Complaint(BaseModel):
complaint_type:ComplaintType
timestamp:datetime = datetime.now()
lat:float
lon:float
description:str
Expected behavior
I expected
Environment:
Can you test whether this fixes it:
class Geoaccuracy(str, Enum):
unknown = "unknown"
latlng = "latlng"
place = "place"
city = "city"
country = "country"
This works for me and correctly shows in SwaggerDocs. In your case, you've specified type Enum but not which type for Enum (looks like its str, too).
thanks! that did the trick with the docs!.
I realize the other question is rather for Pydantic repo, but how should I automatically convert all nested enum values to corresponding simple values, instead? I see FastAPI does that, but wasn't able to track the exact code line
Thanks @Sieboldianus for your help!
@Casyfill it's actually an issue in Swagger UI, by adding the str, the generated JSON Schema also includes the type "string", and Swagger UI requires it to be able to display it.
It's actually a bug in Swagger UI, Pydantic and FastAPI are generating a valid OpenAPI schema.
But I should update the Enum examples in the docs.
Update, I just updated the docs, to use Enums that inherit from str: https://fastapi.tiangolo.com/tutorial/path-params/#working-with-python-enumerations
Thank you both! That makes a lot of sense - somehow I never used Enum before, so - good to know!
Thanks @Casyfill for reporting back and closing the issue! :cake:
Most helpful comment
Can you test whether this fixes it:
This works for me and correctly shows in SwaggerDocs. In your case, you've specified type
Enumbut not which type for Enum (looks like itsstr, too).