3

My IntEnum class:

class Role(IntEnum):
    client = 1
    manager = 2
    admin = 3

What I got in generated openapi.json:

{
  enum: [1, 2, 3],
  title: "Role",
  type: "integer"
}

What I need:

{
  enum: {
    client: 1,
    manager: 2,
    admin: 3
  },
  title: "Role",
  type: "integer"
}

Is it possible?🙏🏼

2
  • The names in an IntEnum are only used internally. Any reason you can't use a StrEnum? Commented Feb 3, 2024 at 12:47
  • I use this IntEnum as type for field in db and I wouldn't store roles and statuses as strings Commented Feb 4, 2024 at 16:32

2 Answers 2

0

Thanks everyone for your attention, but I found a solution:) I should to override __get_pydantic_json_schema__ method in all my classes where I need custom schema generation.

Sign up to request clarification or add additional context in comments.

Comments

0

You can make use of docstring on Enum Class. Fastapi will generate json schema included automatically

class Role(IntEnum):
    """
    1: client 
    2: manager
    3: admin
    """
    client = 1
    manager = 2
    admin = 3

This will generate, on openapi.json

{
...
"type":"object",
"description": 
""" 1: client 
    2: manager
    3: admin
"""
}

Comments

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.