Skip to content

Commit 10aa34a

Browse files
Pr1312 (#1339)
* Force evaluation of reverse_lazy urls in set_query_parameters. Fixes #1311 Passing reverse_lazy url to SpectacularRedocView errors because set_query_parameters calls urlls.parse.urlparse on url which may not be a string but a 'django.utils.functional.lazy.<locals>.__proxy__'. Forcing url to be string fixes this issue. * Update plumbing.py * fix second lazy url issue & add test #1312 --------- Co-authored-by: Işık Kaplan <[email protected]>
1 parent caf707d commit 10aa34a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

drf_spectacular/plumbing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,7 @@ def build_mock_request(method, path, view, original_request, **kwargs):
12831283

12841284
def set_query_parameters(url, **kwargs) -> str:
12851285
""" deconstruct url, safely attach query parameters in kwargs, and serialize again """
1286+
url = str(url) # Force evaluation of reverse_lazy urls
12861287
scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(url)
12871288
query = urllib.parse.parse_qs(query)
12881289
query.update({k: v for k, v in kwargs.items() if v is not None})
@@ -1291,6 +1292,7 @@ def set_query_parameters(url, **kwargs) -> str:
12911292

12921293

12931294
def get_relative_url(url: str) -> str:
1295+
url = str(url) # Force evaluation of reverse_lazy urls
12941296
scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(url)
12951297
return urllib.parse.urlunparse(('', '', path, params, query, fragment))
12961298

tests/test_plumbing.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
from django.conf.urls import include
1717
from django.db import models
1818
from django.urls import re_path
19+
from django.utils.functional import lazystr
1920
from rest_framework import generics, serializers
2021

2122
from drf_spectacular.openapi import AutoSchema
2223
from drf_spectacular.plumbing import (
2324
analyze_named_regex_pattern, build_basic_type, build_choice_field, detype_pattern,
24-
follow_field_source, force_instance, get_list_serializer, is_field, is_serializer,
25-
resolve_type_hint, safe_ref,
25+
follow_field_source, force_instance, get_list_serializer, get_relative_url, is_field,
26+
is_serializer, resolve_type_hint, safe_ref, set_query_parameters,
2627
)
2728
from drf_spectacular.validation import validate_schema
2829
from tests import generate_schema
@@ -437,3 +438,13 @@ def test_safe_ref():
437438
schema = safe_ref(schema)
438439
assert schema == {'$ref': '#/components/schemas/Foo'}
439440
assert safe_ref(schema) == safe_ref(schema)
441+
442+
443+
def test_url_tooling_with_lazy_url():
444+
some_url = "http://api.example.org/accounts/"
445+
446+
assert get_relative_url(some_url) == "/accounts/"
447+
assert set_query_parameters(some_url, foo=123) == some_url + "?foo=123"
448+
449+
assert get_relative_url(lazystr(some_url)) == "/accounts/"
450+
assert set_query_parameters(lazystr(some_url), foo=123) == some_url + "?foo=123"

0 commit comments

Comments
 (0)