Challenge - 5 Problems
Path Parameter Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing a path with a typed parameter?
Consider this FastAPI code snippet:
What will be the JSON response when you access
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}
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type automatically.
✗ Incorrect
The path parameter
item_id is declared as int. FastAPI converts the string from the URL to an integer. Then the function returns the doubled value, so 5 * 2 = 10.📝 Syntax
intermediate2: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?Attempts:
2 left
💡 Hint
FastAPI validates path parameters based on their declared type annotations.
✗ Incorrect
Only when the path parameter is declared as
int or float will FastAPI validate and reject invalid strings with a 422 error. Declaring as str or no type means no validation error for non-integers.🔧 Debug
advanced2:00remaining
Why does this FastAPI path parameter validation still work despite the default value?
Look at this code:
When accessing
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}
Attempts:
2 left
💡 Hint
Default values do not make path parameters optional or disable their type validation in FastAPI.
✗ Incorrect
Path parameters are always required and validated based on their type annotation, even with a default value set. FastAPI attempts to convert the path segment ('abc') to int, which fails, resulting in a 422 error. The default is not used.
❓ state_output
advanced2:00remaining
What is the response when using a constrained path parameter with validation?
Given this FastAPI code:
What happens when you access
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}
Attempts:
2 left
💡 Hint
Path parameters can have validation constraints like greater than (gt) and less than (lt).
✗ Incorrect
The Path function enforces that order_id must be greater than 100 and less than 200. 150 meets this, so it returns normally. 99 fails the gt=100 check, so FastAPI returns a 422 error.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about how FastAPI uses Python typing and Pydantic models.
✗ Incorrect
FastAPI reads the type hints on path parameters and uses Pydantic internally to convert and validate the values before passing them to the function. This automatic process ensures type safety and validation.