Skip to content

Commit 4bb33bb

Browse files
adamchainzfelixxm
authored andcommitted
Fixed #31459 -- Fixed handling invalid indentifiers in URL path conversion.
This patch adjusted existing tests that used invalid identifiers.
1 parent 578c03b commit 4bb33bb

File tree

5 files changed

+13
-5
lines changed

5 files changed

+13
-5
lines changed

django/urls/resolvers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def __str__(self):
197197

198198

199199
_PATH_PARAMETER_COMPONENT_RE = _lazy_re_compile(
200-
r'<(?:(?P<converter>[^>:]+):)?(?P<parameter>\w+)>'
200+
r'<(?:(?P<converter>[^>:]+):)?(?P<parameter>[^>]+)>'
201201
)
202202

203203

tests/check_framework/test_urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_contains_re_named_group(self):
143143
self.assertEqual(len(result), 1)
144144
warning = result[0]
145145
self.assertEqual(warning.id, '2_0.W001')
146-
expected_msg = "Your URL pattern '(?P<named-group>\\d+)' has a route"
146+
expected_msg = "Your URL pattern '(?P<named_group>\\d+)' has a route"
147147
self.assertIn(expected_msg, warning.msg)
148148

149149
@override_settings(ROOT_URLCONF='check_framework.urls.path_compatibility.beginning_with_caret')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.urls import path
22

33
urlpatterns = [
4-
path(r'(?P<named-group>\d+)', lambda x: x),
4+
path(r'(?P<named_group>\d+)', lambda x: x),
55
]

tests/gis_tests/geoapp/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
gis_sitemap_views.kml,
2121
name='django.contrib.gis.sitemaps.views.kml'),
2222
path(
23-
'sitemaps/kml/<label>/<<model>/<field_name>.kmz',
23+
'sitemaps/kml/<label>/<model>/<field_name>.kmz',
2424
gis_sitemap_views.kmz,
2525
name='django.contrib.gis.sitemaps.views.kmz'),
2626
]

tests/urlpatterns/tests.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,22 @@ def requires_tiny_int(value):
249249

250250

251251
class ParameterRestrictionTests(SimpleTestCase):
252-
def test_non_identifier_parameter_name_causes_exception(self):
252+
def test_integer_parameter_name_causes_exception(self):
253253
msg = (
254254
"URL route 'hello/<int:1>/' uses parameter name '1' which isn't "
255255
"a valid Python identifier."
256256
)
257257
with self.assertRaisesMessage(ImproperlyConfigured, msg):
258258
path(r'hello/<int:1>/', lambda r: None)
259259

260+
def test_non_identifier_parameter_name_causes_exception(self):
261+
msg = (
262+
"URL route 'b/<int:book.id>/' uses parameter name 'book.id' which "
263+
"isn't a valid Python identifier."
264+
)
265+
with self.assertRaisesMessage(ImproperlyConfigured, msg):
266+
path(r'b/<int:book.id>/', lambda r: None)
267+
260268
def test_allows_non_ascii_but_valid_identifiers(self):
261269
# \u0394 is "GREEK CAPITAL LETTER DELTA", a valid identifier.
262270
p = path('hello/<str:\u0394>/', lambda r: None)

0 commit comments

Comments
 (0)