Impossible to set examples for enums? #7401
-
ExampleHere's a self-contained, minimal, reproducible, example with my use case: from enum import Enum
from fastapi import FastAPI, Query
app = FastAPI()
class SomeValue(str, Enum):
a = 'a'
b = 'b'
@app.get("/")
def read_root(query_value: SomeValue = Query(..., example='b')):
# Doesnt matter what happens here
return {"Hello": "World"}DescriptionI set an example for Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
There are imports missing on the minimal example 😴 |
Beta Was this translation helpful? Give feedback.
-
|
I'm sorry for the missing imports, I edited it. We have a guideline that every parameters needs an example and our testsuit has a test where it explicitly tests each endpoint with its examples (which it gets from the created /openapi.json). I understand that for enums all possible values are already present but I don't understand why examples for enums are removed. Before we where using connexion where you have to write the openapi spec yourself in a yaml, where setting an example for a enum was no problem. see also the first example for 'Parameter Examples' in 'Adding Examples' of the swagger docu: https://swagger.io/docs/specification/adding-examples/#Parameter_Examples |
Beta Was this translation helpful? Give feedback.
-
|
found a workaround from enum import Enum
from fastapi import FastAPI, Query
app = FastAPI()
class SomeValue(str, Enum):
a = 'a'
b = 'b'
@classmethod
def __modify_schema__(cls, field_schema):
# __modify_schema__ should mutate the dict it receives in place,
# the returned value will be ignored
field_schema.update(
example='b',
)
@app.get("/")
def read_root(query_value: SomeValue):
# Doesnt matter what happens here
return {"Hello": "World"} |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @Kludex ! 👏 🙇 Thanks for reporting back and closing the issue 👍 |
Beta Was this translation helpful? Give feedback.

found a workaround