Allow path parameters to be specified at router level #369
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR proposes and implements the following.
Current behavior
Adding path params when including the router (e.g.
app.add_router("/item/{item_id}", router)
does not work as expected. The test client and the actual application routing logic cannot find the endpoint.New (proposed) behavior
Using
app.add_router("/item/{item_id}", router)
will allow the user to treatitem_id
as a path parameter if they so choose.This change should not break any existing behavior.
Example
With FastAPI, you can do something like this
Then you could do the following and expect output:
This change will allow a similar pattern in django-ninja path parameters when including the router in a parent router, e.g.:
This PR only allows this to work when using the
Path()
object to indicate that it's a path parameter. The change to make it auto detect these is less trivial because the router's "prefix" is not actually a property of theRouter,
but a property of its parent router, so reading the prefix path is difficult when resolving path parameters.