Skip to content
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

fix(schema): fix schema generation with multiple Enums having the same name #2226

Merged
merged 1 commit into from Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1857-PrettyWood.md
@@ -0,0 +1 @@
fix schema generation with multiple Enums having the same name
2 changes: 1 addition & 1 deletion pydantic/schema.py
Expand Up @@ -781,7 +781,7 @@ def field_singleton_schema( # noqa: C901 (ignore complexity)
f_schema['const'] = literal_value

if lenient_issubclass(field_type, Enum):
enum_name = normalize_name(field_type.__name__)
enum_name = model_name_map[field_type]
f_schema, schema_overrides = get_field_info_schema(field)
f_schema.update(get_schema_ref(enum_name, ref_prefix, ref_template, schema_overrides))
definitions[enum_name] = enum_process_schema(field_type)
Expand Down
52 changes: 52 additions & 0 deletions tests/test_schema.py
Expand Up @@ -2097,3 +2097,55 @@ class Model(BaseModel):
'properties': {'a': {'title': 'A', 'type': 'string'}},
'required': ['a'],
}


def test_multiple_enums_with_same_name(create_module):
module_1 = create_module(
# language=Python
"""
from enum import Enum

from pydantic import BaseModel


class MyEnum(str, Enum):
a = 'a'
b = 'b'
c = 'c'


class MyModel(BaseModel):
my_enum_1: MyEnum
"""
)

module_2 = create_module(
# language=Python
"""
from enum import Enum

from pydantic import BaseModel


class MyEnum(str, Enum):
d = 'd'
e = 'e'
f = 'f'


class MyModel(BaseModel):
my_enum_2: MyEnum
"""
)

class Model(BaseModel):
my_model_1: module_1.MyModel
my_model_2: module_2.MyModel

assert len(Model.schema()['definitions']) == 4
assert set(Model.schema()['definitions']) == {
f'{module_1.__name__}__MyEnum',
f'{module_1.__name__}__MyModel',
f'{module_2.__name__}__MyEnum',
f'{module_2.__name__}__MyModel',
}