-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't set custom title for enum field #4188
Comments
I think this is expected behaviour since enums cannot be extended so I think Pydantic works around this by creating a new model including I could be wrong though. I'm trying to set the title for an enum myself, and the solution you suggested ( |
assuming this is the same issue (which it appears to be, at least at the symptom level), @samuelcolvin seems to be aware of it (#3896 (comment)); I don't believe there have been any updates on a fix, though :( |
The extra class ColorChoice(str, Enum):
"""
some description in detail
"""
RED = "RED"
BLUE = "BLUE" The first case without |
As @IterableTrucks notes, in OpenAPI<3.1 sibling keys next to However, you can modify the schema for the enum by the use of In v1: import json
from enum import Enum
from pydantic import BaseModel
class ColorChoice(str, Enum):
RED = "RED"
BLUE = "BLUE"
@classmethod
def __modify_schema__(cls, schema):
schema['title'] = 'ABC' # the title will be 'ABC'
return schema
class Shape(BaseModel):
color_choice: ColorChoice This also works in v2, but the import json
from enum import Enum
from pydantic import BaseModel
from pydantic.json_schema import GetJsonSchemaHandler
from pydantic_core import core_schema
class ColorChoice(str, Enum):
RED = "RED"
BLUE = "BLUE"
@classmethod
def __get_pydantic_json_schema__(cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler):
schema = handler(core_schema)
schema['title'] = 'Abc' # title will become 'Abc'
return schema
class Shape(BaseModel):
color_choice: ColorChoice
print(json.dumps(json.loads(Shape.schema_json()), indent=4))
"""
{
"type": "object",
"properties": {
"color_choice": {
"$ref": "#/$defs/ColorChoice"
}
},
"required": [
"color_choice"
],
"title": "Shape",
"$defs": {
"ColorChoice": {
"enum": [
"RED",
"BLUE"
],
"title": "Abc",
"type": "string"
}
}
}
""" We don't do this with If any of this behavior is a problem, please create a new issue to describe the problem, and we can consider modifying or providing more configurability of this behavior out of the box in v2's GenerateJsonSchema. |
Checks
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:I have an enum field, color_choice. I would like the schema to include the title 'Color Choice' (with a space).
First attempt:
If I try setting the title using Field, I get an extra
allOf
, and the title without the space is still included in the ref.I am hoping to just get:
I have found these somewhat similar issues:
#1748
#1749
#2592
#3896
Thanks in advance!
The text was updated successfully, but these errors were encountered: