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

Avoid duplicating user-defined enum property names #1168

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/avoid_duplicating_user_defined_enum_property_names.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
default: major
---

# Avoid duplicating user-defined enum property names

#1168 by @wileykestner

The generator will now create names for properties gauranteed to avoid colliding with the names of other existing, user-defined types.

Resolves #1167
5 changes: 5 additions & 0 deletions openapi_python_client/parser/properties/enum_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def build( # noqa: PLR0911
class_name = data.title or name
if parent_name:
class_name = f"{utils.pascal_case(parent_name)}{utils.pascal_case(class_name)}"
original_class_name = class_name
name_index = 0
while class_name in schemas.classes_by_name:
name_index += 1
class_name = f"{original_class_name}PropertyEnum_{name_index}"
class_info = Class.from_string(string=class_name, config=config)
values = EnumProperty.values_from_list(value_list, class_info)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ def build( # noqa: PLR0911
class_name = data.title or name
if parent_name:
class_name = f"{utils.pascal_case(parent_name)}{utils.pascal_case(class_name)}"
original_class_name = class_name
name_index = 0
while class_name in schemas.classes_by_name:
name_index += 1
class_name = f"{original_class_name}PropertyEnum_{name_index}"
class_info = Class.from_string(string=class_name, config=config)
values: set[str | int] = set(value_list)

Expand Down
28 changes: 28 additions & 0 deletions tests/test_parser/test_properties/test_enum_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ def test_conflict(config: Config, property_class: PropertyClass) -> None:
assert err.detail == "Found conflicting enums named Existing with incompatible values."


def test_avoids_false_conflict(config: Config, property_class: PropertyClass) -> None:
schemas = Schemas()

_, schemas = property_class.build(
data=oai.Schema(enum=["a"]), name="Friend", required=True, schemas=schemas, parent_name="", config=config
)
_, schemas = property_class.build(
data=oai.Schema(enum=["a", "b"]),
name="FriendShips",
required=True,
schemas=schemas,
parent_name="",
config=config,
)
prop, new_schemas = property_class.build(
data=oai.Schema(enum=["c"]),
name="ships",
required=True,
schemas=schemas,
parent_name="Friend",
config=config,
)

assert sorted([n for n in schemas.classes_by_name]) == ["Friend", "FriendShips"]
assert sorted([n for n in new_schemas.classes_by_name]) == ["Friend", "FriendShips", "FriendShipsPropertyEnum1"]
assert prop.name == "ships"


def test_bad_default_value(config: Config, property_class: PropertyClass) -> None:
data = oai.Schema(default="B", enum=["A"])
schemas = Schemas()
Expand Down
Loading