0
0
FastAPIframework~20 mins

Path parameter types and validation in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Path Parameter Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing a path with a typed parameter?
Consider this FastAPI code snippet:
from fastapi import FastAPI
app = FastAPI()

@app.get('/items/{item_id}')
async def read_item(item_id: int):
    return {"item_id": item_id * 2}

What will be the JSON response when you access /items/5?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/items/{item_id}')
async def read_item(item_id: int):
    return {"item_id": item_id * 2}
A{"item_id": "5"}
B404 Not Found error
C{"item_id": 10}
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type automatically.
📝 Syntax
intermediate
2:00remaining
Which path parameter declaration causes a validation error for non-integers?
Given these FastAPI path operations, which one will reject a non-integer path parameter like /users/abc with a 422 error?
A
@app.get('/users/{user_id}')
async def get_user(user_id: float):
    return {"user_id": user_id}
B
@app.get('/users/{user_id}')
async def get_user(user_id: str):
    return {"user_id": user_id}
C
@app.get('/users/{user_id}')
async def get_user(user_id):
    return {"user_id": user_id}
D
@app.get('/users/{user_id}')
async def get_user(user_id: int):
    return {"user_id": user_id}
Attempts:
2 left
💡 Hint
FastAPI validates path parameters based on their declared type annotations.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI path parameter validation still work despite the default value?
Look at this code:
from fastapi import FastAPI
app = FastAPI()

@app.get('/products/{product_id}')
async def get_product(product_id: int = 0):
    return {"product_id": product_id}

When accessing /products/abc, the API returns a 422 validation error (not {"product_id": 0}). Why?
FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get('/products/{product_id}')
async def get_product(product_id: int = 0):
    return {"product_id": product_id}
ABecause default values in path parameters do not disable type validation; the path segment is still validated as int.
BBecause FastAPI automatically converts 'abc' to 0 when default is set.
CBecause the function is missing a type annotation for product_id.
DBecause the route path is incorrect and does not match the request.
Attempts:
2 left
💡 Hint
Default values do not make path parameters optional or disable their type validation in FastAPI.
state_output
advanced
2:00remaining
What is the response when using a constrained path parameter with validation?
Given this FastAPI code:
from fastapi import FastAPI, Path
app = FastAPI()

@app.get('/orders/{order_id}')
async def read_order(order_id: int = Path(..., gt=100, lt=200)):
    return {"order_id": order_id}

What happens when you access /orders/150 and /orders/99 respectively?
FastAPI
from fastapi import FastAPI, Path
app = FastAPI()

@app.get('/orders/{order_id}')
async def read_order(order_id: int = Path(..., gt=100, lt=200)):
    return {"order_id": order_id}
ABoth /orders/150 and /orders/99 return {"order_id": <value>} without validation.
BAccessing /orders/150 returns 422 error; /orders/99 returns {"order_id": 99}.
CBoth /orders/150 and /orders/99 return 422 validation errors.
DAccessing /orders/150 returns {"order_id": 150}; /orders/99 returns 422 validation error.
Attempts:
2 left
💡 Hint
Path parameters can have validation constraints like greater than (gt) and less than (lt).
🧠 Conceptual
expert
3:00remaining
How does FastAPI handle path parameter type conversion and validation internally?
Which statement best describes FastAPI's process for path parameter type conversion and validation?
AFastAPI converts path parameters to strings and leaves validation to the endpoint function code.
BFastAPI uses Python type hints to convert path parameters and Pydantic to validate constraints before calling the endpoint function.
CFastAPI only validates query parameters, not path parameters, which are always strings.
DFastAPI requires manual conversion and validation of path parameters inside the endpoint function.
Attempts:
2 left
💡 Hint
Think about how FastAPI uses Python typing and Pydantic models.