From e88ba56b6a7db4fa154098024c14902fc2448436 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Mon, 14 Sep 2026 11:43:05 +0800 Subject: [PATCH] fix(control): serde(flatten) breaks integer query parameters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `/summary/buckets?from_ms=…` returned 400: invalid type: string "1789300000000", expected i64 `#[serde(flatten)]` makes serde deserialize through `deserialize_any`, and in a query string everything arrives as a string — so an `i64` field behind a flattened struct can never parse. The endpoints I added two commits ago both did this. The failure shape is the bad kind: without parameters everything works, and it only breaks **when a time window is supplied** — which the UI always does. So the endpoints looked fine in every check I ran by hand and would have been permanently empty in the chart they exist for. Unit tests structurally cannot see this. It happens only when a request actually passes through query-string deserialization, which is exactly the class of failure `scripts/smoke.sh` exists for — and the smoke script was hitting all these endpoints without parameters. So it now hits four of them *with* a window, and that's the part worth keeping: mutation-verified by putting `flatten` back, which turns the new check red with the 400 in the output. 1066 tests, 46 smoke checks, clippy clean. Co-Authored-By: Claude Opus 5 --- crates/tw-control/src/lib.rs | 41 ++++++++++++++++++++++++++++++------ scripts/smoke.sh | 13 ++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/crates/tw-control/src/lib.rs b/crates/tw-control/src/lib.rs index 6ecc92b5..e937a2bd 100644 --- a/crates/tw-control/src/lib.rs +++ b/crates/tw-control/src/lib.rs @@ -461,7 +461,7 @@ async fn cost_buckets( State(s): State, axum::extract::Query(q): axum::extract::Query, ) -> Result>, Fail> { - let (from, to) = q.win.range(); + let (from, to) = q.range(); // 桶宽有下限,否则一个 `bucket_ms=1` 能让这条查询扫出几百万个分组。 let bucket = q.bucket_ms.unwrap_or(3_600_000).max(1_000); let store = need_store(&s)?; @@ -478,7 +478,7 @@ async fn cost_by( State(s): State, axum::extract::Query(q): axum::extract::Query, ) -> Result>, Fail> { - let (from, to) = q.win.range(); + let (from, to) = q.range(); let store = need_store(&s)?; let g = store.lock().await; let x = g @@ -1110,22 +1110,51 @@ struct Window { to_ms: Option, } +/// **不能用 `#[serde(flatten)]` 摊平 `Window`。** +/// +/// flatten 会让 serde 走 `deserialize_any`,而 query string 里一切都是 +/// 字符串 —— 于是 `from_ms=123` 被当成字符串喂给 `i64`,整个请求 400: +/// `invalid type: string "123", expected i64`。 +/// +/// 表现很坏:不带参数时一切正常,只有在**同时带上时间窗**的时候才失败, +/// 而界面恰恰总是带着它调。单元测试看不见这件事 —— 它只在真的经过一次 +/// query string 解析时才发生。 #[derive(serde::Deserialize)] struct BucketQuery { - #[serde(flatten)] - win: Window, + from_ms: Option, + to_ms: Option, bucket_ms: Option, } +impl BucketQuery { + fn range(&self) -> (i64, i64) { + Window { + from_ms: self.from_ms, + to_ms: self.to_ms, + } + .range() + } +} + #[derive(serde::Deserialize)] struct GroupQuery { - #[serde(flatten)] - win: Window, + from_ms: Option, + to_ms: Option, /// **是枚举不是字符串。**它会决定 SQL 里的列名,用字符串就是一个 /// 注入口;写错的值在这里被 serde 直接拒掉,而不是拼进查询。 dim: tw_api::CostDim, } +impl GroupQuery { + fn range(&self) -> (i64, i64) { + Window { + from_ms: self.from_ms, + to_ms: self.to_ms, + } + .range() + } +} + impl Window { fn range(&self) -> (i64, i64) { let now = now_ms(); diff --git a/scripts/smoke.sh b/scripts/smoke.sh index 6f61fffb..73eaadbf 100755 --- a/scripts/smoke.sh +++ b/scripts/smoke.sh @@ -255,6 +255,19 @@ get() { curl -s -o "$TMP/out" -w '%{http_code}' --unix-socket "$SOCK" "http://lo post() { curl -s -o "$TMP/out" -w '%{http_code}' --unix-socket "$SOCK" -XPOST \ -H 'content-type: application/json' -d "$2" "http://localhost$1"; } +# **带上时间窗再打一次。**不带参数时一切正常、带上 `from_ms` 就 400, +# 是这两个端点真实发生过的形态:`#[serde(flatten)]` 让 serde 走 +# deserialize_any,而 query string 里一切都是字符串,于是 i64 永远解析 +# 失败。单元测试看不见 —— 它只在真的经过一次 query string 解析时发生, +# 而界面恰恰总是带着时间窗调。 +NOW=$(python3 -c 'import time;print(int(time.time()*1000))') +DAY=$((NOW - 86400000)) +for ep in "/summary?from_ms=$DAY" "/summary/buckets?from_ms=$DAY&bucket_ms=3600000" \ + "/summary/by?dim=model&from_ms=$DAY" "/history?limit=5&from_ms=$DAY"; do + C=$(get "$ep") + [ "$C" = "200" ] && ok "GET ${ep%%\?*}(带时间窗)" || bad "GET $ep 返回 $C" "$(cat "$TMP/out" 2>/dev/null | head -c 200)" +done + for ep in /status /overview /summary /history /latency /latency/provider /storage /quota /leaks \ /clients /scan /sessions /baseline /mcp/targets /diagnostics /config /config/history; do C=$(get "$ep")