Repository navigation
Commit 4c2d8d7
fix(runtime)!: an analytics query that selects nothing answers 400 VALIDATION_FAILED at the entry (#35771)
<!-- claude-projects-attribution: {"github_login":"os-tesla"} -->
_Requested by **Tesla** · [project
thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6Jao6bxiPKCvVuzoFTExumbxoz)_
Fixes #33840
Clause-②: no (narrowing)
Metadata-protocol: none
Skills-docs: none — nothing under skills/ covers analytics/query
projection or an empty measures
## 维护者速读
- 用户能看到什么变化:调用分析查询接口却既没选指标也没选维度的调用方,现在立刻收到“请至少选择一个指标或维度”的 400
提示,而不是服务器内部错误 500;预览生成语句的接口同样拒绝,不再回显一条坏语句。
- 为什么做:来自质检回归发现的仪表板查询负例,修补分析查询入口校验的缺口,让调用方的输入错误不再被报成服务器故障。
- 出错会怎样:属破坏性收窄(BREAKING):此前预览生成语句的接口对这种空查询回 200,现在回 400;最坏情况是把本来合法的查询误拒为
400,调用方会立刻看到;改动只在入口多一条校验,可直接撤回。
- 要你做什么:这是申报的收窄,按 #35610 需要你在 GitHub 上点一次 Approve;之后席位自主落地。
- 席位意见:通过,没有必改项(席位记录见本 PR 评论)。
## What changes
`POST /api/v1/analytics/query` with `measures: []` and no dimensions
passed the entry check (the schema accepts an empty `measures` because a
dimension-only query is legal, and it has no cross-field rule), reached
the native SQL strategy, which assembled `SELECT FROM "TABLE"`, and
answered `500 DATABASE_ERROR`. The `/analytics/sql` door echoed the same
statement as a 200.
`assertAnalyticsQueryBody` (runtime,
`packages/runtime/src/domains/analytics.ts`) now runs one more rule
after the schema parse succeeds: a query that selects no measure, no
dimension and no `timeDimensions` entry carrying a `granularity` is
refused with the existing `400 VALIDATION_FAILED` and one
`details.fields[]` entry `{ field: 'measures', code: 'min_items' }`
whose message tells the caller to select at least one measure or
dimension. Both `/analytics/query` and `/analytics/sql` call this one
function, so the `/sql` echo is covered by the same guard with no extra
code. The body is still forwarded untouched when it passes.
What counts as a projection follows what the strategies group or
aggregate by: a measure, a dimension, or a bucketed time dimension (the
ObjectQL strategy turns each granularity into a group key). A time
dimension with only a `dateRange` is a filter and selects nothing on its
own, so it does not rescue an empty projection.
No spec change, no new error code (`VALIDATION_FAILED` and the
`min_items` field code are existing catalog members). Changeset:
`@objectstack/runtime` patch.
**BREAKING (narrowing).** `AnalyticsQuerySchema` admits `measures: []`
with no minimum, so the refused body was inside the published accept
set, and `/analytics/sql` answered it with a 200 before this change.
Where the engine refused such a body before choosing a strategy, that
refusal now becomes the same 400 at `measures`, measured on the engine
at this head for both strategies and both doors: an unknown or `public:
false` cube (`404 CUBE_NOT_FOUND`), a cube over an object the caller may
not read (`403 PERMISSION_DENIED`), and an `order` key naming no
selected member (`400 INVALID_FIELD`). On a deployment the ObjectQL
strategy serves, `/analytics/query` answered a 200 with one row that
carries no column. Declared `Clause-②: no (narrowing)`, as the
dataset-door twin was; the changeset carries the FROM/TO, the caller's
one-line fix and the ADR-0087 disposition `not-required
(no-migration-prescription)`: no schema key, export, stored row or
authored value moves.
**Read first (by risk):**
1. `packages/runtime/src/domains/analytics.ts`, the new
`assertAnalyticsQuerySelectsSomething` and its call at the end of
`assertAnalyticsQueryBody` (the risk hunk: the bucketed time-dimension
exemption, which keeps a granularity-only query reaching the engine).
2. `packages/runtime/src/analytics-empty-projection-refusal.test.ts`
(refusal and preservation pins on both doors).
**Two-minute check:** `pnpm --filter @objectstack/runtime exec vitest
run --maxWorkers=2 src/analytics-empty-projection-refusal.test.ts` shows
20 passed.
## Tests
New `packages/runtime/src/analytics-empty-projection-refusal.test.ts`,
driving the mounted routes through `dispatcher-plugin` with the harness
of the sibling empty-cube test, on both `/analytics/query` and
`/analytics/sql`:
- refusal: `{measures: []}`, `{measures: [], dimensions: []}`, and
`{measures: []}` with a date-range-only time dimension each answer 400,
`error.code` `VALIDATION_FAILED`, `error.httpStatus` 400, one field
entry at `measures` with code `min_items`, and the service is never
called;
- the envelope parses against `ApiErrorSchema`;
- preservation: one measure and no dimension, a dimension-only query,
and a bucketed time dimension alone each reach the service with the body
untouched;
- precedence: an unknown cube with an empty projection gets the 400 at
`measures` and never reaches a stub engine that answers `CUBE_NOT_FOUND`
/ 404 for that cube; control: the same cube with one measure reaches it
and answers the 404; and `where`, `order` and `limit` with an empty
projection are still refused.
<details><summary>Readings (head d8fc051 unless noted)</summary>
- new file + sibling empty-cube test at d8fc051: `Test Files 2 passed
(2)`, `Tests 24 passed (24)`, lock VERDICT command-exit 0.
- `pnpm --filter @objectstack/runtime test` at 5aeaf59 (before the
last commit, which only adds a module-top side-effect import to the new
test): `Test Files 633 passed (633)`, `Tests 11669 passed | 25 skipped
(11694)`, lock VERDICT command-exit 0.
- `pnpm --filter @objectstack/runtime typecheck` at 5aeaf59: exit 0
(tsc and check:test-typecheck OK).
- Ablation (fix committed first; `scripts/ablation-replace.mjs` deleted
the call line, anchor 1 to 0, blob 2fa8f93 to 614cfcb88): `Tests 8
failed | 6 passed (14)`, the 8 being the refusal and envelope cases on
both doors, the 6 the preservation cases; restore verified blob equals
HEAD and `git diff HEAD` empty. This red run is also the door-level
reproduction: without the rule the empty projection reaches the service.
- Round 2 at d229284: the pin file has 20 tests, `Test Files 1 passed
(1)` / `Tests 20 passed (20)`, VERDICT command-exit 0; runtime typecheck
exit 0. Ablation again (call line deleted, blob 2fa8f93 to 614cfcb88):
`Tests 12 failed | 8 passed (20)`, the 12 being every refusal, envelope
and precedence case on both doors, the 8 being the preservation cases
and the 404 controls; restored to the HEAD blob with `git diff HEAD`
empty.
- Engine readings behind the BREAKING sentence, from a scratch test
against `AnalyticsService` (deleted, not committed), body `measures:
[]`, on `query` and `generateSql` under each of the native SQL and
ObjectQL strategies: unknown cube and `public: false` cube gave
`CUBE_NOT_FOUND` 404; a walled object gave `PERMISSION_DENIED` 403;
`order` on an unselected key gave `INVALID_FIELD` 400; the plain empty
projection gave `SELECT FROM` on `generateSql`, and on `query` a 200
with one columnless row under the ObjectQL strategy.
</details>
## Acceptance notes
- The live 500 was not re-measured on a booted showcase in this run; the
card thread carries six live reproductions at f6f5b50 (QA run, comment
6946115523), and the door-level reproduction above shows the unfixed
entry forwards the body.
- `/analytics/sql`: covered by the same guard with no extra code (both
doors call `assertAnalyticsQueryBody`).
- The engine itself (`native-sql-strategy`) still builds an empty SELECT
list for a caller that reaches it in-process without this door; not
changed here (the card's route is the runtime door).
## Verification
<details><summary>Local gate subset</summary>
`node scripts/pm/dispatch-gates.mjs --commands` derived 68 families for
this diff; all 68 run locally, reconciled with `--ran`: "68 derived
famil(ies) accounted for, 68 run, 0 NOT-MEASURED (a DERIVED zero)". Two
needed a second run: `check:dual-build-cjs-loads` first answered
PREREQUISITE NOT MET (no full build yet) and then exit 0 once the
type-check-debt re-measure had built the tree; `check:test-source-alias`
was red on the new test (a metadata load paid inside a clocked window)
and green after the module-top import fix at d8fc051. `node
scripts/pm/overlap-check.mjs` before each push: VERDICT clean exit 0. CI
owns the full farm.
At head 806d7f5 (the changeset re-declaration):
`check-adr-0087-registration --base origin/main` exit 0 ("1
declared-breaking changeset(s), each carrying an ADR-0087 disposition",
BREAKING+bang+clause-②-narrowing, not-required
(no-migration-prescription)); `check-changeset-no-major --base
origin/main` exit 0 ("This diff introduces no `major` bump");
`check-empty-changeset`, `check-closing-keyword-parity`,
`check:doc-authoring`, `check:nul-bytes`, `check:issue-citations`,
`check:changeset-gate-self-tests` exit 0. The code and test files are
unchanged since d8fc051.
Round 2 at d229284 (test file and changeset only): 66 of the 68
derived families run, all exit 0; `--ran` reports 2 UNRUN,
`check:type-check-debt` and `check:dual-build-cjs-loads`, which need a
full-tree build this worktree does not have. Both read built output and
compiled types, which this round does not change; CI runs both.
</details>
<details><summary>Full dogfood battery (head 806d7f5)</summary>
Closure built under the lock (`pnpm --filter '@objectstack/dogfood^...'
build`, VERDICT command-exit 0), then `pnpm --filter
@objectstack/dogfood test` with no shard and no path: VERDICT
command-exit 0, `Test Files 482 passed | 3 skipped (485)`, `Tests 4094
passed | 68 skipped (4162)`, no failure. Not re-run at d229284:
`analytics.ts` is unchanged since then, and round 2 changes only the pin
file and the changeset text.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01L9qibwJEh7hMPPvagWM1g7)_
<!-- signed: repo:objectstack#7 · os-tesla ·
session_01L9qibwJEh7hMPPvagWM1g7 -->
<!-- signed: repo:objectstack#7 · os-tesla ·
session_01L9qibvvVzM5KhCWkT5f45c -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent ecd4b2b commit 4c2d8d7
3 files changed
Lines changed: 242 additions & 0 deletions
File tree
- .changeset
- packages/runtime/src
- domains
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
100 | 140 | | |
101 | 141 | | |
102 | 142 | | |
| |||
0 commit comments