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
Fixed #26431 -- Prevented django.urls.resolve() from returning missing optional parameters. #11477
Fixed #26431 -- Prevented django.urls.resolve() from returning missing optional parameters. #11477
Conversation
2aef44f
to
edb8df2
Compare
edb8df2
to
8195450
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MisterRios Thanks for this patch 👍 Tests in i18n
also pass if I remove empty kwargs
in _reverse_with_prefix()
, e.g.
kwargs = {key: value for key, value in kwargs.items() if value is not None}
Can you add extra tests to show that fix should be made in RegexPattern
?
Moreover tests added to the urlpatterns_reverse
pass without this fix that's why I moved them to a separate commit.
@felixxm Yes! I came up against this, but My first approach (which might be preferable) was to strip out kwargs set as None which are valid url kwargs: # Remove optional kwargs if a value is not declared
none_kwargs = {key for key, value in kwargs.items() if value is None}
available_kwargs = {kwarg for result, params in possibility for kwarg in params}
to_delete_kwargs = available_kwargs.intersection(none_kwargs)
kwargs = {key: value for key, value in kwargs.items() if key not in to_delete_kwargs} But I noticed that tests for non-provided optional arguments already passed without this patch, Those arguments never come to this stage.
Will do.
|
@MisterRios Thanks for explanation 👍 I think that current approach is good and I'm waiting for extra tests. |
@felixxm I wasn't sure exactly where to add them, but ended up with two tests that check the matches. The one |
… provided optional parameter.
…g optional parameters. Previous behavior was inconsistent with django.urls.reverse() and caused that translate_url() created an incorrect URL when an optional parameter was missing.
e768191
to
76b993a
Compare
@MisterRios Thanks! Welcome aboard 🚀 |
@felixxm Thanks! Hopefully the first of many! |
Will an update be provided for django 1.11 to fix the mentioned error? |
@nikolaysm No, Django 1.11 receives only security fixes. |
@felixxm Ok, thanks for your reply. |
…amed groups are missing in the URL pattern
Commit Message too long?
I had patched with the original patch provided by the reporter, but then dug a bit deeper. I then patched at
_reverse_with_prefix
which is where most of my previous work was situated. (Delete kwargs withNone
values that are valid parameters- using set magic). But the tests fornamed_optional
andnamed_optional_terminated
with just one parameter still went through, so I dug deeper.Usually these None-value parameters never make it to
_reverse_with_prefix
. The culprit, in the end, was django.urls.resolvers.RegexPattern.match. It user groupdict() which returns aNone
as a default value instead of nixing the parameter: https://docs.python.org/3.6/library/re.html#re.match.groupdicthttps://code.djangoproject.com/ticket/26431