django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

OpenAPI schema invalid for re_path

Open clintonb opened this issue 5 years ago • 7 comments

Checklist

  • [x] I have verified that that issue exists against the master branch of Django REST framework.
  • [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [x] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [x] I have reduced the issue to the simplest possible case.
  • [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

  1. Create an app with urls.py like this:
urlpatterns = router.urls
urlpatterns += [
    # TODO Use some other ViewSet. The path is what is most important here.
    re_path(r'^email-hmac/(?P<key>[-:\w]+)', views.EmailHmacView.as_view(), name='email_hmac'),
]
  1. Render the OpenAPI schema.

Expected behavior

The schema should render the path as /api/v1/email-hmac/{key}:.

Actual behavior

The schema renders the path as /api/v1/email-hmac/(P{key}[-:\w]+):.

The rendered path is completely invalid. My current workaround is to ditch the re_path, and return a 404 from the view if the value does not match the regex.

clintonb avatar Apr 05 '20 00:04 clintonb

Hi @clintonb

I've tried to replicate this issue. I've written the following test; however, as it passes I must not be understanding your issue correctly. Are you in a position to help develop this test so it demonstrates the reported issue?

---
 tests/schemas/test_openapi.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py
index d483f3d4..2f8aa2bd 100644
--- a/tests/schemas/test_openapi.py
+++ b/tests/schemas/test_openapi.py
@@ -2,7 +2,7 @@ import uuid
 import warnings
 
 import pytest
-from django.conf.urls import url
+from django.conf.urls import re_path, url
 from django.test import RequestFactory, TestCase, override_settings
 from django.utils.translation import gettext_lazy as _
 
@@ -1110,3 +1110,15 @@ class TestGenerator(TestCase):
         schema = generator.get_schema(request=create_request('/'))
         assert 'components' not in schema
         assert 'content' not in schema['paths']['/example/']['delete']['responses']['204']
+
+    def test_schema_re_path(self):
+        router = routers.SimpleRouter()
+        router.register('account', views.ExampleGenericViewSet, basename="account")
+        urlpatterns = router.urls
+        urlpatterns += [
+            re_path(r'^email-hmac/(?P<key>[-:\w]+)', views.ExampleDetailView.as_view(), name='email_hmac'),
+        ]
+        generator = SchemaGenerator(patterns=urlpatterns)
+        schema = generator.get_schema()
+        schema_str = str(schema)
+        assert schema_str.count("email-hmac/{key}") == 1
-- 

Note: This patch is also is committed to this branch

smithdc1 avatar Sep 06 '20 16:09 smithdc1

A similar thing happens with this code

re_path(r'^domains/(?P<name>[^/]+)/rrsets/(?P<subname>[^/]*)\.\.\./(?P<type>[^/]+)/$', views.RRsetDetail.as_view(), name='rrset'),

which renders as domains/{name}/rrsets/{subname}\.\.\./{type}/ in the schema. The backslashes \ are incorrect.

Related: https://github.com/desec-io/desec-stack/issues/359#issuecomment-865365725

peterthomassen avatar Jun 29 '21 13:06 peterthomassen

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 28 '22 02:03 stale[bot]

@smithdc1 David, are you able to reproduce what I described in https://github.com/encode/django-rest-framework/issues/7255#issuecomment-870606086?

peterthomassen avatar Mar 28 '22 19:03 peterthomassen

Hi @peterthomassen apologies, it's unlikely I'm going to get time to look at this again any time soon.

smithdc1 avatar Mar 28 '22 19:03 smithdc1

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 31 '22 15:07 stale[bot]

I think this is stilt current.

peterthomassen avatar Jul 31 '22 15:07 peterthomassen