Make repository list() reject unrecognised filter names
Repository.list(**filters) let each implementation pick out the filter names it understood and silently ignore the rest, so a typo or an unsupported filter returned every row in the table instead of an error. This was hit for real: LeaveWishRepository.list(parent_personal_data_uuid =...) returns every wish in the database, because that implementation only recognises workspace_id and child_id. A guard written on top of it would have passed while checking nothing. All ten implementations now validate against their supported set and raise ValueError naming the offending key. ValueError rather than a domain error because passing a filter that does not exist is a programming bug to fix, not a runtime condition to handle -- it must not become an HTTP 4xx. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
This commit is contained in:
@@ -7,6 +7,7 @@ import aiosqlite
|
||||
|
||||
from app.entities.child import Child
|
||||
from app.repositories.child_repository import ChildRepository
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
|
||||
@@ -87,6 +88,7 @@ class SQLiteChildRepository(ChildRepository):
|
||||
raise NotFoundError(f"Child {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[Child]:
|
||||
validate_filters(filters, {"workspace_id"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
if "workspace_id" in filters and filters["workspace_id"] is not None:
|
||||
|
||||
@@ -12,6 +12,7 @@ import aiosqlite
|
||||
|
||||
from app.entities.day_override import DayOverride
|
||||
from app.repositories.day_override_repository import DayOverrideRepository
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
|
||||
@@ -91,6 +92,7 @@ class SQLiteDayOverrideRepository(DayOverrideRepository):
|
||||
raise NotFoundError(f"DayOverride {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[DayOverride]:
|
||||
validate_filters(filters, {"workspace_id"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
for column in ("workspace_id",):
|
||||
|
||||
@@ -10,6 +10,7 @@ from uuid import UUID
|
||||
import aiosqlite
|
||||
|
||||
from app.entities.leave_wish import LeaveWish
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.repositories.leave_wish_repository import LeaveWishRepository
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
@@ -154,6 +155,7 @@ class SQLiteLeaveWishRepository(LeaveWishRepository):
|
||||
raise NotFoundError(f"LeaveWish {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[LeaveWish]:
|
||||
validate_filters(filters, {"workspace_id", "child_id"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
for column in ("workspace_id", "child_id"):
|
||||
|
||||
@@ -7,6 +7,7 @@ from uuid import UUID
|
||||
import aiosqlite
|
||||
|
||||
from app.entities.parental_leave import ParentalLeaveFetch, TierData
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.repositories.parental_leave_repository import ParentalLeaveRepository
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
@@ -165,6 +166,7 @@ class SQLiteParentalLeaveRepository(ParentalLeaveRepository):
|
||||
raise NotFoundError(f"ParentalLeaveFetch {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[ParentalLeaveFetch]:
|
||||
validate_filters(filters, {"personal_data_uuid", "workspace_id", "child_id"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
for column in ("personal_data_uuid", "workspace_id", "child_id"):
|
||||
|
||||
@@ -10,6 +10,7 @@ from uuid import UUID
|
||||
import aiosqlite
|
||||
|
||||
from app.entities.personal_data import PersonalData
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.repositories.personal_data_repository import PersonalDataRepository
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
@@ -108,14 +109,19 @@ class SQLitePersonalDataRepository(PersonalDataRepository):
|
||||
raise NotFoundError(f"PersonalData {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[PersonalData]:
|
||||
clauses = ["deleted_at IS NULL"]
|
||||
validate_filters(filters, {"workspace_id", "include_deleted"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
include_deleted = filters.get("include_deleted", False)
|
||||
if not include_deleted:
|
||||
clauses.append("deleted_at IS NULL")
|
||||
for column in ("workspace_id",):
|
||||
if column in filters and filters[column] is not None:
|
||||
clauses.append(f"{column} = ?")
|
||||
params.append(str(filters[column]))
|
||||
where = "WHERE " + " AND ".join(clauses)
|
||||
cursor = await self._conn.execute(f"SELECT * FROM personal_data {where}", params)
|
||||
where = "WHERE " + " AND ".join(clauses) if clauses else ""
|
||||
query = f"SELECT * FROM personal_data {where}" if where else "SELECT * FROM personal_data"
|
||||
cursor = await self._conn.execute(query, params)
|
||||
rows = await cursor.fetchall()
|
||||
return [self._row_to_entity(r) for r in rows]
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ from uuid import UUID
|
||||
import aiosqlite
|
||||
|
||||
from app.entities.planned_leave import PlannedLeaveDay
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.repositories.planned_leave_repository import PlannedLeaveRepository
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
@@ -85,6 +86,7 @@ class SQLitePlannedLeaveRepository(PlannedLeaveRepository):
|
||||
raise NotFoundError(f"PlannedLeaveDay {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[PlannedLeaveDay]:
|
||||
validate_filters(filters, {"workspace_id", "child_id", "parent_personal_data_uuid"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
for column in ("workspace_id", "child_id", "parent_personal_data_uuid"):
|
||||
|
||||
@@ -6,6 +6,7 @@ from uuid import UUID
|
||||
import aiosqlite
|
||||
|
||||
from app.entities.user import User
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.repositories.user_repository import UserRepository
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
@@ -72,6 +73,7 @@ class SQLiteUserRepository(UserRepository):
|
||||
raise NotFoundError(f"User {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[User]:
|
||||
validate_filters(filters, set())
|
||||
cursor = await self._conn.execute('SELECT * FROM "user"')
|
||||
rows = await cursor.fetchall()
|
||||
return [self._row_to_entity(r) for r in rows]
|
||||
|
||||
@@ -13,6 +13,7 @@ from uuid import UUID
|
||||
import aiosqlite
|
||||
|
||||
from app.entities.vacation_days import VacationDays
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.repositories.vacation_days_repository import VacationDaysRepository
|
||||
from app.utils.errors import NotFoundError
|
||||
|
||||
@@ -96,6 +97,7 @@ class SQLiteVacationDaysRepository(VacationDaysRepository):
|
||||
raise NotFoundError(f"VacationDays {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[VacationDays]:
|
||||
validate_filters(filters, {"workspace_id", "personal_data_uuid", "year"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
for column in ("workspace_id", "personal_data_uuid", "year"):
|
||||
|
||||
@@ -7,6 +7,7 @@ import aiosqlite
|
||||
|
||||
from app.entities.workspace import Workspace
|
||||
from app.entities.workspace_invitation import WorkspaceInvitation
|
||||
from app.repositories.filter_validator import validate_filters
|
||||
from app.repositories.workspace_repository import (
|
||||
WorkspaceInvitationRepository,
|
||||
WorkspaceRepository,
|
||||
@@ -70,6 +71,7 @@ class SQLiteWorkspaceRepository(WorkspaceRepository):
|
||||
raise NotFoundError(f"Workspace {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[Workspace]:
|
||||
validate_filters(filters, set())
|
||||
cursor = await self._conn.execute("SELECT * FROM workspace")
|
||||
rows = await cursor.fetchall()
|
||||
return [self._row_to_entity(r) for r in rows]
|
||||
@@ -157,6 +159,7 @@ class SQLiteWorkspaceInvitationRepository(WorkspaceInvitationRepository):
|
||||
raise NotFoundError(f"WorkspaceInvitation {id} not found")
|
||||
|
||||
async def list(self, **filters: object) -> list[WorkspaceInvitation]:
|
||||
validate_filters(filters, {"workspace_id", "status"})
|
||||
clauses: list[str] = []
|
||||
params: list[object] = []
|
||||
if "workspace_id" in filters and filters["workspace_id"] is not None:
|
||||
|
||||
Reference in New Issue
Block a user