fix: serialize date objects to ISO format for JSON
All checks were successful
Deploy Development / deploy (push) Successful in 45s
Build Test / lint-backend (push) Successful in 1s
Build Test / build-frontend (push) Successful in 14s

- Added serialize_dates() helper to convert date objects to strings
- Applied to list_goals and get_goals_grouped endpoints
- Fixes issue where start_date was saved but not visible in frontend
- Python datetime.date objects need explicit .isoformat() conversion

Root cause: FastAPI doesn't auto-serialize all date types consistently
This commit is contained in:
Lars 2026-03-28 14:36:45 +01:00
parent 370f0d46c7
commit 97defaf704

View File

@ -25,6 +25,19 @@ from goal_utils import get_current_value_for_goal
router = APIRouter(prefix="/api/goals", tags=["goals"]) router = APIRouter(prefix="/api/goals", tags=["goals"])
def serialize_dates(obj):
"""Convert date/datetime objects to ISO format strings for JSON serialization."""
if obj is None:
return None
if isinstance(obj, dict):
return {k: serialize_dates(v) for k, v in obj.items()}
if isinstance(obj, list):
return [serialize_dates(item) for item in obj]
if isinstance(obj, (date,)):
return obj.isoformat()
return obj
# ============================================================================ # ============================================================================
# Pydantic Models # Pydantic Models
# ============================================================================ # ============================================================================
@ -362,6 +375,9 @@ def list_goals(session: dict = Depends(require_auth)):
print(f"[ERROR] Failed to update progress for goal {goal.get('id')}: {e}") print(f"[ERROR] Failed to update progress for goal {goal.get('id')}: {e}")
# Continue with other goals even if one fails # Continue with other goals even if one fails
# Serialize date objects to ISO format strings
goals = serialize_dates(goals)
return goals return goals
except Exception as e: except Exception as e:
@ -700,6 +716,9 @@ def get_goals_grouped(session: dict = Depends(require_auth)):
goal_dict['focus_contributions'] = focus_map.get(goal['id'], []) goal_dict['focus_contributions'] = focus_map.get(goal['id'], [])
grouped[cat].append(goal_dict) grouped[cat].append(goal_dict)
# Serialize date objects to ISO format strings
grouped = serialize_dates(grouped)
return grouped return grouped
# ============================================================================ # ============================================================================