The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(proxy): add generic list handler for /management/v1 (#35308)
yuneng-jiang416e39815474
diff --git a/litellm/proxy/management_endpoints/management_v1/common.py b/litellm/proxy/management_endpoints/management_v1/common.pyindex c0e7f49f2e9..daa2c60ac5e 100644--- a/litellm/proxy/management_endpoints/management_v1/common.py+++ b/litellm/proxy/management_endpoints/management_v1/common.py@@ -7,6 +7,7 @@ from fastapi.dependencies.utils import get_flat_dependantfrom fastapi.responses import JSONResponsefrom litellm.types.proxy.management_endpoints.management_v1 import (+ ListLinks,PageLinks,ProblemDetail,)@@ -43,6 +44,21 @@ def _declared_query_params(request: Request) -> frozenset[str]:return frozenset(field.alias for field in get_flat_dependant(dependant, skip_repeats=True).query_params)+def escape_like(value: str) -> str:+ """Escape LIKE/ILIKE metacharacters. Ids routinely contain `_`, which is a wildcard unescaped."""+ return value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")+++def unknown_query_param_problem(unknown: tuple[str, ...], allowed: tuple[str, ...]) -> ProblemDetail:+ return ProblemDetail(+ type=f"{PROBLEM_TYPE_BASE}unknown-query-parameter",+ title="Unknown query parameter",+ status=400,+ detail=f"Unrecognized query parameter(s): {', '.join(unknown)}.",+ allowed=sorted(allowed),+ )++async def reject_unknown_query_params(request: Request) -> None:"""Reject any query param the route did not declare.@@ -53,15 +69,7 @@ async def reject_unknown_query_params(request: Request) -> None:unknown: tuple[str, ...] = tuple(sorted(name for name in request.query_params if name not in declared))if not unknown:return- raise ManagementProblem(- ProblemDetail(- type=f"{PROBLEM_TYPE_BASE}unknown-query-parameter",- title="Unknown query parameter",- status=400,- detail=f"Unrecognized query parameter(s): {', '.join(unknown)}.",- allowed=sorted(declared),- )- )+ raise ManagementProblem(unknown_query_param_problem(unknown=unknown, allowed=tuple(sorted(declared))))def _page_url(request: Request, page: int) -> str:@@ -75,3 +83,15 @@ def build_page_links(request: Request, page: int, has_more: bool) -> PageLinks:prev=_page_url(request, page - 1) if page > 1 else None,next=_page_url(request, page + 1) if has_more else None,)+++def build_list_links(request: Request, page: int, total_pages: int) -> ListLinks:+ """Page-mode links. `last` clamps to page 1 on an empty result set so every link still resolves."""+ last = max(total_pages, 1)+ return ListLinks(+ self_link=_page_url(request, page),+ first=_page_url(request, 1),+ prev=_page_url(request, page - 1) if page > 1 else None,+ next=_page_url(request, page + 1) if page < last else None,+ last=_page_url(request, last),+ )diff --git a/litellm/proxy/management_endpoints/management_v1/list_framework.py b/litellm/proxy/management_endpoints/management_v1/list_framework.pynew file mode 100644index 00000000000..3e4b9131d1e--- /dev/null+++ b/litellm/proxy/management_endpoints/management_v1/list_framework.py@@ -0,0 +1,522 @@+"""Generic list handling for `/management/v1` collection routes.++A resource declares a `ListSpec`; `build_query_plan` turns query parameters into a+`QueryPlan` or an RFC 9457 problem without touching a database, and `handle_list`+runs that plan through an injected `ListExecutor`. Keeping the planning pure is what+lets a caller assert the plan as a value instead of asserting against a live Prisma+client, and it keeps this module free of any database dependency.++A plan's `where` is a tuple of frozen `Predicate`s rather than a backend-shaped+mapping, so the framework never has to know which query builder executes it and a+planned predicate cannot be rewritten afterwards. `where_sql` renders one for a+raw-SQL executor with every caller-supplied value bound to a placeholder.+"""++from collections.abc import Callable, Mapping, Sequence+from dataclasses import dataclass+from datetime import datetime, timezone+from math import ceil+from typing import Generic, Literal, Protocol, TypeVar++from fastapi import Request+from pydantic import TypeAdapter, ValidationError+from typing_extensions import assert_never++from litellm.proxy._types import UserAPIKeyAuth+from litellm.proxy.management_endpoints.management_v1.common import (+ PROBLEM_TYPE_BASE,+ ManagementProblem,+ build_list_links,+ escape_like,+ unknown_query_param_problem,+)+from litellm.types.proxy.management_endpoints.management_v1 import (+ ListMeta,+ ListResponse,+ ProblemDetail,+)++ComparisonOp = Literal["eq", "gte", "lte", "gt", "lt", "contains", "not"]+# `is_null` is not in the design doc's operator set. It is here because there is no+# other way to ask for "max_budget IS NULL", and a table that renders nulls as+# "Unlimited" has to be able to filter on them.+FilterOp = ComparisonOp | Literal["in", "is_null"]++FilterType = type[str] | type[int] | type[float] | type[datetime]+FilterValue = str | int | float | datetime++PAGE_PARAM = "page"+PAGE_SIZE_PARAM = "page_size"+SORT_PARAM = "sort"+SEARCH_PARAM = "q"++TRow = TypeVar("TRow")+TRow_co = TypeVar("TRow_co", covariant=True)+TOut = TypeVar("TOut")++_FILTER_OP_ADAPTER: TypeAdapter[FilterOp] = TypeAdapter(FilterOp)+++@dataclass(frozen=True, slots=True)+class Compare:+ """`field <op> value`."""++ field: str+ op: ComparisonOp+ value: FilterValue+++@dataclass(frozen=True, slots=True)+class Within:+ """`field IN (values)`."""++ field: str+ values: tuple[FilterValue, ...]+++@dataclass(frozen=True, slots=True)+class IsNull:+ """`field IS NULL`, or `IS NOT NULL` when negated."""++ field: str+ negated: bool+++@dataclass(frozen=True, slots=True)+class AnyOf:+ """Disjunction of its clauses. `?q=` is the only producer today."""++ clauses: tuple["Predicate", ...]+++Predicate = Compare | Within | IsNull | AnyOf+++@dataclass(frozen=True, slots=True)+class FilterSpec:+ type: FilterType+ ops: frozenset[FilterOp]+++@dataclass(frozen=True, slots=True)+class SortKey:+ field: str+ descending: bool+++@dataclass(frozen=True, slots=True)+class ScopeAll:+ """The caller may read every row of the resource."""+++@dataclass(frozen=True, slots=True)+class ScopeWhere:+ """The caller may read the rows matching every predicate in `where`."""++ where: tuple[Predicate, ...]+++@dataclass(frozen=True, slots=True)+class ScopeDenied:+ """The caller may read no rows at all, and should be told so rather than shown an empty page."""++ reason: str+++Scope = ScopeAll | ScopeWhere | ScopeDenied+++@dataclass(frozen=True, slots=True)+class ListSpec(Generic[TRow, TOut]):+ resource: str+ sortable: frozenset[str]+ searchable: frozenset[str]+ filters: Mapping[str, FilterSpec]+ default_sort: tuple[SortKey, ...]+ default_page_size: int+ max_page_size: int+ scope: Callable[[UserAPIKeyAuth], Scope]+ serialize: Callable[[TRow], TOut]+ tiebreaker: str++ def __post_init__(self) -> None:+ """A malformed spec is a programming error at import time, so this raises rather than+ returning a problem: there is no request in flight and no caller to answer."""+ if not 1 <= self.default_page_size <= self.max_page_size:+ raise ValueError(+ f"{self.resource}: default_page_size must be between 1 and max_page_size "+ f"({self.max_page_size}), got {self.default_page_size}. A default above the cap "+ f"would serve more rows than the resource allows whenever page_size is omitted."+ )+ if not self.tiebreaker:+ raise ValueError(f"{self.resource}: tiebreaker is required; it is the final sort key on every query.")+ undeclared = tuple(sorted(frozenset(key.field for key in self.default_sort) - self.sortable))+ if undeclared:+ raise ValueError(f"{self.resource}: default_sort orders by non-sortable field(s): {', '.join(undeclared)}.")+ non_text = tuple(+ sorted(field for field, spec in self.filters.items() if "contains" in spec.ops and spec.type is not str)+ )+ if non_text:+ raise ValueError(+ f"{self.resource}: contains renders as ILIKE and is only meaningful on text columns, "+ f"but is declared on: {', '.join(non_text)}."+ )+++@dataclass(frozen=True, slots=True)+class QueryPlan:+ """`where` is an implicit AND, ordered scope-first; `order` always ends with the spec's tiebreaker."""++ where: tuple[Predicate, ...]+ order: tuple[SortKey, ...]+ skip: int+ take: int+++class ListExecutor(Protocol[TRow_co]):+ """The database half of a list, injected so this module never imports Prisma."""++ async def count(self, where: tuple[Predicate, ...]) -> int: ...++ async def find_many(self, plan: QueryPlan) -> Sequence[TRow_co]: ...+++def order_by_sql(order: tuple[SortKey, ...]) -> str:+ """`ORDER BY` body for a plan, NULLS LAST in both directions.++ Postgres sorts nulls last ascending but first descending, so an unqualified flip of+ the sort direction drags every "Unlimited" row to the top of the table. Every field+ reaching here is either a member of `ListSpec.sortable` (caller-supplied sort is+ checked against it, `default_sort` at construction) or the spec's `tiebreaker`, so+ these are developer-declared column names, never caller-controlled text.+ """+ return ", ".join(f'"{key.field}" {"DESC" if key.descending else "ASC"} NULLS LAST' for key in order)+++def _sql_operator(op: ComparisonOp) -> str:+ match op:+ case "eq":+ return "="+ case "not":+ return "<>"+ case "gte":+ return ">="+ case "lte":+ return "<="+ case "gt":+ return ">"+ case "lt":+ return "<"+ case "contains":+ return "ILIKE"+ case _:+ assert_never(op)+++def _render(predicate: Predicate, index: int) -> tuple[str, tuple[object, ...]]:+ match predicate:+ case IsNull(field=field, negated=negated):+ return f'"{field}" IS {"NOT NULL" if negated else "NULL"}', ()+ case Within(field=field, values=values):+ placeholders = ", ".join(f"${index + offset}" for offset in range(len(values)))+ return f'"{field}" IN ({placeholders})', values+ case AnyOf(clauses=clauses):+ rendered, params = _render_all(clauses, index)+ return f"({' OR '.join(rendered)})", params+ case Compare(field=field, op="contains", value=value):+ return f"\"{field}\" ILIKE ${index} ESCAPE '\\'", (f"%{escape_like(str(value))}%",)+ case Compare(field=field, op=op, value=value):+ return f'"{field}" {_sql_operator(op)} ${index}', (value,)+ case _:+ assert_never(predicate)+++def _render_all(predicates: tuple[Predicate, ...], index: int) -> tuple[tuple[str, ...], tuple[object, ...]]:+ if not predicates:+ return (), ()+ head, head_params = _render(predicates[0], index)+ tail, tail_params = _render_all(predicates[1:], index + len(head_params))+ return (head, *tail), head_params + tail_params+++def where_sql(where: tuple[Predicate, ...], first_index: int = 1) -> tuple[str, tuple[object, ...]]:+ """`WHERE` body and its bind parameters, numbered from `first_index`.++ Returns `("", ())` when there is nothing to filter on. Every caller-supplied value+ becomes a `$n` placeholder rather than being written into the SQL text; only column+ names reach the text, and those come from the spec's own declarations.+ """+ clauses, params = _render_all(where, first_index)+ return " AND ".join(clauses), params+++def _problem(slug: str, title: str, status: int, detail: str, allowed: tuple[str, ...] | None = None) -> ProblemDetail:+ return ProblemDetail(+ type=f"{PROBLEM_TYPE_BASE}{slug}",+ title=title,+ status=status,+ detail=detail,+ allowed=sorted(allowed) if allowed is not None else None,+ )+++def _invalid(detail: str) -> ProblemDetail:+ return _problem("invalid-query-parameter", "Invalid query parameter", 400, detail)+++def _parse_filter_key(name: str) -> tuple[str, FilterOp] | None:+ """`filter[max_budget][gte]` -> `("max_budget", "gte")`; bare `filter[status]` -> `("status", "eq")`."""+ if not name.startswith("filter[") or not name.endswith("]"):+ return None+ field, separator, raw_op = name[len("filter[") : -1].partition("][")+ if not separator:+ return field, "eq"+ try:+ return field, _FILTER_OP_ADAPTER.validate_python(raw_op)+ except ValidationError:+ return None+++def _is_known_param(spec: ListSpec[TRow, TOut], name: str) -> bool:+ if name in (PAGE_PARAM, PAGE_SIZE_PARAM):+ return True+ if name == SORT_PARAM:+ return bool(spec.sortable)+ if name == SEARCH_PARAM:+ return bool(spec.searchable)+ parsed = _parse_filter_key(name)+ return parsed is not None and parsed[0] in spec.filters+++def _allowed_params(spec: ListSpec[TRow, TOut]) -> tuple[str, ...]:+ return tuple(+ sorted(+ (PAGE_PARAM, PAGE_SIZE_PARAM)+ + ((SORT_PARAM,) if spec.sortable else ())+ + ((SEARCH_PARAM,) if spec.searchable else ())+ + tuple(+ f"filter[{field}]" if op == "eq" else f"filter[{field}][{op}]"+ for field, filter_spec in spec.filters.items()+ for op in filter_spec.ops+ )+ )+ )+++def _parse_positive_int(name: str, raw: str) -> int | ProblemDetail:+ try:+ value = int(raw)+ except ValueError:+ return _invalid(f"'{name}' must be an integer.")+ if value < 1:+ return _invalid(f"'{name}' must be 1 or greater.")+ return value+++def _parse_page(params: Mapping[str, str]) -> int | ProblemDetail:+ raw = params.get(PAGE_PARAM)+ return 1 if raw is None else _parse_positive_int(PAGE_PARAM, raw)+++def _parse_page_size(spec: ListSpec[TRow, TOut], params: Mapping[str, str]) -> int | ProblemDetail:+ raw = params.get(PAGE_SIZE_PARAM)+ if raw is None:+ return spec.default_page_size+ value = _parse_positive_int(PAGE_SIZE_PARAM, raw)+ if isinstance(value, ProblemDetail):+ return value+ return min(value, spec.max_page_size)+++def _parse_sort(spec: ListSpec[TRow, TOut], params: Mapping[str, str]) -> tuple[SortKey, ...] | ProblemDetail:+ raw = params.get(SORT_PARAM)+ if raw is None:+ return spec.default_sort+ segments = tuple(segment.strip() for segment in raw.split(","))+ keys = tuple(+ SortKey(field=segment[1:] if segment.startswith("-") else segment, descending=segment.startswith("-"))+ for segment in segments+ )+ rejected = tuple(sorted(frozenset(key.field for key in keys) - spec.sortable))+ if rejected:+ return _problem(+ "invalid-sort-field",+ "Invalid sort field",+ 400,+ f"Cannot sort {spec.resource} by: {', '.join(repr(field) for field in rejected)}.",+ tuple(spec.sortable),+ )+ return keys+++def _to_utc(value: datetime) -> datetime:+ return value.replace(tzinfo=timezone.utc) if value.tzinfo is None else value.astimezone(timezone.utc)+++def _coerce(field: str, op: FilterOp, raw: str, target: FilterType) -> FilterValue | ProblemDetail:+ try:+ if target is str:+ return raw+ if target is int:+ return int(raw)+ if target is float:+ return float(raw)+ return _to_utc(datetime.fromisoformat(raw[:-1] + "+00:00" if raw.endswith("Z") else raw))+ except ValueError:+ return _invalid(f"'filter[{field}][{op}]' is not a valid {target.__name__}: {raw!r}.")+++def _null_predicate(field: str, raw: str) -> Predicate | ProblemDetail:+ if raw.lower() == "true":+ return IsNull(field=field, negated=False)+ if raw.lower() == "false":+ return IsNull(field=field, negated=True)+ return _invalid(f"'filter[{field}][is_null]' must be 'true' or 'false'.")+++def _within_predicate(field: str, raw: str, target: FilterType) -> Predicate | ProblemDetail:+ coerced = tuple(_coerce(field, "in", item.strip(), target) for item in raw.split(","))+ problems = tuple(item for item in coerced if isinstance(item, ProblemDetail))+ if problems:+ return problems[0]+ return Within(field=field, values=tuple(item for item in coerced if not isinstance(item, ProblemDetail)))+++def _parse_filter(field: str, op: FilterOp, raw: str, filter_spec: FilterSpec) -> Predicate | ProblemDetail:+ if op not in filter_spec.ops:+ return _problem(+ "unsupported-filter-operator",+ "Unsupported filter operator",+ 400,+ f"Operator '{op}' is not supported on '{field}'.",+ tuple(filter_spec.ops),+ )+ if op == "is_null":+ return _null_predicate(field, raw)+ if op == "in":+ return _within_predicate(field, raw, filter_spec.type)+ value = _coerce(field, op, raw, filter_spec.type)+ if isinstance(value, ProblemDetail):+ return value+ return Compare(field=field, op=op, value=value)+++def _parse_filters(spec: ListSpec[TRow, TOut], params: Mapping[str, str]) -> tuple[Predicate, ...] | ProblemDetail:+ keys = tuple(+ (name, parsed)+ for name in sorted(params)+ if (parsed := _parse_filter_key(name)) is not None and parsed[0] in spec.filters+ )+ parsed = tuple(_parse_filter(field, op, params[name], spec.filters[field]) for name, (field, op) in keys)+ problems = tuple(item for item in parsed if isinstance(item, ProblemDetail))+ if problems:+ return problems[0]+ return tuple(item for item in parsed if not isinstance(item, ProblemDetail))+++def _search_predicate(spec: ListSpec[TRow, TOut], params: Mapping[str, str]) -> Predicate | None:+ raw = params.get(SEARCH_PARAM)+ if not raw:+ return None+ return AnyOf(clauses=tuple(Compare(field=field, op="contains", value=raw) for field in sorted(spec.searchable)))+++def _scope_predicates(scope: Scope) -> tuple[Predicate, ...] | ProblemDetail:+ match scope:+ case ScopeAll():+ return ()+ case ScopeWhere(where=where):+ return where+ case ScopeDenied(reason=reason):+ return _problem("forbidden", "Forbidden", 403, reason)+ case _:+ assert_never(scope)+++def build_query_plan(+ spec: ListSpec[TRow, TOut],+ params: Mapping[str, str],+ caller: UserAPIKeyAuth,+) -> QueryPlan | ProblemDetail:+ """Turn query parameters into a plan, or into the problem that explains why they are not one."""+ scope_predicates = _scope_predicates(spec.scope(caller))+ if isinstance(scope_predicates, ProblemDetail):+ return scope_predicates++ unknown = tuple(sorted(name for name in params if not _is_known_param(spec, name)))+ if unknown:+ return unknown_query_param_problem(unknown=unknown, allowed=_allowed_params(spec))++ page = _parse_page(params)+ if isinstance(page, ProblemDetail):+ return page++ page_size = _parse_page_size(spec, params)+ if isinstance(page_size, ProblemDetail):+ return page_size++ sort = _parse_sort(spec, params)+ if isinstance(sort, ProblemDetail):+ return sort++ filters = _parse_filters(spec, params)+ if isinstance(filters, ProblemDetail):+ return filters++ search = _search_predicate(spec, params)+ return QueryPlan(+ # Scope first: conjuncts a caller filter sits behind and cannot replace.+ where=scope_predicates + filters + ((search,) if search is not None else ()),+ # Ordering by an all-null column without a unique final key lets Postgres return+ # the same row on two different pages.+ order=sort + (SortKey(field=spec.tiebreaker, descending=False),),+ skip=(page - 1) * page_size,+ take=page_size,+ )+++def _duplicate_params(request: Request) -> tuple[str, ...]:+ names = tuple(name for name, _ in request.query_params.multi_items())+ return tuple(sorted(frozenset(name for name in names if names.count(name) > 1)))+++async def handle_list(+ spec: ListSpec[TRow, TOut],+ executor: ListExecutor[TRow],+ request: Request,+ caller: UserAPIKeyAuth,+) -> ListResponse[TOut]:+ """Plan, execute, count, serialize, envelope. Failures reach the client as RFC 9457 problems."""+ plan = build_query_plan(spec=spec, params=request.query_params, caller=caller)+ if isinstance(plan, ProblemDetail):+ raise ManagementProblem(plan)++ # Checked here rather than in build_query_plan because a Mapping[str, str] cannot+ # represent a repeat: query_params.get() silently keeps the last one, so ?page=1&page=999+ # would page from 999 without the caller ever being told which value won.+ duplicates = _duplicate_params(request)+ if duplicates:+ raise ManagementProblem(+ _problem(+ "duplicate-query-parameter",+ "Duplicate query parameter",+ 400,+ f"Repeated query parameter(s): {', '.join(duplicates)}. Each may appear once; "+ f"use a comma-separated list for multiple sort keys or filter values.",+ )+ )++ total_count = await executor.count(plan.where)+ rows = await executor.find_many(plan)+ total_pages = ceil(total_count / plan.take)+ page = plan.skip // plan.take + 1+ return ListResponse[TOut](+ data=tuple(spec.serialize(row) for row in rows),+ meta=ListMeta(+ total_count=total_count,+ page=page,+ page_size=plan.take,+ total_pages=total_pages,+ ),+ links=build_list_links(request=request, page=page, total_pages=total_pages),+ )diff --git a/litellm/proxy/management_endpoints/management_v1/spend_logs.py b/litellm/proxy/management_endpoints/management_v1/spend_logs.pyindex c11a14bbfea..ccde3c4112c 100644--- a/litellm/proxy/management_endpoints/management_v1/spend_logs.py+++ b/litellm/proxy/management_endpoints/management_v1/spend_logs.py@@ -13,6 +13,7 @@ from litellm.proxy.management_endpoints.management_v1.common import (PROBLEM_TYPE_BASE,ManagementProblem,build_page_links,+ escape_like,reject_unknown_query_params,)from litellm.proxy.utils import PrismaClient@@ -34,10 +35,6 @@ def _as_utc(value: datetime) -> datetime:return value.replace(tzinfo=timezone.utc) if value.tzinfo is None else value.astimezone(timezone.utc)-def _escape_like(value: str) -> str:- return value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")--async def _end_user_scope_clause(user_api_key_dict: UserAPIKeyAuth,prisma_client: PrismaClient,@@ -133,7 +130,7 @@ async def list_spend_log_end_users()window_params: tuple[Any, ...] = (_as_utc(start_time), _as_utc(end_time))- search_params: tuple[Any, ...] = (f"%{_escape_like(q)}%",) if q else ()+ search_params: tuple[Any, ...] = (f"%{escape_like(q)}%",) if q else ()search_clause = (f"end_user ILIKE ${len(window_params) + 1} ESCAPE '\\'",) if q else ()scope_clause, scope_params = await _end_user_scope_clause(diff --git a/litellm/types/proxy/management_endpoints/management_v1.py b/litellm/types/proxy/management_endpoints/management_v1.pyindex 2aecc54f114..b2244f6eb9b 100644--- a/litellm/types/proxy/management_endpoints/management_v1.py+++ b/litellm/types/proxy/management_endpoints/management_v1.py@@ -1,7 +1,11 @@"""Shared response shapes for the `/management/v1` control-plane surface."""+from typing import Generic, TypeVar+from pydantic import BaseModel, ConfigDict, Field+TOut = TypeVar("TOut")+class ProblemDetail(BaseModel):"""RFC 9457 problem details, served as `application/problem+json`."""@@ -37,3 +41,33 @@ class FacetListResponse(BaseModel):data: list[str]meta: PageMetalinks: PageLinks+++class ListMeta(BaseModel):+ """Page-mode counterpart to `PageMeta`: an entity list pays for the COUNT(*) so the table can show a page count."""++ total_count: int+ page: int+ page_size: int+ total_pages: int+++class ListLinks(BaseModel):+ """Page-mode counterpart to `PageLinks`. `first`/`last` are knowable here because the total count is."""++ model_config = ConfigDict(populate_by_name=True)++ self_link: str = Field(alias="self")+ first: str+ prev: str | None = None+ next: str | None = None+ last: str+++class ListResponse(BaseModel, Generic[TOut]):+ """Rows stay flat: JSON:API's `{type, id, attributes}` wrapper is a deliberate deviation, so every+ dashboard column accessor would otherwise have to go through `.attributes`."""++ data: list[TOut]+ meta: ListMeta+ links: ListLinksdiff --git a/tests/test_litellm/proxy/management_endpoints/management_v1/test_list_framework.py b/tests/test_litellm/proxy/management_endpoints/management_v1/test_list_framework.pynew file mode 100644index 00000000000..35bd5517361--- /dev/null+++ b/tests/test_litellm/proxy/management_endpoints/management_v1/test_list_framework.py@@ -0,0 +1,871 @@+from collections.abc import Mapping, Sequence+from dataclasses import dataclass, replace+from datetime import datetime, timezone++import pytest+from fastapi import Request+from pydantic import BaseModel++from litellm.proxy._types import UserAPIKeyAuth+from litellm.proxy.management_endpoints.management_v1.common import (+ MANAGEMENT_V1_PREFIX,+ PROBLEM_TYPE_BASE,+ ManagementProblem,+ build_page_links,+)+from litellm.proxy.management_endpoints.management_v1.list_framework import (+ AnyOf,+ Compare,+ FilterSpec,+ IsNull,+ ListSpec,+ QueryPlan,+ ScopeAll,+ ScopeDenied,+ ScopeWhere,+ SortKey,+ Within,+ build_query_plan,+ handle_list,+ order_by_sql,+ where_sql,+)+from litellm.types.proxy.management_endpoints.management_v1 import (+ PageLinks,+ PageMeta,+ ProblemDetail,+)++BUDGETS_PATH = f"{MANAGEMENT_V1_PREFIX}/budgets"+CALLER = UserAPIKeyAuth(user_id="caller-1")+++@dataclass(frozen=True, slots=True)+class BudgetRow:+ budget_id: str+ max_budget: float | None+ created_by: str+++class BudgetOut(BaseModel):+ budget_id: str+ max_budget: float | None+++def _serialize(row: BudgetRow) -> BudgetOut:+ return BudgetOut(budget_id=row.budget_id, max_budget=row.max_budget)+++def _spec(+ scope=lambda caller: ScopeAll(),+ searchable=frozenset({"budget_id", "created_by"}),+ sortable=frozenset({"max_budget", "created_at", "budget_id"}),+) -> ListSpec[BudgetRow, BudgetOut]:+ return ListSpec(+ resource="budgets",+ sortable=sortable,+ searchable=searchable,+ filters={+ "max_budget": FilterSpec(type=float, ops=frozenset({"eq", "gte", "lte", "is_null"})),+ "created_at": FilterSpec(type=datetime, ops=frozenset({"gte", "lte"})),+ "created_by": FilterSpec(type=str, ops=frozenset({"eq", "in", "contains"})),+ "tpm_limit": FilterSpec(type=int, ops=frozenset({"eq"})),+ },+ default_sort=(SortKey(field="created_at", descending=True),),+ default_page_size=25,+ max_page_size=100,+ scope=scope,+ serialize=_serialize,+ tiebreaker="budget_id",+ )+++def _spec_with(**overrides) -> ListSpec[BudgetRow, BudgetOut]:+ """`replace` re-runs `__init__`, so the spec's own validation applies to the override."""+ return replace(_spec(), **overrides)+++class RecordingExecutor:+ """In-memory stand-in for the Prisma-backed executor PR 2 supplies."""++ def __init__(self, rows: tuple[BudgetRow, ...], total_count: int | None = None) -> None:+ self.rows = rows+ self.total_count = len(rows) if total_count is None else total_count+ self.plan: QueryPlan | None = None+ self.count_where: tuple[object, ...] | None = None++ async def count(self, where: tuple[object, ...]) -> int:+ self.count_where = where+ return self.total_count++ async def find_many(self, plan: QueryPlan) -> Sequence[BudgetRow]:+ self.plan = plan+ return self.rows[plan.skip : plan.skip + plan.take]+++def _request(query: str = "") -> Request:+ return Request(+ {+ "type": "http",+ "method": "GET",+ "scheme": "http",+ "root_path": "",+ "path": BUDGETS_PATH,+ "query_string": query.encode(),+ "headers": [(b"host", b"testserver")],+ }+ )+++def _plan(query_params: Mapping[str, str], spec: ListSpec[BudgetRow, BudgetOut] | None = None) -> QueryPlan:+ result = build_query_plan(spec=spec or _spec(), params=query_params, caller=CALLER)+ assert isinstance(result, QueryPlan), result+ return result+++def _problem(query_params: Mapping[str, str], spec: ListSpec[BudgetRow, BudgetOut] | None = None) -> ProblemDetail:+ result = build_query_plan(spec=spec or _spec(), params=query_params, caller=CALLER)+ assert isinstance(result, ProblemDetail), result+ return result+++def _conjuncts(plan: QueryPlan) -> tuple[object, ...]:+ return plan.where+++# ---------------------------------------------------------------- invariant 1+++def test_appends_the_tiebreaker_to_the_default_sort():+ """Without a unique final key, ordering by an all-null column lets Postgres hand the+ same row back on two different pages."""+ assert _plan({}).order == (SortKey(field="created_at", descending=True), SortKey(field="budget_id", descending=False))+++def test_appends_the_tiebreaker_to_an_explicit_multi_key_sort():+ order = _plan({"sort": "-max_budget,created_at"}).order++ assert len(order) == 3+ assert order[-1] == SortKey(field="budget_id", descending=False)+++def test_appends_the_tiebreaker_even_when_the_caller_already_sorts_by_it():+ """Deduplicating it away is the tempting simplification, and it is the one that+ reintroduces a non-total order the moment the leading key stops being unique."""+ assert _plan({"sort": "-budget_id"}).order == (+ SortKey(field="budget_id", descending=True),+ SortKey(field="budget_id", descending=False),+ )+++# ---------------------------------------------------------------- invariant 2+++def test_orders_nulls_last_in_both_directions():+ """Postgres sorts nulls last ascending but first descending, so flipping the sort+ direction on max_budget would otherwise float every "Unlimited" row to the top."""+ sql = order_by_sql((SortKey(field="max_budget", descending=True), SortKey(field="budget_id", descending=False)))++ assert sql == '"max_budget" DESC NULLS LAST, "budget_id" ASC NULLS LAST'+++def test_order_sql_covers_every_key_in_the_plan():+ sql = order_by_sql(_plan({"sort": "-max_budget,created_at"}).order)++ assert sql.count("NULLS LAST") == 3+ assert sql == '"max_budget" DESC NULLS LAST, "created_at" ASC NULLS LAST, "budget_id" ASC NULLS LAST'+++# ------------------------------------------------------------ where rendering+++def test_every_caller_value_is_bound_not_interpolated():+ """The one property that keeps a filter value from reaching the SQL text. A value that+ looks like SQL has to come back as a parameter, never as part of the statement."""+ sql, params = where_sql((Compare(field="budget_id", op="eq", value="'; DROP TABLE x --"),))++ assert sql == '"budget_id" = $1'+ assert params == ("'; DROP TABLE x --",)+ assert "DROP" not in sql+++def test_placeholders_are_numbered_across_the_whole_plan():+ """A predicate that binds several values has to advance the counter by that many, or+ every later predicate reads the wrong parameter."""+ sql, params = where_sql(+ (+ Compare(field="created_by", op="eq", value="alice"),+ Within(field="budget_id", values=("a", "b", "c")),+ Compare(field="max_budget", op="gte", value=5.0),+ )+ )++ assert sql == '"created_by" = $1 AND "budget_id" IN ($2, $3, $4) AND "max_budget" >= $5'+ assert params == ("alice", "a", "b", "c", 5.0)+++def test_placeholder_numbering_can_start_past_earlier_parameters():+ sql, params = where_sql((Compare(field="created_by", op="eq", value="alice"),), first_index=4)++ assert sql == '"created_by" = $4'+ assert params == ("alice",)+++def test_is_null_binds_no_parameter_and_does_not_consume_a_placeholder():+ sql, params = where_sql(+ (IsNull(field="max_budget", negated=False), Compare(field="created_by", op="eq", value="alice"))+ )++ assert sql == '"max_budget" IS NULL AND "created_by" = $1'+ assert params == ("alice",)+++def test_is_null_negated_renders_is_not_null():+ assert where_sql((IsNull(field="max_budget", negated=True),))[0] == '"max_budget" IS NOT NULL'+++def test_a_search_renders_as_a_parenthesised_or():+ """Without the parentheses the OR would bind looser than the surrounding ANDs and the+ scope predicate would stop constraining the search branch."""+ sql, params = where_sql(+ (+ Compare(field="created_by", op="eq", value="alice"),+ AnyOf(+ clauses=(+ Compare(field="budget_id", op="contains", value="prod"),+ Compare(field="created_by", op="contains", value="prod"),+ )+ ),+ )+ )++ assert sql == (+ '"created_by" = $1 AND ('+ "\"budget_id\" ILIKE $2 ESCAPE '\\'"+ " OR "+ "\"created_by\" ILIKE $3 ESCAPE '\\'"+ ")"+ )+ assert params == ("alice", "%prod%", "%prod%")+++def test_contains_escapes_like_metacharacters():+ """Budget ids routinely contain '_', which is a single-character wildcard unescaped."""+ _, params = where_sql((Compare(field="budget_id", op="contains", value="device_id%"),))++ assert params == (r"%device\_id\%%",)+++ ("op", "operator"),+ [("eq", "="), ("not", "<>"), ("gte", ">="), ("lte", "<="), ("gt", ">"), ("lt", "<")],+)+def test_each_comparison_operator_renders_its_sql_spelling(op, operator):+ assert where_sql((Compare(field="max_budget", op=op, value=1),))[0] == f'"max_budget" {operator} $1'+++def test_an_empty_plan_renders_no_where_body():+ assert where_sql(()) == ("", ())+++def test_a_planned_filter_renders_end_to_end():+ """Ties the parser to the renderer: what build_query_plan produces is what executes."""+ sql, params = where_sql(_plan({"filter[max_budget][is_null]": "true", "q": "prod"}).where)++ assert sql == (+ '"max_budget" IS NULL AND ('+ "\"budget_id\" ILIKE $1 ESCAPE '\\'"+ " OR "+ "\"created_by\" ILIKE $2 ESCAPE '\\'"+ ")"+ )+ assert params == ("%prod%", "%prod%")+++# ---------------------------------------------------------------- invariant 3+++def test_the_scope_predicate_is_the_first_conjunct():+ spec = _spec(scope=lambda caller: ScopeWhere(where=(Compare(field="created_by", op="eq", value=caller.user_id),)))++ conjuncts = _conjuncts(_plan({"filter[max_budget][gte]": "5"}, spec=spec))++ assert conjuncts[0] == Compare(field="created_by", op="eq", value="caller-1")+++def test_a_caller_filter_cannot_replace_the_scope_predicate():+ """The failure this guards is a `{**scope, **filters}` merge: a caller filtering on+ the scoped column would silently overwrite the scope and read another user's rows."""+ spec = _spec(scope=lambda caller: ScopeWhere(where=(Compare(field="created_by", op="eq", value=caller.user_id),)))++ conjuncts = _conjuncts(_plan({"filter[created_by][eq]": "someone-else"}, spec=spec))++ assert conjuncts[0] == Compare(field="created_by", op="eq", value="caller-1")+ assert Compare(field="created_by", op="eq", value="someone-else") in conjuncts+ assert len(conjuncts) == 2+++def test_the_scope_predicate_survives_a_search():+ spec = _spec(scope=lambda caller: ScopeWhere(where=(Compare(field="created_by", op="eq", value=caller.user_id),)))++ conjuncts = _conjuncts(_plan({"q": "prod"}, spec=spec))++ assert conjuncts[0] == Compare(field="created_by", op="eq", value="caller-1")+ assert any(isinstance(conjunct, AnyOf) for conjunct in conjuncts)+++def test_an_unscoped_caller_gets_no_scope_conjunct():+ assert _plan({"filter[max_budget][gte]": "5"}).where == (Compare(field="max_budget", op="gte", value=5.0),)+++def test_an_unfiltered_unscoped_list_has_an_empty_where():+ assert _plan({}).where == ()+++# ---------------------------------------------------------------- invariant 4+++def test_a_denied_scope_is_a_403_problem():+ spec = _spec(scope=lambda caller: ScopeDenied(reason="Only a proxy admin can list budgets."))++ problem = _problem({}, spec=spec)++ assert problem.status == 403+ assert problem.type == f"{PROBLEM_TYPE_BASE}forbidden"+ assert problem.detail == "Only a proxy admin can list budgets."+++async def test_a_denied_scope_never_reaches_the_database():+ """A 200 with an empty list would tell the caller the resource is empty rather than+ that they cannot read it, and would still pay for the query."""+ spec = _spec(scope=lambda caller: ScopeDenied(reason="nope"))+ executor = RecordingExecutor(rows=(BudgetRow(budget_id="b1", max_budget=None, created_by="x"),))++ with pytest.raises(ManagementProblem) as raised:+ await handle_list(spec=spec, executor=executor, request=_request(), caller=CALLER)++ assert raised.value.problem.status == 403+ assert executor.plan is None+ assert executor.count_where is None+++# ---------------------------------------------------------------- invariant 5+++def test_page_size_falls_back_to_the_spec_default():+ assert _plan({}).take == 25+++def test_page_size_is_clamped_to_the_spec_maximum():+ """Clamped rather than rejected: an over-large page is a UI bug, not a caller error,+ but serving it would let one request read the whole table."""+ assert _plan({"page_size": "100000"}).take == 100+++def test_page_offsets_by_page_size():+ plan = _plan({"page": "3", "page_size": "10"})++ assert (plan.skip, plan.take) == (20, 10)++[email protected]("page", ["0", "-1"], ids=["zero", "negative"])+def test_page_below_one_is_rejected(page):+ problem = _problem({"page": page})++ assert problem.status == 400+ assert problem.type == f"{PROBLEM_TYPE_BASE}invalid-query-parameter"+++ "params",+ [{"page": "one"}, {"page_size": "many"}, {"page_size": "0"}],+ ids=["page-not-an-int", "page-size-not-an-int", "page-size-zero"],+)+def test_unusable_paging_values_are_rejected(params):+ assert _problem(params).status == 400+++# ---------------------------------------------------------------- invariant 6+++def test_an_unknown_query_parameter_is_rejected_with_the_allowed_set():+ problem = _problem({"page_sizee": "10"})++ assert problem.status == 400+ assert problem.type == f"{PROBLEM_TYPE_BASE}unknown-query-parameter"+ assert "page_sizee" in problem.detail+ assert problem.allowed is not None+ assert "page_size" in problem.allowed+ assert "filter[max_budget][gte]" in problem.allowed+++def test_the_allowed_set_enumerates_only_operators_the_field_declares():+ problem = _problem({"nope": "1"})++ assert problem.allowed is not None+ assert "filter[max_budget][is_null]" in problem.allowed+ assert "filter[tpm_limit][gte]" not in problem.allowed+ assert "filter[created_by][in]" in problem.allowed+++def test_a_filter_on_an_undeclared_field_is_an_unknown_parameter():+ problem = _problem({"filter[secret_column][eq]": "x"})++ assert problem.type == f"{PROBLEM_TYPE_BASE}unknown-query-parameter"+ assert "filter[secret_column][eq]" in problem.detail+++def test_every_declared_parameter_is_accepted():+ """Guards the unknown-param check against rejecting the spec's own contract."""+ plan = _plan(+ {+ "page": "2",+ "page_size": "10",+ "sort": "-max_budget",+ "q": "prod",+ "filter[max_budget][gte]": "5",+ "filter[created_by][in]": "a,b",+ }+ )++ assert plan.take == 10+++# ---------------------------------------------------------------- invariant 7+++def test_sorting_by_an_undeclared_field_is_rejected():+ problem = _problem({"sort": "api_key"})++ assert problem.status == 400+ assert problem.type == f"{PROBLEM_TYPE_BASE}invalid-sort-field"+ assert problem.allowed == ["budget_id", "created_at", "max_budget"]+ assert "api_key" in problem.detail+++def test_one_bad_key_rejects_the_whole_multi_key_sort():+ """Dropping the unknown key and sorting by the rest would silently return a+ differently-ordered page than the one asked for."""+ assert _problem({"sort": "-created_at,api_key"}).type == f"{PROBLEM_TYPE_BASE}invalid-sort-field"+++def test_a_double_dash_prefix_is_not_a_descending_sort():+ assert _problem({"sort": "--created_at"}).type == f"{PROBLEM_TYPE_BASE}invalid-sort-field"+++# ---------------------------------------------------------------- invariant 8+++def test_an_operator_the_field_does_not_declare_is_rejected():+ problem = _problem({"filter[max_budget][contains]": "5"})++ assert problem.status == 400+ assert problem.type == f"{PROBLEM_TYPE_BASE}unsupported-filter-operator"+ assert problem.allowed == ["eq", "gte", "is_null", "lte"]+ assert "contains" in problem.detail+++def test_the_same_operator_is_accepted_on_a_field_that_declares_it():+ """Pins the rejection to the field's own operator set rather than a global denylist."""+ conjuncts = _conjuncts(_plan({"filter[created_by][contains]": "ops"}))++ assert conjuncts == (Compare(field="created_by", op="contains", value="ops"),)+++def test_a_string_that_is_not_an_operator_at_all_is_an_unknown_parameter():+ """`gt3` is a typo, not an operator the field withheld, so the useful reply is the+ parameter list rather than this field's operator set."""+ problem = _problem({"filter[max_budget][gt3]": "5"})++ assert problem.type == f"{PROBLEM_TYPE_BASE}unknown-query-parameter"+ assert problem.allowed is not None+ assert "filter[max_budget][gte]" in problem.allowed+++# ---------------------------------------------------------------- invariant 9+++def test_search_against_a_spec_with_nothing_searchable_is_rejected():+ """A silently-empty search filter returns the unfiltered table, which reads as+ "no results were filtered out" rather than "this resource cannot be searched"."""+ problem = _problem({"q": "prod"}, spec=_spec(searchable=frozenset()))++ assert problem.status == 400+ assert problem.type == f"{PROBLEM_TYPE_BASE}unknown-query-parameter"+ assert problem.allowed is not None+ assert "q" not in problem.allowed+++def test_search_is_a_case_insensitive_or_across_every_searchable_field():+ conjuncts = _conjuncts(_plan({"q": "Prod"}))++ assert conjuncts == (+ AnyOf(+ clauses=(+ Compare(field="budget_id", op="contains", value="Prod"),+ Compare(field="created_by", op="contains", value="Prod"),+ )+ ),+ )+++def test_an_empty_search_string_adds_no_filter():+ assert _plan({"q": ""}).where == ()+++# --------------------------------------------------------------- invariant 10+++def test_multi_key_sort_parses_the_json_api_grammar():+ order = _plan({"sort": "-created_at,budget_id,-max_budget"}).order++ assert order[:3] == (+ SortKey(field="created_at", descending=True),+ SortKey(field="budget_id", descending=False),+ SortKey(field="max_budget", descending=True),+ )+++def test_sort_segments_tolerate_surrounding_whitespace():+ assert _plan({"sort": "-created_at, budget_id"}).order[:2] == (+ SortKey(field="created_at", descending=True),+ SortKey(field="budget_id", descending=False),+ )+++# ------------------------------------------------------------- filter parsing+++def test_comparison_operators_become_prisma_range_fragments():+ conjuncts = _conjuncts(_plan({"filter[max_budget][gte]": "5", "filter[max_budget][lte]": "50"}))++ assert conjuncts == (+ Compare(field="max_budget", op="gte", value=5.0),+ Compare(field="max_budget", op="lte", value=50.0),+ )+++def test_eq_is_a_bare_value_not_a_wrapped_one():+ assert _conjuncts(_plan({"filter[tpm_limit][eq]": "100"})) == (Compare(field="tpm_limit", op="eq", value=100),)+++def test_a_filter_with_no_operator_bracket_means_eq():+ """`filter[status]=active` is the design doc's canonical spelling for equality;+ only the non-eq operators carry a second bracket."""+ assert _conjuncts(_plan({"filter[tpm_limit]": "100"})) == (Compare(field="tpm_limit", op="eq", value=100),)+++def test_the_bare_form_and_the_explicit_eq_form_agree():+ assert _plan({"filter[created_by]": "alice"}) == _plan({"filter[created_by][eq]": "alice"})+++def test_the_bare_form_still_coerces_to_the_declared_type():+ assert _problem({"filter[tpm_limit]": "1.5"}).status == 400+++def test_the_bare_form_is_rejected_on_a_field_that_does_not_declare_eq():+ """The shorthand is sugar for the eq operator, not a bypass around the operator set."""+ problem = _problem({"filter[created_at]": "2026-07-23T00:00:00Z"})++ assert problem.type == f"{PROBLEM_TYPE_BASE}unsupported-filter-operator"+ assert problem.allowed == ["gte", "lte"]+++def test_the_allowed_set_advertises_the_bare_spelling_for_eq():+ allowed = _problem({"nope": "1"}).allowed++ assert allowed is not None+ assert "filter[max_budget]" in allowed+ assert "filter[max_budget][eq]" not in allowed+ assert "filter[created_at][gte]" in allowed+ assert "filter[created_at]" not in allowed+++ "name",+ ["filter[]", "filter[a][b][c]", "filter[a][", "filter", "filter[a][gte", "filter[max_budget]]["],+ ids=["empty", "triple", "unbalanced", "bare-word", "unterminated", "bracketed-field"],+)+def test_malformed_filter_keys_are_unknown_parameters_not_eq_filters(name):+ """A malformed key must not fall through to the bare-eq branch and silently filter+ on a field nobody declared. `field in spec.filters` is the gate that makes this hold,+ which is also why the parser needs no separate well-formedness guard."""+ assert _problem({name: "x"}).type == f"{PROBLEM_TYPE_BASE}unknown-query-parameter"+++def test_in_splits_on_commas_and_coerces_every_member():+ assert _conjuncts(_plan({"filter[created_by][in]": "alice, bob"})) == (+ Within(field="created_by", values=("alice", "bob")),+ )+++def test_is_null_true_matches_rows_with_no_budget():+ """Budgets renders a null max_budget as "Unlimited"; without is_null there is no way+ to ask for those rows."""+ assert _conjuncts(_plan({"filter[max_budget][is_null]": "true"})) == (+ IsNull(field="max_budget", negated=False),+ )+++def test_is_null_false_matches_rows_that_have_one():+ assert _conjuncts(_plan({"filter[max_budget][is_null]": "false"})) == (+ IsNull(field="max_budget", negated=True),+ )+++def test_is_null_rejects_a_non_boolean():+ assert _problem({"filter[max_budget][is_null]": "maybe"}).status == 400+++ "params",+ [+ {"filter[max_budget][gte]": "lots"},+ {"filter[tpm_limit][eq]": "1.5"},+ {"filter[created_at][gte]": "yesterday"},+ {"filter[created_by][in]": "alice,"},+ ],+ ids=["float", "int", "datetime", "in-member"],+)+def test_a_value_that_does_not_match_the_declared_type_is_rejected(params):+ numeric_in = _spec_with(filters={**_spec().filters, "created_by": FilterSpec(type=int, ops=frozenset({"in"}))})+ target = numeric_in if "filter[created_by][in]" in params else _spec()++ assert _problem(params, spec=target).status == 400+++def test_a_datetime_filter_is_normalised_to_utc():+ """The dashboard sends both offset-bearing and naive timestamps; reading a naive one+ as server-local time would shift the window off what the table is showing."""+ with_offset = _conjuncts(_plan({"filter[created_at][gte]": "2026-07-23T02:00:00+02:00"}))+ naive = _conjuncts(_plan({"filter[created_at][gte]": "2026-07-23 00:00:00"}))++ assert with_offset == (Compare(field="created_at", op="gte", value=datetime(2026, 7, 23, tzinfo=timezone.utc)),)+ assert naive == with_offset+++def test_filters_are_ordered_deterministically():+ """Two requests differing only in query-string order must plan identically, or the+ plan stops being a comparable value."""+ forwards = _plan({"filter[created_by][eq]": "a", "filter[max_budget][gte]": "5"})+ backwards = _plan({"filter[max_budget][gte]": "5", "filter[created_by][eq]": "a"})++ assert forwards == backwards+++# ------------------------------------------------------------------- envelope+++async def test_returns_the_page_mode_envelope():+ executor = RecordingExecutor(+ rows=tuple(BudgetRow(budget_id=f"b{i}", max_budget=float(i), created_by="u") for i in range(10)),+ total_count=42,+ )++ response = await handle_list(+ spec=_spec(), executor=executor, request=_request("page=2&page_size=5"), caller=CALLER+ )+ body = response.model_dump(by_alias=True)++ assert body["meta"] == {"total_count": 42, "page": 2, "page_size": 5, "total_pages": 9}+ assert set(body) == {"data", "meta", "links"}+ assert "has_more" not in body["meta"]+++async def test_serializes_rows_flat_without_a_json_api_resource_wrapper():+ executor = RecordingExecutor(rows=(BudgetRow(budget_id="b1", max_budget=None, created_by="u"),))++ response = await handle_list(spec=_spec(), executor=executor, request=_request(), caller=CALLER)+ body = response.model_dump(by_alias=True)++ assert body["data"] == [{"budget_id": "b1", "max_budget": None}]+ assert "attributes" not in body["data"][0]+ assert "created_by" not in body["data"][0]+++async def test_links_let_a_client_page_without_building_urls():+ executor = RecordingExecutor(rows=(), total_count=42)++ response = await handle_list(+ spec=_spec(), executor=executor, request=_request("page=2&page_size=5"), caller=CALLER+ )+ links = response.model_dump(by_alias=True)["links"]++ assert links["self"] == f"{BUDGETS_PATH}?page_size=5&page=2"+ assert links["first"] == f"{BUDGETS_PATH}?page_size=5&page=1"+ assert links["prev"] == f"{BUDGETS_PATH}?page_size=5&page=1"+ assert links["next"] == f"{BUDGETS_PATH}?page_size=5&page=3"+ assert links["last"] == f"{BUDGETS_PATH}?page_size=5&page=9"+++async def test_the_last_page_has_no_next_link():+ executor = RecordingExecutor(rows=(), total_count=10)++ response = await handle_list(+ spec=_spec(), executor=executor, request=_request("page=2&page_size=5"), caller=CALLER+ )+ links = response.model_dump(by_alias=True)["links"]++ assert links["next"] is None+ assert links["prev"] == f"{BUDGETS_PATH}?page_size=5&page=1"+++async def test_an_empty_result_set_still_resolves_every_link():+ executor = RecordingExecutor(rows=(), total_count=0)++ response = await handle_list(spec=_spec(), executor=executor, request=_request(), caller=CALLER)+ body = response.model_dump(by_alias=True)++ assert body["data"] == []+ assert body["meta"]["total_pages"] == 0+ assert body["links"]["first"] == body["links"]["last"] == f"{BUDGETS_PATH}?page=1"+ assert body["links"]["next"] is None+ assert body["links"]["prev"] is None+++async def test_the_executor_counts_the_same_predicate_it_reads():+ """Counting a wider predicate than the read inflates total_pages and hands the UI+ pages that are always empty."""+ executor = RecordingExecutor(rows=(), total_count=3)+ spec = _spec(scope=lambda caller: ScopeWhere(where=(Compare(field="created_by", op="eq", value=caller.user_id),)))++ await handle_list(spec=spec, executor=executor, request=_request("filter[max_budget][gte]=5"), caller=CALLER)++ assert executor.plan is not None+ assert executor.count_where == executor.plan.where+++async def test_a_rejected_request_is_raised_as_a_problem_before_any_query():+ executor = RecordingExecutor(rows=())++ with pytest.raises(ManagementProblem) as raised:+ await handle_list(spec=_spec(), executor=executor, request=_request("sort=api_key"), caller=CALLER)++ assert raised.value.problem.status == 400+ assert executor.count_where is None+++# ------------------------------------------------------- spec construction+++def test_a_default_page_size_above_the_cap_is_rejected_at_construction():+ """The cap is only enforced on a supplied page_size, so a default above it would serve+ more rows than the resource allows on exactly the request that omits page_size."""+ with pytest.raises(ValueError, match="default_page_size"):+ _spec_with(default_page_size=200, max_page_size=100)+++ "overrides",+ [{"default_page_size": 0}, {"default_page_size": -5}, {"max_page_size": 0}],+ ids=["zero-default", "negative-default", "zero-cap"],+)+def test_a_non_positive_page_size_is_rejected_at_construction(overrides):+ """take=0 divides by zero when handle_list computes total_pages, so the resource would+ 500 on every request instead of failing when it is registered."""+ with pytest.raises(ValueError, match="default_page_size"):+ _spec_with(**overrides)+++def test_a_default_sort_on_a_non_sortable_field_is_rejected_at_construction():+ """Caller-supplied sort is validated against `sortable`; default_sort is not read from+ the request, so without this it reaches order_by_sql and yields invalid SQL."""+ with pytest.raises(ValueError, match="default_sort"):+ _spec_with(default_sort=(SortKey(field="not_a_column", descending=True),))+++def test_an_empty_tiebreaker_is_rejected_at_construction():+ with pytest.raises(ValueError, match="tiebreaker"):+ _spec_with(tiebreaker="")+++def test_a_page_size_equal_to_the_cap_is_a_valid_spec():+ """Guards the bound against being tightened into an off-by-one that bans max==default."""+ assert _spec_with(default_page_size=100, max_page_size=100).default_page_size == 100+++# ------------------------------------------------------ repeated parameters+++async def test_a_repeated_query_parameter_is_rejected():+ """Starlette keeps the last value, so ?page=1&page=999 would page from 999 with nothing+ telling the caller which one won. The doc rejects silently-altered params for this reason."""+ executor = RecordingExecutor(rows=())++ with pytest.raises(ManagementProblem) as raised:+ await handle_list(spec=_spec(), executor=executor, request=_request("page=1&page=999"), caller=CALLER)++ assert raised.value.problem.status == 400+ assert raised.value.problem.type == f"{PROBLEM_TYPE_BASE}duplicate-query-parameter"+ assert "page" in raised.value.problem.detail+ assert executor.count_where is None+++async def test_a_repeated_filter_parameter_is_rejected():+ executor = RecordingExecutor(rows=())++ with pytest.raises(ManagementProblem) as raised:+ await handle_list(+ spec=_spec(),+ executor=executor,+ request=_request("filter[created_by][eq]=alice&filter[created_by][eq]=bob"),+ caller=CALLER,+ )++ assert raised.value.problem.type == f"{PROBLEM_TYPE_BASE}duplicate-query-parameter"+ assert "filter[created_by][eq]" in raised.value.problem.detail+++async def test_distinct_parameters_are_not_treated_as_duplicates():+ """Guards the check against rejecting two different operators on one field, which is+ how a range filter is expressed."""+ executor = RecordingExecutor(rows=(), total_count=0)++ response = await handle_list(+ spec=_spec(),+ executor=executor,+ request=_request("filter[max_budget][gte]=5&filter[max_budget][lte]=50&page=2"),+ caller=CALLER,+ )++ assert response.meta.page == 2+ assert executor.count_where is not None+++async def test_a_denied_scope_outranks_a_duplicate_parameter():+ """Permission is the stronger statement about the caller, so it is answered first."""+ spec = _spec(scope=lambda caller: ScopeDenied(reason="nope"))+ executor = RecordingExecutor(rows=())++ with pytest.raises(ManagementProblem) as raised:+ await handle_list(spec=spec, executor=executor, request=_request("page=1&page=2"), caller=CALLER)++ assert raised.value.problem.status == 403+++# --------------------------------------------------- facet-mode regression+++def test_the_facet_page_shapes_are_untouched_by_page_mode():+ """The live facet endpoint reports `has_more` and has no first/last, because it+ deliberately skips the COUNT(*). Folding it into the page-mode shapes would either+ break its response or make every keystroke pay for a full-table count."""+ assert set(PageMeta.model_fields) == {"page", "page_size", "has_more"}+ assert set(PageLinks.model_fields) == {"self_link", "prev", "next"}++ links = build_page_links(request=_request("q=ac&page=2"), page=2, has_more=True).model_dump(by_alias=True)++ assert set(links) == {"self", "prev", "next"}+ assert links["next"] == "/management/v1/budgets?q=ac&page=3"