fix: validate sort_by parameter with Literal type in views.py (#36) #44
No reviewers
Labels
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/major-domo-database#44
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "ai/major-domo-database-36"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
sort_by: strtosort_by: Literal[...]in bothget_season_batting_statsandget_season_pitching_statsendpoints inapp/routers_v3/views.pypa, ab, run, hit, double, triple, homerun, rbi, bb, so, bphr, bpfo, bp1b, bplo, gidp, hbp, sac, ibb, avg, obp, slg, ops, woba, k_pct, sb, cstbf, outs, games, gs, win, loss, hold, saves, bsave, ir, irs, ab, run, e_run, hits, double, triple, homerun, bb, so, hbp, sac, ibb, gidp, sb, cs, bphr, bpfo, bp1b, bplo, wp, balk, wpa, era, whip, avg, obp, slg, ops, woba, hper9, kper9, bbper9, kperbb, lob_2outs, rbipercent, re24sort_byvalues with a 422 Unprocessable Entity response before reaching thegetattrcallFiles Changed
app/routers_v3/views.pyTests
No test suite exists for this repo. Changes verified by reading back modified file — both endpoints now use
Literaltype constraints that match the exact column names defined onSeasonBattingStatsandSeasonPitchingStatsmodels indb_engine.py.Other observations
StratPlay/StratGameattribute access and unused imports — not introduced by this change.AI Code Review
Files Reviewed
app/routers_v3/views.py(modified)Findings
Correctness
Literalvalues againstSeasonBattingStatsandSeasonPitchingStatsfield definitions inapp/db_engine.py— every value in both Literal unions maps exactly to a real model field. No omissions or typos found."woba"for batting,"era"for pitching) are present in the Literal unions and unchanged from before.getattrfallback inget_top_hitters/get_top_pitchersis now effectively dead code for the FastAPI path (FastAPI validates before the call), but it's harmless and lives indb_engine.pyoutside this PR's scope.Security
sort_by: straccepted arbitrary values; they were passed togetattrwith a safe default fallback, so not directly exploitable, but the explicit Literal constraint is cleaner defense-in-depth.sort_byvalues before any ORM code runs.Style & Conventions
sort_order.lower()is now redundant sincesort_orderis constrained toLiteral["asc", "desc"]— both values are already lowercase. Not a blocker, and this pattern predates this PR.List,Query,pydantic) are not introduced by this change and are out of scope.Suggestions
.lower()onsort_ordercould be cleaned up in a follow-up, but it's harmless.Verdict: APPROVED
The Literal type constraints are correct, complete, and directly fix the issue. The diff is larger than the functional change due to ruff reformatting, but the logic is unchanged. Clean fix.
Automated review by Claude PR Reviewer