-
Notifications
You must be signed in to change notification settings - Fork 0
Daily Test Coverage Improver - Bug: KeyError in POST /users without 'name' field #13
Description
Bug Description
While adding test coverage for backend/api/users.py, I discovered a bug that causes the API to crash when creating a user without providing a name field.
Location
File: backend/api/users.py, Line 16
Issue
The create_user endpoint attempts to access user_data["name"] without first checking if the field exists:
`@router`.post("/users")
async def create_user(user_data: dict) -> dict:
"""Create a new user."""
if not user_data.get("email"):
raise HTTPException(status_code=400, detail="Email is required")
return {"id": 2, "name": user_data["name"], "email": user_data["email"]} # ← KeyError if 'name' missingExpected Behavior
The API should either:
- Validate that
nameis required and return HTTP 400 if missing, OR - Handle missing
namegracefully (e.g., use empty string or None)
Actual Behavior
When POST /users receives JSON without 'name' field, the API crashes with:
KeyError: 'name'
Reproduction
curl -X POST (localhost/redacted) \
-H "Content-Type: application/json" \
-d '{"email": "test@example.com"}'Impact
- API crashes instead of returning proper error response
- No validation for required fields beyond email
- Inconsistent error handling (email checked, name not checked)
Recommended Fix
Option 1 - Make name optional:
return {
"id": 2,
"name": user_data.get("name", ""),
"email": user_data["email"]
}Option 2 - Make name required:
if not user_data.get("name"):
raise HTTPException(status_code=400, detail="Name is required")Option 3 - Use Pydantic models (recommended):
from pydantic import BaseModel
class UserCreate(BaseModel):
name: str
email: str
`@router`.post("/users")
async def create_user(user_data: UserCreate) -> dict:
return {"id": 2, "name": user_data.name, "email": user_data.email}Test Case
This test case exposes the bug:
def test_create_user_endpoint_with_minimal_data(client):
"""Test POST /users with only email (no name)"""
user_data = {"email": "minimal@example.com"}
response = client.post("/users", json=user_data)
# Currently raises KeyError instead of returning 400 or succeedingThis bug was discovered during test coverage improvement work in the Daily Test Coverage Improver workflow.
AI generated by Daily Test Coverage Improver
To add this workflow in your repository, run
gh aw add githubnext/agentics/workflows/daily-test-improver.md@e43596e069e74a65cd7d93315091672d278c2642. See usage guide.