Skip to content

Commit

Permalink
Handle non-json native enum values (#7056)
Browse files Browse the repository at this point in the history
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
  • Loading branch information
adriangb and dmontagu committed Aug 16, 2023
1 parent d142a03 commit 0d4b77b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pydantic/json_schema.py
Expand Up @@ -709,6 +709,8 @@ def literal_schema(self, schema: core_schema.LiteralSchema) -> JsonSchemaValue:
The generated JSON schema.
"""
expected = [v.value if isinstance(v, Enum) else v for v in schema['expected']]
# jsonify the expected values
expected = [to_jsonable_python(v) for v in expected]

if len(expected) == 1:
return {'const': expected[0]}
Expand Down
13 changes: 13 additions & 0 deletions tests/test_json_schema.py
Expand Up @@ -5415,3 +5415,16 @@ class A(MyBaseModel):
'title': 'B',
'type': 'object',
}


def test_enum_complex_value() -> None:
"""https://github.com/pydantic/pydantic/issues/7045"""

class MyEnum(Enum):
foo = (1, 2)
bar = (2, 3)

ta = TypeAdapter(MyEnum)

# insert_assert(ta.json_schema())
assert ta.json_schema() == {'enum': [[1, 2], [2, 3]], 'title': 'MyEnum', 'type': 'array'}

0 comments on commit 0d4b77b

Please sign in to comment.