Skip to content

Commit dd9921e

Browse files
fix(rest)!: clone refuses a body that carries overrides beside other top-level keys (#27934)
<!-- claude-projects-attribution: {"github_login":"os-elon-musk"} --> _Requested by **Elon** · [project thread](https://claude.ai/code/project/chan_01WedDzV26nniwxv7jC88B9D?thread=cmsg_01WedDzV26nniwxv7jC88B9DHZ7P4hcXjQQUXjv2w7ACfa)_ Fixes #27669 Clause-②: no (narrowing) Metadata-protocol: none Skills-docs: none — no skill teaches the clone door's body shapes, and `content/docs/api/data-api.mdx` describes only the two accepted shapes, which are unchanged ## 维护者速读 - **改了什么**:克隆接口 `POST /data/OBJ/ID/clone` 的请求体若同时带 `overrides` 和其它顶层键(例如 `{"overrides":{"name":"Copy"},"status":"draft"}`),现在返回 400,不再只读 `overrides`、悄悄丢掉 `status` 并返回 201。 - **为什么改**:调用方以为两个值都生效了,实际只生效一半;与 #27489 修过的「静默丢弃」同一类。 - **风险与回滚**:风险是混用两种请求体形状、曾「侥幸成功」的调用方现在收到 400;单独的 `{"overrides":{...}}`、不带 `overrides` 键的裸字段表、`{}`、无 body 行为不变。回滚即删掉克隆处理器里的这一段守卫。 - **先看**:`packages/rest/src/data-action-routes.ts`(克隆处理器的 `strays` 守卫)、`packages/rest/src/clone-overrides-refusal.test.ts`(新的 `[#27669]` 一组)。 - **两分钟验证**:`pnpm --filter @objectstack/rest exec vitest run src/clone-overrides-refusal.test.ts`,预期 19 条全部通过。 - **需要你做的**:无,席位复核与合同复审后落地。 ## What changes - **The guard.** In the clone handler of `packages/rest/src/data-action-routes.ts`, when the body is a field map that carries `overrides` (the nested shape), any other top-level key is a stray. One or more strays answer `400` through the same nested-envelope writer #27659 uses (`sendEnvelopeError`, `error.code: 'VALIDATION_ERROR'`), before `cloneData` is called. The message names each stray key, names both accepted shapes (`{ "overrides": {...} }` or a bare field map), and tells a caller whose record has a real field named `overrides` to nest it: `{ "overrides": { "overrides": ... } }`. That is the triage ruling; no new shape is added. - **Order.** The stray check runs before the #27489 non-object check, so `{"overrides":"x","status":"draft"}` is refused for its stray key first; both are `400 VALIDATION_ERROR` before `cloneData`. - **Unchanged.** The nested shape alone, a bare field map with no `overrides` key, `{}`, a bare `null` body and no body clone exactly as before. The SDK's `data.clone` sends only the nested shape. - **Changeset.** `@objectstack/rest` patch, BREAKING banner, the ADR-0087 disposition `not-required (no-migration-prescription)` and the remedy, modelled on #27659's. ## Tests `packages/rest/src/clone-overrides-refusal.test.ts` (the #27659 harness, real `HonoHttpServer`) gains a `[#27669]` block: - Refusal: one stray key, two stray keys, and an empty `overrides` with a stray key. Each asserts `400`, `VALIDATION_ERROR`, every stray key named, both shapes named, the nested-`overrides` prescription, and that `cloneData` was never called. - Preservation: `{"overrides":{"overrides":"x","name":"Copy"}}` answers `201` and `cloneData` receives `{ overrides: 'x', name: 'Copy' }`. - The existing 15 #27489 cases are untouched and green. ## Verification <details><summary>Pin on main, then the fix, then an ablation</summary> - Pin first: the `[#27669]` block was committed alone on top of `main` `b13a1ed3` (commit `b276331d`). `vitest run src/clone-overrides-refusal.test.ts`: 3 failed, 16 passed, each `expected 201 to be 400`. - With the fix (`e2d1eb18`): 19 passed (19). - Ablation through `node scripts/ablation-replace.mjs`, anchor `if (strays.length > 0) {` replaced by `if (strays.length < 0) {`: anchor 1 → 0, blob `327a925b243e` → `71a31169d8a4`; result 3 failed, 16 passed. Restored: blob after restore `327a925b243e` == HEAD, `git diff HEAD` empty. The test imports `./rest-server` from `src/`, so no `dist/` leg applies. </details> <details><summary>Package tests and typecheck: @objectstack/rest green at the final head</summary> - At `9d859e3a` (after one merge of `origin/main`, install and closure rebuild): `pnpm --filter @objectstack/rest test` exit 0, Test Files 348 passed (348), Tests 6754 passed and 332 skipped. - `pnpm --filter @objectstack/rest typecheck` at `e2d1eb18`: exit 0, `check:test-typecheck` OK with 0 files / 0 errors in the debt ledger. </details> <details><summary>Derived gate families: 59 derived, 58 exit 0, 1 NOT MEASURED; pnpm lint exit 0</summary> - `node scripts/pm/dispatch-gates.mjs --commands` derived 59 commands at `e2d1eb18`, each run with its exit code captured before any pipe. 58 exited 0 (`check-plugin-teardown-shape --self-test` after fetching its pinned commit into the shallow clone). - `pnpm check:dual-build-cjs-loads` exited 3, PREREQUISITE NOT MET (it needs every package's `dist/`): NOT MEASURED locally; CI's `Build Core` job runs it. - `--ran` reconciliation: 59 derived famil(ies) accounted for. - `pnpm lint`: exit 0. - The merge of `origin/main` changed none of this PR's three files' other regions; `data-action-routes.ts` auto-merged. </details> <details><summary>Dogfood battery (owed by the narrowing arm): green at the final head</summary> - At `9d859e3a`: `pnpm turbo run build --filter='@objectstack/dogfood^...'` then `pnpm --filter @objectstack/dogfood test`, exit 0. Test Files 357 passed, 3 skipped (360). Tests 2875 passed, 20 skipped (2895). Not run under `os-verify-lock` (one worktree on this box). - An earlier full run at `e2d1eb18` (before the merge) had 4 failures, none on the clone door: three were the stale hook-abort pins tracked as #27806 (fixed on `main` by #27878), and one was `platform-app-object-entry-views.test.ts` naming `sys_exchange_rate`. All four pass at `9d859e3a`. </details> ## Reviewer notes - Contract review is owed (narrowing) and is arranged by the seat. - Serial note: PR #27795 and the #27728/#27729 work edit other regions of the same file; this diff stays inside the clone handler. --- _Generated by [Claude Code](https://claude.ai/code/session_016WV4czA6HpNWK4YvhLTw3x)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016WV4czA6HpNWK4YvhLTw3x --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent bacb3e6 commit dd9921e

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@objectstack/rest': patch
3+
---
4+
5+
fix(rest)!: the clone door refuses a body that carries `overrides` beside other top-level keys, instead of cloning with `overrides` alone (#27669)
6+
7+
Clause-②: no (narrowing) — `POST /data/:object/:id/clone` refuses a body that mixes the nested `{ "overrides": {...} }` shape with other top-level keys; it used to answer `201` with those keys ignored. Nothing is widened.
8+
Metadata-protocol: none
9+
10+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export or type changes: one REST door now refuses a request shape whose extra keys it used to ignore. There is nothing for a migration to rewrite and no tombstone. -->
11+
12+
**BREAKING** accept-set narrowing at one REST data door (`@objectstack/rest`).
13+
14+
- **Before.** `{ "overrides": { "name": "Copy" }, "status": "draft" }` read `overrides` alone: the clone got the new name, `status` was dropped, and the door answered `201`.
15+
- **Now.** A body that carries `overrides` beside any other top-level key answers `400` with `error.code: 'VALIDATION_ERROR'` before the clone runs, naming the stray keys and the two accepted shapes.
16+
- **Unchanged.** `{ "overrides": {...} }` alone, a bare field map without an `overrides` key, `{}`, and no body at all clone exactly as before. The SDK's `data.clone` sends only the nested shape.
17+
- **Who sees it.** An API caller or script that mixes the two body shapes. Such a request used to succeed while applying only part of what was sent.
18+
19+
**The remedy.** Send either `{ "overrides": {...} }` or a bare field map, not both. A record with a real field named `overrides` sets it through the nested shape: `{ "overrides": { "overrides": ... } }`.

‎packages/rest/src/clone-overrides-refusal.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,35 @@ describe('[#27489] clone — object overrides and absent bodies keep their answe
9898
expect(out.cloneData.mock.calls[0][0]).toMatchObject({ object: 'account', id: 'a1' });
9999
});
100100
});
101+
102+
/**
103+
* [#27669] A body that carries `overrides` beside other top-level keys is
104+
* refused, instead of reading `overrides` alone and dropping the rest with a
105+
* `201`. The refusal names the stray keys and both accepted shapes, and points
106+
* a record with a real field named `overrides` at the nested shape.
107+
*/
108+
describe('[#27669] clone — overrides beside other top-level keys is refused before cloneData', () => {
109+
it.each([
110+
['one stray key', '{"overrides":{"name":"Copy"},"status":"draft"}', ['"status"']],
111+
['two stray keys', '{"overrides":{"name":"Copy"},"status":"draft","owner":"u2"}', ['"status"', '"owner"']],
112+
['an empty overrides with a stray key', '{"overrides":{},"status":"draft"}', ['"status"']],
113+
])('%s → 400 VALIDATION_ERROR naming the stray keys and both shapes, cloneData never called', async (_label, body, strays) => {
114+
const out = await clone(body);
115+
116+
expect(out.status).toBe(400);
117+
expect(out.body?.error?.code).toBe('VALIDATION_ERROR');
118+
const message: string = out.body?.error?.message ?? '';
119+
for (const key of strays) expect(message).toContain(key);
120+
expect(message).toContain('{ "overrides": {...} }');
121+
expect(message).toContain('a bare field map');
122+
expect(message).toContain('{ "overrides": { "overrides": ... } }');
123+
expect(out.cloneData).not.toHaveBeenCalled();
124+
});
125+
126+
it('a field named overrides set through the nested shape → 201, cloneData receives it', async () => {
127+
const out = await clone('{"overrides":{"overrides":"x","name":"Copy"}}');
128+
129+
expect(out.status).toBe(201);
130+
expect(out.cloneData.mock.calls[0][0]).toMatchObject({ overrides: { overrides: 'x', name: 'Copy' } });
131+
});
132+
});

‎packages/rest/src/data-action-routes.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ export function registerDataActionEndpoints(this: DataActionRouteHost, basePath:
374374
// object's `enable.clone` capability, default on). Optional JSON body
375375
// `{ overrides?: {...} }` (or a bare field map) is applied on top of
376376
// the copied values, e.g. to set a new name or clear a unique field.
377-
// Any other `overrides` (or bare body) is a 400.
377+
// Any other `overrides` (or bare body), or `overrides` beside other
378+
// top-level keys, is a 400.
378379
// Distinct path segment (`/clone`) keeps it clear of the greedy
379380
// `/data/:object/:id` matchers.
380381
this.routeManager.register({
@@ -397,6 +398,21 @@ export function registerDataActionEndpoints(this: DataActionRouteHost, basePath:
397398
// callers can POST overrides directly without nesting.
398399
const nested = isFieldMap(body) && 'overrides' in body;
399400
const overrides = nested ? body.overrides : body;
401+
// [#27669] Refused, not dropped: `overrides` beside other
402+
// top-level keys used to clone with `overrides` alone and
403+
// answer `201`, the other keys silently ignored. A record
404+
// with a real field named `overrides` sets it through the
405+
// nested shape.
406+
if (nested) {
407+
const strays = Object.keys(body).filter((key) => key !== 'overrides');
408+
if (strays.length > 0) {
409+
sendEnvelopeError(res, 400, 'VALIDATION_ERROR',
410+
`The clone body carries "overrides" beside other keys (${strays.map((key) => `"${key}"`).join(', ')}). `
411+
+ 'Send either { "overrides": {...} } or a bare field map, not both. '
412+
+ 'To set a field named "overrides", nest it: { "overrides": { "overrides": ... } }.');
413+
return;
414+
}
415+
}
400416
// [#27489] Refused, not dropped: a non-object `overrides`
401417
// (`"x"`, `1`, `true`, `null`) used to be skipped and answer
402418
// `201` with a plain copy, and an array passed the `typeof`

0 commit comments

Comments
 (0)