Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.3k
-
Describe the bug To Reproduce
fromfastapiimportAPIRoutersub_sub_router=APIRouter()sub_router=APIRouter()main_router=APIRouter()main_router.include_router(sub_router,prefix='/foo')sub_router.include_router(sub_sub_router)
@sub_sub_router.get('')deftest():return'bar'
Expected behavior Additional context
So for my usecase I have 2 alternative ways to work around this issue:
I'm perfectly fine with using one of these workarounds if I have to, but before I do I wanted to know if you see this behavior as a bug as well. I checked out pull request#415 to see if I could make a pull request that fixes this, but unfortunately I don't know how to reach the parent router in the code that was changed there. |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 10 comments 3 replies
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Here's a workaround that I think handles your issue pretty cleanly: @sub_sub_router.get("$")deftest():return'bar' Why this works: the path passed to the route decorator here is actually used to build a regex. Note: this will prevent the check that the route starts with a "/" when you are done cascading your inclusions, so you'd need to take responsibility for that. But that seems like a pretty reasonable compromise given the design you are trying to achieve. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 3👎 2
-
As of today, this does not work. You need to send a request to |
BetaWas this translation helpful?Give feedback.
All reactions
👍 5
-
@mmzeynalli Same here |
BetaWas this translation helpful?Give feedback.
All reactions
👍 4
-
@dmontagu thanks, that should fix the issue for me. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Thanks for the help here@dmontagu ! 👏 🙇 Thanks for reporting back and closing the issue@teuneboon 👍 |
BetaWas this translation helpful?Give feedback.
All reactions
-
@teuneboon@tiangolo@dmontagu the generated swagger shows /route$ instead of /route for this approach... and calls the route that is suffixed with the dollar sign when you click "try it out". Is there a way to avoid this? |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
@HansBrende I'm not sure I understand what you mean. If you still have problems please create a new issue with a simple self-contained example that shows how to reproduce the error, that way we can help you. |
BetaWas this translation helpful?Give feedback.
All reactions
-
@tiangolo I am referring to the approach suggested by dmontagu. It works, but the Open API (swagger) page displays the regex in the path and the "try it out" button calls the incorrect API path. Not sure what part of this you didn't understand, perhaps you can elaborate. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Hello there, I had the same issue as@HansBrende regarding the generated OpenAPI schemas (also mentioned as "swagger" in his comment). @dmontagu's answer - with I took the time to search for "OpenAPI" in the documentation and ended in theExtending OpenAPI chapter. I leave to you the lecture of the chapter but if we take the final example - as of today: fromfastapiimportFastAPIfromfastapi.openapi.utilsimportget_openapiapp=FastAPI()@app.get("/items/")asyncdefread_items():return [{"name":"Foo"}]defcustom_openapi():ifapp.openapi_schema:returnapp.openapi_schemaopenapi_schema=get_openapi(title="Custom title",version="2.5.0",description="This is a very custom OpenAPI schema",routes=app.routes, )openapi_schema["info"]["x-logo"]= {"url":"https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" }app.openapi_schema=openapi_schemareturnapp.openapi_schemaapp.openapi=custom_openapi A simple change of the defcustom_openapi():ifapp.openapi_schema:returnapp.openapi_schemaopenapi_schema=get_openapi(title="Custom title",version="2.5.0",description="This is a very custom OpenAPI schema",routes=app.routes, )openapi_schema["info"]["x-logo"]= {"url":"https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png" }## Change starts here.forpathin {xforxinapp.openapi_schema["paths"]ifx.endswith("$")}:app.openapi_schema["paths"][path[:-1]]=app.openapi_schema["paths"][path]delapp.openapi_schema["paths"][path]### Change ends here.app.openapi_schema=openapi_schemareturnapp.openapi_schema I suppose that this is the right way but@tiangolo may correct me here. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 2
-
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
BetaWas this translation helpful?Give feedback.
All reactions
-
@tiangolo I wrote under answer a year ago, so Im writing here again. The problem persists, and adding P. S. My version: 0.115.14 |
BetaWas this translation helpful?Give feedback.
All reactions
-
Code example in initial post is invalid. Simplified correct code example: fromfastapiimportAPIRouter,FastAPIrouter=APIRouter()@router.get("")deftest():return"bar"app=FastAPI()app.include_router(router) So, it's not about "double-nested" router, but just about router without prefix |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
In my case I have: fromfastapiimportAPIRouterfrom .action_portfolioimportrouterasaction_routerfrom .general_portfolioimportrouterasgeneral_routerfrom .review_portfolioimportrouterasreview_routerfrom .update_portfolioimportrouterasupdate_routerrouters= [update_router,action_router,review_router,general_router]router=APIRouter()forrinrouters:router.include_router(r,prefix='/portfolios') And in general router: router=APIRouter(prefix='',tags=['User Portfolios'])@router.get('',summary='Get portfolio list',)asyncdeffunc():pass Because of this, I had to write: fromfastapiimportAPIRouterfrom .action_portfolioimportrouterasaction_routerfrom .general_portfolioimportrouterasgeneral_routerfrom .review_portfolioimportrouterasreview_routerfrom .update_portfolioimportrouterasupdate_routerrouters= [update_router,action_router,review_router]# FIXME: As prefix and route cannot be both empty string, we had to do# such workaround. The solution mentioned in https://github.com/tiangolo/fastapi/discussions/8618# is not valid anymore, and I have added comment. We are waiting for an answerrouter=APIRouter()forrinrouters:router.include_router(r,prefix='/portfolios')router.include_router(general_router,prefix='') and router=APIRouter(prefix='/portfolios',tags=['User Portfolios'])@router.get('',summary='Get portfolio list',)asyncdeffunc():pass |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #510 on February 28, 2023 11:55.