vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.26k stars 431 forks source link

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

Open trankov opened 3 months ago

trankov commented 3 months ago

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

Xdynix commented 3 months ago

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