[QUESTION] Url parameters in nested routers?
See original GitHub issueI’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:
- Created 4 years ago
- Reactions:10
- Comments:13 (4 by maintainers)
Top GitHub Comments
@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:
Please check with a recent version and let me know if you still have any problem.
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.
I’d be happy to close this now on the basis that the above works for me, but maybe others may have input.