Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

sujin502
Copy link

@sujin502 sujin502 commented Sep 1, 2025

During the use of FastAPI, I noticed a problem with sub-routes, My project structure is as follows:

┌─example
│ ├─main.py
│ └─sub_dir
│   ├─__init__.py
│   └─static
│     └─index.html

The code is as follows:

# example/main.py
from fastapi import FastAPI

from sub_dir import sub_dir

app = FastAPI()
app.include_router(sub_dir, prefix="/app")

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="127.0.0.1", port=8000)
# example/sub_dir/__init__.py
from pathlib import Path as libPath

from fastapi import APIRouter
from fastapi.staticfiles import StaticFiles

skip = APIRouter()
skip.mount("/static", StaticFiles(directory=libPath(__file__).resolve().parent / "static"), name="skip")

__all__ = ["skip"]

After running main.py and accessing http://127.0.0.1:8000/app/sub_dir/index.html, one should have seen the content of example/sub_dir/static/index.html. However, it did not display as expected, Log entry as follows:

INFO:     Started server process [60576]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:59030 - "GET /app/sub_dir/index.html HTTP/1.1" 404 Not Found

By examining the source code, it was discovered that APIRouter.include_router failed to properly handle routing.Mount.

After the repair, we ran main.py, and we could see that the content in example/sub_dir/static/index.html could be displayed normally. The log is as follows:

INFO:     Started server process [78196]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:58825 - "GET /app/sub_dir/index.html HTTP/1.1" 200 OK

This will be extremely useful in complex routing scenarios!!!

@sujin502 sujin502 marked this pull request as draft September 1, 2025 12:49
@sujin502 sujin502 closed this Sep 1, 2025
@sujin502 sujin502 reopened this Sep 1, 2025
@sujin502 sujin502 marked this pull request as ready for review September 1, 2025 12:56
@svlandeg
Copy link
Member

svlandeg commented Sep 3, 2025

Hi @sujin502, thanks for your contribution and detailed explanation!

Can you design a minimal unit test that would fail on master but would succeed on this PR? Then it will be 100% clear that this PR fixes that use-case, and it will also ensure that the test suite coverage remains at 100% (the CI will be red otherwise).

I'll put this PR in draft until the CI goes green.

@svlandeg svlandeg added the bug Something isn't working label Sep 3, 2025
@svlandeg svlandeg changed the title Fix the issue where ASGIApp is not correctly mounted to the sub-routes. 🐛 Ensure that ASGIApp is mounted correctly to the sub-routes Sep 3, 2025
@svlandeg svlandeg marked this pull request as draft September 3, 2025 13:10
@sujin502 sujin502 marked this pull request as ready for review September 9, 2025 11:58
@sujin502
Copy link
Author

sujin502 commented Sep 9, 2025

I'm very sorry. Due to my busy work schedule, I didn't notice this until now. The unit test code has been completed. There are absolutely no errors now!

Finally, I would like to express my deepest gratitude to you and all the contributors for your efforts. It is truly wonderful.

@github-actions github-actions bot removed the waiting label Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants