Fix format_suffix_patterns behavior with Django 2 path() routes by axnsan12 · Pull Request #5691 · encode/django-rest-framework (original) (raw)

I think this is a bit hard to fix properly. As it is now, format_suffix_patterns takes an "allowed" argument which is a list of accepted formats that is converted to a regx like (?P<format>fmt1|fmt2|fmt3).

def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None): ...

As far as I can tell, the only way to enforce that in Django 2 path would be to register a custom path converter for all combinations of allowed that are needed, and use that format specifier in a path of the form blabla.<registered_format_name:format>. This could get a bit clumsy, since each registered converted would have to have a dinamically generated name, maybe something like 'drf_format_suffix_' + '_'.join(allowed).

Note: generating regex patterns from route patterns is not an option, because the new converters actually transform their captured value, i.e. a view registered at path('api/<int:id>/', view) would expect to receive a value of type int, unlike a function registered at url('api/(?P<id>[0-9]+)', view) which would expect a string containing only digits.