0

I have this Gender class which I am using for validation on a field:

class Gender(str, Enum):
    male = "m"
    female = "f"

I am getting a dropdown for this on my doc pages, but they are displaying m and f as the text node on the generated HTML select field.

<select class=""><option value="">--</option><option value="m">m</option><option value="f">f</option></select>

If I wish to get Male and Female on the HTML text but m and f for their corresponding value, how should I do it? I wish the HTML for swagger to be like this:

<select class=""><option value="">--</option><option value="m">Male</option><option value="f">Female</option></select>
Helen
  • 87,344
  • 17
  • 243
  • 314
  • This is not possible out-of-the-box and would require tweaking (possibly forking) FastAPI/Pydantic and Swagger UI. I suggest adding this info to the `description` of the relevant parameter/field instead. – Helen Mar 03 '23 at 12:53
  • 2
    The Swagger docs page is just an API documentation page, and while it's interactive, it's not a user interface. Having different values in the dropdown options and values would work in complete opposition to being API documentation, since the dropdown values won't reflect what value should be used when calling the API. If you want this kind of customization, you should build a proper UI. – M.O. Mar 03 '23 at 12:56
  • Having enum value descriptions should be possible when using [OpenAPI Specification 3.1](https://stackoverflow.com/a/66471626/113116), but neither Swagger UI nor Pydantic support this syntax/concept at this time, as far as I know. – Helen Mar 03 '23 at 12:56

0 Answers0