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

Fixed #26431 -- Prevented django.urls.resolve() from returning missing optional parameters. #11477

Conversation

MisterRios
Copy link
Contributor

…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 with None values that are valid parameters- using set magic). But the tests for named_optional and named_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 a None as a default value instead of nixing the parameter: https://docs.python.org/3.6/library/re.html#re.match.groupdict

https://code.djangoproject.com/ticket/26431

@MisterRios MisterRios force-pushed the 26431/incorrect_urls_from_missing_optional_kwargs branch 2 times, most recently from 2aef44f to edb8df2 Compare June 15, 2019 04:32
@felixxm felixxm self-assigned this Jun 19, 2019
@felixxm felixxm force-pushed the 26431/incorrect_urls_from_missing_optional_kwargs branch from edb8df2 to 8195450 Compare June 19, 2019 09:32
Copy link
Member

@felixxm felixxm left a 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 felixxm changed the title Fixed #26431:translate_url() creates an incorrect URL when optional n… Fixed #26431 -- Prevented translate_url() from creating incorrect URLs when optional parameter is missing. Jun 19, 2019
@MisterRios
Copy link
Contributor Author

@MisterRios Thanks for this patch +1 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}

@felixxm Yes! I came up against this, but urlpatterns_reverse.tests.ReverseShortcutTests.test_redirect_view_object fails because the kwarg wrong_argument=None doesn't get stripped out, and doesn't raise a NoReverseMatch Error.

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.

Can you add extra tests to show that fix should be made in RegexPattern?

Will do.

Moreover tests added to the urlpatterns_reverse pass without this fix that's why I moved them to a separate commit.
Thanks.

@felixxm
Copy link
Member

felixxm commented Jun 21, 2019

@MisterRios Thanks for explanation 👍 I think that current approach is good and I'm waiting for extra tests.

@MisterRios
Copy link
Contributor Author

@felixxm I wasn't sure exactly where to add them, but ended up with two tests that check the matches. The one test_re_path_with_optional_parameter_not_provided fails without the patch.

…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.
@felixxm felixxm force-pushed the 26431/incorrect_urls_from_missing_optional_kwargs branch from e768191 to 76b993a Compare June 24, 2019 09:49
@felixxm felixxm changed the title Fixed #26431 -- Prevented translate_url() from creating incorrect URLs when optional parameter is missing. Fixed #26431 -- Prevented django.urls.resolve() from returning missing optional parameters. Jun 24, 2019
@felixxm felixxm merged commit 76b993a into django:master Jun 24, 2019
@felixxm
Copy link
Member

felixxm commented Jun 24, 2019

@MisterRios Thanks! Welcome aboard 🚀

@MisterRios
Copy link
Contributor Author

@felixxm Thanks! Hopefully the first of many!

@nikolaysm
Copy link

Will an update be provided for django 1.11 to fix the mentioned error?

@felixxm
Copy link
Member

felixxm commented Sep 27, 2019

@nikolaysm No, Django 1.11 receives only security fixes.

@nikolaysm
Copy link

nikolaysm commented Sep 27, 2019

@nikolaysm No, Django 1.11 receives only security fixes.

@felixxm Ok, thanks for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants