Skip to content
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

Fix PATH_PARAMETER_PATTERN for DRF default value pattern. #468

Merged

Conversation

ngnpope
Copy link
Contributor

@ngnpope ngnpope commented Jan 19, 2023

Django REST Framework will use [^/.]+ as the default value pattern in certain cases, e.g. (?P<pk>[^/.]+), which doesn't work well with the current regular expression in PATH_PARAMETER_PATTERN.

❯ git describe; git rev-parse HEAD
3.14.0-69-g0618fa88
0618fa88e1a8c2cf8a2aab29ef6de66b49e5f7ed

❯ git grep -n '\[^/.\]+'
rest_framework/routers.py:143:            self._default_value_pattern = '[^/.]+'
tests/test_routers.py:304:        expected = ['^notes/$', '^notes/(?P<pk>[^/.]+)/$']
tests/test_routers.py:319:        expected = ['^notes$', '^notes/(?P<pk>[^/.]+)$']

The fix inserts (?:[^/]*?\[\^[^/]*/)? before the final [^/]* in the pattern. This allows matching a slash inside an inverted character set, i.e. [^...], treating it as part of the named group.

Django REST Framework will use `[^/.]+` as the default value pattern in
certain cases, e.g. `(?P<pk>[^/.]+)`, which doesn't work well with the
current regular expression in `PATH_PARAMETER_PATTERN`.

```
❯ git describe; git rev-parse HEAD
3.14.0-69-g0618fa88
0618fa88e1a8c2cf8a2aab29ef6de66b49e5f7ed

❯ git grep -n '\[^/.\]+'
rest_framework/routers.py:143:            self._default_value_pattern = '[^/.]+'
tests/test_routers.py:304:        expected = ['^notes/$', '^notes/(?P<pk>[^/.]+)/$']
tests/test_routers.py:319:        expected = ['^notes$', '^notes/(?P<pk>[^/.]+)$']
```

The fix inserts `(?:[^/]*?\[\^[^/]*/)?` before the final `[^/]*` in the
pattern. This allows matching a slash inside an inverted character set,
i.e. `[^...]`, treating it as part of the named group.
@codecov
Copy link

codecov bot commented Jan 20, 2023

Codecov Report

Merging #468 (3cb7cd9) into master (eefb1d6) will increase coverage by 0.15%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #468      +/-   ##
==========================================
+ Coverage   93.15%   93.30%   +0.15%     
==========================================
  Files         103      103              
  Lines        2630     2630              
  Branches      229      229              
==========================================
+ Hits         2450     2454       +4     
+ Misses        138      136       -2     
+ Partials       42       40       -2     
Impacted Files Coverage Δ
openapi_core/contrib/django/requests.py 95.45% <100.00%> (+9.09%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Collaborator

@p1c2u p1c2u left a comment

Choose a reason for hiding this comment

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

The pattern needs reformatting but I can handle it.

@p1c2u
Copy link
Collaborator

p1c2u commented Jan 20, 2023

@ngnpope thank you for the contribution

@p1c2u p1c2u merged commit af6f24a into python-openapi:master Jan 20, 2023
@ngnpope ngnpope deleted the fix/django-path-parameter-pattern branch January 20, 2023 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants