Search code examples
enumsfastapiopenapipydanticopenapi-generator

FastAPI: How to generate a readable scheme from IntEnum in openapi.json with the correct keys, not digital ones?


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?🙏🏼


Solution

  • 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.