django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[BUG] Awkward router behaviour when path is like '/{some_var}'

Open trankov opened this issue 1 year ago • 1 comments

If some @router... decorator has path parameter like a '/{variable}', then all the following routes in the same module became to call not their decorated functions, but this one.

After changing the pattern, f.e. '/{variable}/' or '/more/{variable}', the proper behaviour returns. Putting this route function to the bottom of a module leads to normal behavior.

An error raises at ninja/operation.py:120 as follows:

Traceback (most recent call last):
  File ".../.venv/lib/python3.12/site-packages/ninja/operation.py", line 120, in run
    result = self.view_func(request, **values)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Versions

  • Python version: [3.12.4]
  • Django version: [5.1b1]
  • Django-Ninja version: [1.2.2]
  • Pydantic version: [2.7.1]

trankov avatar Jul 30 '24 07:07 trankov

This is a Django behavior, not Django Ninja one. When multiple URL patterns match the request URL, it will always use the first one. So the correct way of defining this catch-all view is to put it at the bottom of the module, as you mentioned.

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.

Ref

Xdynix avatar Aug 04 '24 00:08 Xdynix