-1

I have an endpoint which doesn't return a BaseModel-based response, and returns the following structure:

Dict[EmailStr, MyEnum]

And these are defined / imported like so:

from pydantic import EmailStr
from enum import Enum

class MyEnum(Enum):
    FOO1 = "bar1"
    FOO2 = "bar2"

Unfortunately, redoc does not provide an accurate example:

{
  "property1": "bar1",
  "property2": "bar1"
}

I would like the keys to be something like "user@example.com", and the example values to use all of MyEnum's attributes.

Is this possible?

Fastapi: 0.101.0
Kludge
  • 2,653
  • 4
  • 20
  • 42
  • 1
    It might be possible to do thia using [Pydantic's `RootModel`](https://docs.pydantic.dev/latest/usage/models/#rootmodel-and-custom-root-types), but I'm not sure. Try it out. – M.O. Aug 10 '23 at 12:09
  • I'm trying it at the moment. I swear to god it wasn't part of any AI-based chat / google search response @M.O. – Kludge Aug 10 '23 at 13:56
  • For future references: using RootModel alone had no effect on how redoc generates the examples. However, subclassing it allowed me to add custom configuration (`model_config` in pydantic v2 and `class Config` in pydantic v1) and add my own custom examples. Good enough @M.O. https://fastapi.tiangolo.com/tutorial/schema-extra-example – Kludge Aug 10 '23 at 14:13
  • You might find [this](https://stackoverflow.com/a/74370808/17865804), as well as [this](https://stackoverflow.com/a/73401333/17865804) and [this](https://stackoverflow.com/a/76131490/17865804) helpful – Chris Aug 10 '23 at 15:33

0 Answers0