tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.41k stars 266 forks source link

Passing reverse_lazy url to SpectacularRedocView errors. #1311

Open isik-kaplan opened 1 month ago

isik-kaplan commented 1 month ago

Describe the bug

Passing reverse_lazy url to SpectacularRedocView errors.

I've chased down the issue to drf_spectacular.plumbing.set_query_parameters which uses urllib.parse.urlparse which needs a string to work.

To Reproduce It would be most helpful to provide a small snippet to see how the bug was provoked.

-> scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(url)
(Pdb) from django.urls import reverse_lazy
(Pdb) url = reverse_lazy('rest:v0:api-root')  # Change the url name here to an actual url in the project
(Pdb) url
'/v0/'
(Pdb) type(url)
<class 'django.utils.functional.lazy.<locals>.__proxy__'>
(Pdb) urllib.parse.urlparse(str(url))
ParseResult(scheme='', netloc='', path='/v0/', params='', query='', fragment='')
(Pdb) urllib.parse.urlparse(url)
*** AttributeError: '__proxy__' object has no attribute 'decode'
(Pdb)

Expected behavior

If you change

scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(url)
# to
scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(str(url))
# forcing the evaluation of the lazy url 

it works as expected

isik-kaplan commented 1 month ago

I've attached a very tiny PR that fixes this issue.

autumn-ma commented 1 month ago

bump