Skip to content

Multi-level router can't use empty path (eg. get.("") ) #4664

@jordantshaw

Description

@jordantshaw

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# main.py

from fastapi import FastAPI, APIRouter
from .child import child_router

app = FastAPI()

router = APIRouter()
router.include_router(child_router, prefix='/foo')

app.include_router(router, prefix='/api/v1', include_in_schema=True)

# .child/__init__.py
from fastapi import APIRouter

from .grandchild import grandchild_router

child_router = APIRouter()
child_router.include_router(grandchild_router)

# ..grandchild/__init__.py
from fastapi import APIRouter

grandchild_router = APIRouter()

@grandchild_router.get('')
def test():
    return 'bar'

Description

I currently have something similar setup as the example code. main_router > child_router > grandchild_router. I am setting a prefix on the child router level (during the include_router call). When I set the prefix on the child level, I cannot use an empty path on the grandchild level router.

If I do, I got the following error:
Exception: Prefix and path cannot be both empty (path operation: test)

My file structure looks like this.

project
│   main.py (main_router)
└───child
│   │   __init__.py (child_router)
│   └───grandchild
│       │    __init__.py (grandchild_router)


### Operating System

macOS

### Operating System Details

_No response_

### FastAPI Version

0.75.0

### Python Version

Python 3.9.10

### Additional Context

_No response_

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions