Challenge - 5 Problems
Router Prefix and Tags Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the full path of the endpoint?
Given this FastAPI router setup, what is the full URL path to access the
read_items endpoint?from fastapi import APIRouter
router = APIRouter(prefix="/items", tags=["items"])
@router.get("/")
async def read_items():
return {"msg": "List of items"}FastAPI
from fastapi import APIRouter router = APIRouter(prefix="/items", tags=["items"]) @router.get("/") async def read_items(): return {"msg": "List of items"}
Attempts:
2 left
💡 Hint
Remember the prefix adds before the route path.
✗ Incorrect
The router has a prefix '/items' and the endpoint path is '/'. So the full path is '/items/'.
❓ state_output
intermediate2:00remaining
What tags appear in the OpenAPI docs?
Consider this FastAPI router code. Which tags will appear in the OpenAPI documentation for the
read_users endpoint?from fastapi import APIRouter
router = APIRouter(prefix="/users", tags=["users"])
@router.get("/", tags=["custom"])
async def read_users():
return {"msg": "List of users"}FastAPI
from fastapi import APIRouter router = APIRouter(prefix="/users", tags=["users"]) @router.get("/", tags=["custom"]) async def read_users(): return {"msg": "List of users"}
Attempts:
2 left
💡 Hint
Endpoint tags override router tags if provided.
✗ Incorrect
When tags are set on the endpoint, they replace the router's tags in the OpenAPI docs.
📝 Syntax
advanced2:00remaining
Identify the syntax error in router prefix usage
Which option contains a syntax error when defining a FastAPI router with a prefix and tags?
Code snippet:
Assume
Code snippet:
from fastapi import APIRouter router = APIRouter(prefix=PREFIX, tags=TAG_LIST)
Assume
PREFIX and TAG_LIST are variables.FastAPI
from fastapi import APIRouter router = APIRouter(prefix=PREFIX, tags=TAG_LIST)
Attempts:
2 left
💡 Hint
Tags must be a list of strings, not a single string.
✗ Incorrect
Tags parameter expects a list of strings. Option D assigns a string instead of a list, causing a runtime error.
🔧 Debug
advanced2:00remaining
Why does the endpoint not appear under the expected tag?
You have this FastAPI router:
But in the OpenAPI docs, the endpoint appears under the default tag instead of "products". Why?
from fastapi import APIRouter
router = APIRouter(prefix="/products", tags=["products"])
@router.get("/list", tags=[])
async def list_products():
return {"msg": "Products list"}But in the OpenAPI docs, the endpoint appears under the default tag instead of "products". Why?
FastAPI
from fastapi import APIRouter router = APIRouter(prefix="/products", tags=["products"]) @router.get("/list", tags=[]) async def list_products(): return {"msg": "Products list"}
Attempts:
2 left
💡 Hint
Check if the endpoint has tags set explicitly.
✗ Incorrect
If the endpoint has tags set (even empty list), it overrides the router tags in docs. Here, an empty list causes the endpoint to appear under default tag.
🧠 Conceptual
expert3:00remaining
How does router prefix affect nested routers?
Consider two routers in FastAPI:
What is the full path and tags shown in OpenAPI docs for
from fastapi import APIRouter
parent_router = APIRouter(prefix="/parent", tags=["parent"])
child_router = APIRouter(prefix="/child", tags=["child"])
@child_router.get("/endpoint")
async def child_endpoint():
return {"msg": "Child endpoint"}
parent_router.include_router(child_router)What is the full path and tags shown in OpenAPI docs for
child_endpoint?FastAPI
from fastapi import APIRouter parent_router = APIRouter(prefix="/parent", tags=["parent"]) child_router = APIRouter(prefix="/child", tags=["child"]) @child_router.get("/endpoint") async def child_endpoint(): return {"msg": "Child endpoint"} parent_router.include_router(child_router)
Attempts:
2 left
💡 Hint
Prefixes combine when routers are nested; tags do not merge automatically.
✗ Incorrect
The full path combines parent and child prefixes. Tags from child router apply to its endpoints, not parent's tags.