1

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>
3
  • 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. Commented Mar 3, 2023 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. Commented Mar 3, 2023 at 12:56
  • Having enum value descriptions should be possible when using OpenAPI Specification 3.1, but neither Swagger UI nor Pydantic support this syntax/concept at this time, as far as I know. Commented Mar 3, 2023 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.