Skip to content

Commit

Permalink
bugfix incomplete regex stripping for literal dots #507
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Sep 7, 2021
1 parent b722d5e commit 508f4e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions drf_spectacular/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def get_api_endpoints(self, patterns=None, prefix=''):
else:
return api_endpoints

def get_path_from_regex(self, path_regex):
path = super().get_path_from_regex(path_regex)
# bugfix oversight in DRF regex stripping
path = path.replace('\\.', '.')
return path

def _get_api_endpoints(self, patterns, prefix):
"""
Return a list of all available API endpoints by inspecting the URL conf.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,3 +2133,22 @@ class XViewset(viewsets.ModelViewSet):
assert schema['components']['schemas']['X']['properties']['field'] == {
'type': 'integer', 'default': 3
}


def test_literal_dot_in_regex_path(no_warnings):
@extend_schema(
responses=OpenApiTypes.ANY,
parameters=[
OpenApiParameter('filename', str, OpenApiParameter.PATH),
OpenApiParameter('ext', str, OpenApiParameter.PATH)
]
)
@api_view(['GET'])
def view_func(request, format=None):
pass # pragma: no cover

urlpatterns = [
re_path('^file/(?P<filename>.*)\\.(?P<ext>\\w+)$', view_func)
]
schema = generate_schema(None, patterns=urlpatterns)
assert '/file/{filename}.{ext}' in schema['paths']

0 comments on commit 508f4e5

Please sign in to comment.