#35090: Enforce uniqueness on custom path converters
------------------------------+--------------------------------------
     Reporter:  Adam Johnson  |                    Owner:  nobody
         Type:  Bug           |                   Status:  new
    Component:  Core (URLs)   |                  Version:  dev
     Severity:  Normal        |               Resolution:
     Keywords:                |             Triage Stage:  Unreviewed
    Has patch:  0             |      Needs documentation:  0
  Needs tests:  0             |  Patch needs improvement:  0
Easy pickings:  0             |                    UI/UX:  0
------------------------------+--------------------------------------
Description changed by Adam Johnson:

Old description:

> `register_converter()` allows
> [https://docs.djangoproject.com/en/5.0/topics/http/urls/#registering-
> custom-path-converters custom path converters] to silently replace
> existing converters. This could lead to surprising behaviour, both when
> two places accidentally use the same custom name or when replacing a
> default converter. This could be particularly hard to debug if a third-
> party package uses a custom converter.
>
> For example, say two modules register path converters named “year”. The
> first allows 1-4 digits:
>
> {{{
> class YearConverter:
>     regex = r"[0-9]{1,4}"
>     ...
>
> register_converter(YearConverter, "year")
> }}}
>
> Whilst the second requires exactly four digits:
>
> {{{
> class YearConverter:
>     regex = r"[0-9]{4}"
>     ...
>
> register_converter(YearConverter, "year")
> }}}
>
> Whichever module is loaded last will silently overwrite the other’s
> converter. URLs will then only be interpreted with that converter,
> leading to fewer URLs being matched than intended. This can be
> particularly difficult to spot as import order may change accidentally
> due to other code being rearranged.

New description:

 `register_converter()` allows
 [https://docs.djangoproject.com/en/5.0/topics/http/urls/#registering-
 custom-path-converters custom path converters] to silently replace
 existing converters. This could lead to surprising behaviour, both when
 two places accidentally use the same custom name or when replacing a
 default converter. This could be particularly hard to debug if a third-
 party package uses a custom converter.

 For example, say two modules register path converters named “year”. The
 first allows 1-4 digits:

 {{{
 class YearConverter:
     regex = r"[0-9]{1,4}"
     ...

 register_converter(YearConverter, "year")
 }}}

 Whilst the second requires exactly four digits:

 {{{
 class YearConverter:
     regex = r"[0-9]{4}"
     ...

 register_converter(YearConverter, "year")
 }}}

 Whichever module is loaded last will silently overwrite the other’s
 converter. URLs will then only be interpreted with that converter, leading
 to fewer URLs being matched than intended. This can be particularly
 difficult to spot as import order may change accidentally due to other
 code being rearranged.

 See the full example project at https://github.com/adamchainz/django-
 ticket-35090

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/35090#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018cdbb1ca05-a6363e25-f06d-4e0d-a85f-ee120591fcd4-000000%40eu-central-1.amazonses.com.

Reply via email to