Skip to content

Support basic re_path #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions openapi_core/contrib/django/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def create(cls, request):
else:
route = cls.path_regex.sub(
r'{\1}', request.resolver_match.route)
# Delete start and end marker to allow concatenation.
if route[:1] == "^":
route = route[1:]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I failed to trigger this case into a test environment as there are side effects in testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@p1c2u Can you help me with it?

Copy link
Collaborator

@p1c2u p1c2u May 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just add ^ to re_path:
re_path('^test/test-regexp/$', lambda d: None)
?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to add ^/ but it didn't work. Stupid mistake. It should work now.

if route[-1:] == "$":
route = route[:-1]
path_pattern = '/' + route

path = request.resolver_match and request.resolver_match.kwargs or {}
Expand Down
28 changes: 27 additions & 1 deletion tests/integration/contrib/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def django_settings(self):
import django
from django.conf import settings
from django.contrib import admin
from django.urls import path
from django.urls import path, re_path

if settings.configured:
return
Expand All @@ -44,6 +44,7 @@ def django_settings(self):
django.setup()
settings.ROOT_URLCONF = (
path('admin/', admin.site.urls),
re_path('^test/test-regexp/$', lambda d: None)
)

@pytest.fixture
Expand Down Expand Up @@ -138,6 +139,31 @@ def test_url_rule(self, request_factory):
assert openapi_request.body == request.body
assert openapi_request.mimetype == request.content_type

def test_url_regexp_pattern(self, request_factory):
from django.urls import resolve
request = request_factory.get('/test/test-regexp/')
request.resolver_match = resolve('/test/test-regexp/')

openapi_request = DjangoOpenAPIRequest(request)

path = {}
query = {}
headers = {
'Cookie': '',
}
cookies = {}
assert openapi_request.parameters == RequestParameters(
path=path,
query=query,
header=headers,
cookie=cookies,
)
assert openapi_request.method == request.method.lower()
assert openapi_request.full_url_pattern == \
request._current_scheme_host + "/test/test-regexp/"
assert openapi_request.body == request.body
assert openapi_request.mimetype == request.content_type


class TestDjangoOpenAPIResponse(BaseTestDjango):

Expand Down