Skip to content

Commit a73aa6b

Browse files
committed
Adjust insert_into to raise ValueError
1 parent fa87a95 commit a73aa6b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

rest_framework/schemas/generators.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ def is_api_view(callback):
5151
return (cls is not None) and issubclass(cls, APIView)
5252

5353

54+
INSERT_INTO_COLLISION_FMT = """
55+
Schema Naming Collision.
56+
57+
coreapi.Link for URL path {value_url} cannot be inserted into schema.
58+
Position conflicts with coreapi.Link for URL path {target_url}.
59+
60+
Attemped to insert link with keys: {keys}.
61+
62+
Adjust URLs to avoid naming collision or override `SchemaGenerator.get_keys()`
63+
to customise schema structure.
64+
"""
65+
66+
5467
def insert_into(target, keys, value):
5568
"""
5669
Nested dictionary insertion.
@@ -64,7 +77,15 @@ def insert_into(target, keys, value):
6477
if key not in target:
6578
target[key] = {}
6679
target = target[key]
67-
target[keys[-1]] = value
80+
try:
81+
target[keys[-1]] = value
82+
except TypeError:
83+
msg = INSERT_INTO_COLLISION_FMT.format(
84+
value_url=value.url,
85+
target_url=target.url,
86+
keys=keys
87+
)
88+
raise ValueError(msg)
6889

6990

7091
def is_custom_action(action):

tests/test_schemas.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,9 @@ def test_manually_routing_nested_routes(self):
778778
]
779779

780780
generator = SchemaGenerator(title='Naming Colisions', patterns=patterns)
781-
schema = generator.get_schema()
781+
782+
with pytest.raises(ValueError):
783+
generator.get_schema()
782784

783785
def test_manually_routing_generic_view(self):
784786
patterns = [
@@ -793,12 +795,16 @@ def test_manually_routing_generic_view(self):
793795
]
794796

795797
generator = SchemaGenerator(title='Naming Colisions', patterns=patterns)
796-
schema = generator.get_schema()
798+
799+
with pytest.raises(ValueError):
800+
generator.get_schema()
797801

798802
def test_from_router(self):
799803
patterns = [
800804
url(r'from-router', include(naming_collisions_router.urls)),
801805
]
802806

803807
generator = SchemaGenerator(title='Naming Colisions', patterns=patterns)
804-
schema = generator.get_schema()
808+
809+
with pytest.raises(ValueError):
810+
generator.get_schema()

0 commit comments

Comments
 (0)