question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Url parameters in nested routers?

See original GitHub issue

I’m developing a public api where we’ll probably want to nest routes at least a couple of levels deep, using the initial route to determine the ‘app’ that the rest of the content belongs to.

our target url structure is along the lines of

/api/v1/apps/{shortcode} /api/v1/apps/{shortcode}/people/ /api/v1/apps/{shortcode}/people/{id}

etc. etc…

I’d really like to take advantage of the APIRouter classes to do this as we’re likely to have a lot of endpoints and models but I can’t find a way to ensure the ‘shortcode’ parameter here is passed to child routers, is there any way to do that?

currently I’m doing stuff like this:

app.py

from app.people.routes import router as people_router
from app.routes import router as main_router

app = FastAPI()
app.debug = settings.DEBUG

main_router.include_router(people_router, prefix='/{shortcode}/people')
app.include_router(main_router, prefix='/apps')

This works ok, but the {shortcode} parameter is not included in the auto-generated documentation ( though it is available in the view functions ). I’d rather not have to define every view function individually with prefixed like @router.route('/{shortcode}/people/{id}) ... if I can avoid it somehow?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
tiangolocommented, Jun 28, 2019

@bharling I just added tests for this specific use case in #349, I see it working as expected.

And I see it documented on the API docs:

Selection_066

Please check with a recent version and let me know if you still have any problem.

6reactions
bharlingcommented, Jul 2, 2019

UPDATE:

Following the pattern below works well though however. Doing the below is fine for my purposes definitely. I think my initial issue was because of nesting tagged routes which may actually be a misuse of the openapi spec perhaps anyhow.

example_router = APIRouter()

@example_router.get('/example')
def example_get(shortcode:str):
    return {'shortcode': shortcode }

@example_router.get('/example/{id}')
def example_detail(shortcode:str, id:int):
    return {'shortcode': shortcode, 'id': id}


v1_router = APIRouter()
v1_router.include_router(auth_router, tags=['Authentication'])
v1_router.include_router(app_router, tags=['Apps'])
v1_router.include_router(images_router, tags=['Images'], prefix='/{shortcode}')
v1_router.include_router(people_router, tags=['People'], prefix='/{shortcode}')
v1_router.include_router(example_router, tags=['Example'], prefix='/{shortcode}')

app.include_router(v1_router, prefix='/v1/apps')

I’d be happy to close this now on the basis that the above works for me, but maybe others may have input.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Routing with url param is not working in react-router-dom
0.1), but it (route /primary/:id ) is not working whenever I try and access one of the nested routes. Nested route looks like.....
Read more >
The Guide to Nested Routes with React Router - ui.dev
In this comprehensive, up-to-date guide, you'll learn everything you need to know about creating nested routes with React Router.
Read more >
React Router v6 - Protected Routes, Nested ... - YouTube
React Router allows you to make routers in the react application with ... Protected Router, Nested Router, Active Link, Search Parameter, ...
Read more >
React Router Cheatsheet | Codecademy
Query parameters appear in URLs beginning with a question mark ( ? ) and are followed by a parameter name assigned to a...
Read more >
Nested Routes • Angular - codecraft.tv
We can nest routes, this means that for a given URL we can render a tree of ... Relative Routes; Child Routes; Parent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found