Skip to content

Commit 5675303

Browse files
committed
fix(core): interpolate api targets before a registered api handler
A registered `api` handler (the console's) replaces `executeAPI`, the only caller of `interpolateTarget` on the api path, so `${ctx.recordId}` went out literally. The runner now hands the handler an interpolated target through the same substitution and encoding a url target uses. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTDpMW8KaXLg1DFU6f
1 parent c8583f8 commit 5675303

2 files changed

Lines changed: 126 additions & 3 deletions

File tree

‎ui/packages/core/src/actions/ActionRunner.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,19 @@ export class ActionRunner {
16801680
// Check for a registered custom handler first
16811681
if (actionType && this.handlers.has(actionType)) {
16821682
const handler = this.handlers.get(actionType)!;
1683-
return handler(action, this.context);
1683+
// An `api` target is interpolated HERE, by the runner, before a host
1684+
// handler sees it — the same `interpolateTarget` the built-in
1685+
// `executeAPI` and `executeUrl` run. A registered `api` handler (the
1686+
// console's) replaces `executeAPI` wholesale, so without this the spec's
1687+
// documented `${ctx.recordId}` went out on the wire as the literal
1688+
// placeholder while a `url` target with the same token resolved. One
1689+
// substitution, one vocabulary, one encoding, whoever sends the request.
1690+
return handler(
1691+
actionType === 'api' && typeof action.target === 'string'
1692+
? { ...action, target: this.interpolateTarget(action.target, action) }
1693+
: action,
1694+
this.context,
1695+
);
16841696
}
16851697

16861698
// Built-in action execution by type.
@@ -2802,7 +2814,8 @@ export class ActionRunner {
28022814
/**
28032815
* Substitute `${param.X}` and `${ctx.X}` tokens in an action target.
28042816
*
2805-
* Used by executeUrl and executeAPI so metadata can produce dynamic
2817+
* Used by executeUrl and executeAPI — and on an `api` target before a
2818+
* registered `api` handler receives it — so metadata can produce dynamic
28062819
* URLs without coupling to the wider ExpressionEvaluator surface
28072820
* (which would also evaluate operators, member access, etc. — overkill
28082821
* and unsafe for URL-position values).
@@ -2867,7 +2880,8 @@ export class ActionRunner {
28672880
//
28682881
// `result` (the handler's own return value) is passed by exactly one caller:
28692882
// the `ActionSchema.onSuccess` post-success hop, where a response exists.
2870-
// Every other caller — `executeUrl`, `executeAPI` — interpolates a target
2883+
// Every other caller — `executeUrl`, `executeAPI`, the registered `api`
2884+
// handler hand-off in `dispatchCollected` — interpolates a target
28712885
// BEFORE the request, so there is no result to read; admitting `${result.*}`
28722886
// there would widen the authorable surface with nothing behind it and
28732887
// silently blank the token instead of leaving the author's mistake visible.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* An `api` action's `target` is interpolated before a REGISTERED `api` handler
5+
* receives it, through the same substitution a `url` target goes through.
6+
*
7+
* The console registers its own `api` handler, which replaces the built-in
8+
* `executeAPI` wholesale. Only `executeAPI` called `interpolateTarget`, so on a
9+
* record page `target: "/api/v1/data/gapdata_item/${ctx.recordId}"` reached the
10+
* wire as `…/$%7Bctx.recordId%7D` for PATCH and POST alike, while a `url`
11+
* action with the same token opened the resolved address.
12+
*
13+
* Pinned here: what the handler receives for PATCH and POST, that the encoding
14+
* is the url target's (one opaque segment per value), that the built-in
15+
* executor sends the same path, that a `url` action is unchanged, and that a
16+
* non-`api` handler still receives its target as authored.
17+
*/
18+
19+
import { describe, it, expect, vi, afterEach } from 'vitest';
20+
import { ActionRunner, type ActionContext, type ActionDef, type ActionRunnerHandler } from '../ActionRunner';
21+
22+
const RECORD_PAGE: ActionContext = {
23+
record: { id: 'gd_001', name: 'Gap item' },
24+
objectName: 'gapdata_item',
25+
};
26+
27+
const TARGET = '/api/v1/data/gapdata_item/${ctx.recordId}';
28+
29+
function recordingApiHandler() {
30+
const seen: ActionDef[] = [];
31+
const handler: ActionRunnerHandler = async (action) => {
32+
seen.push(action);
33+
return { success: true, silent: true };
34+
};
35+
return { seen, handler };
36+
}
37+
38+
describe('api targets interpolate before a registered api handler (objectstack#26722)', () => {
39+
afterEach(() => {
40+
vi.unstubAllGlobals();
41+
});
42+
43+
it.each(['PATCH', 'POST'])('a %s api action hands the handler the resolved record path', async (method) => {
44+
const runner = new ActionRunner(RECORD_PAGE);
45+
const { seen, handler } = recordingApiHandler();
46+
runner.registerHandler('api', handler);
47+
48+
const result = await runner.execute({ type: 'api', name: 'call_api', target: TARGET, method });
49+
50+
expect(result.success).toBe(true);
51+
expect(seen).toHaveLength(1);
52+
expect(seen[0].target).toBe('/api/v1/data/gapdata_item/gd_001');
53+
expect(seen[0].method).toBe(method);
54+
});
55+
56+
it('encodes each value the way a url target does', async () => {
57+
const context: ActionContext = { record: { id: 'a/b c' }, objectName: 'gapdata_item' };
58+
const runner = new ActionRunner(context);
59+
const { seen, handler } = recordingApiHandler();
60+
runner.registerHandler('api', handler);
61+
const navigate = vi.fn();
62+
runner.setNavigationHandler(navigate);
63+
64+
await runner.execute({ type: 'api', name: 'call_api', target: TARGET, method: 'PATCH' });
65+
await runner.execute({ type: 'url', name: 'open', target: TARGET });
66+
67+
expect(seen[0].target).toBe('/api/v1/data/gapdata_item/a%2Fb%20c');
68+
expect(navigate.mock.calls[0][0]).toContain('/api/v1/data/gapdata_item/a%2Fb%20c');
69+
});
70+
71+
it('the built-in executor sends the same resolved path', async () => {
72+
const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: vi.fn().mockResolvedValue({}) });
73+
vi.stubGlobal('fetch', fetchMock);
74+
const runner = new ActionRunner(RECORD_PAGE);
75+
76+
await runner.execute({ type: 'api', name: 'export_tab', target: TARGET, method: 'POST' });
77+
78+
expect(fetchMock).toHaveBeenCalledWith(
79+
'/api/v1/data/gapdata_item/gd_001',
80+
expect.objectContaining({ method: 'POST' }),
81+
);
82+
});
83+
84+
it('a url action with the same token is unchanged', async () => {
85+
const runner = new ActionRunner(RECORD_PAGE);
86+
const navigate = vi.fn();
87+
runner.setNavigationHandler(navigate);
88+
89+
const result = await runner.execute({ type: 'url', name: 'open', target: TARGET });
90+
91+
expect(result.success).toBe(true);
92+
expect(navigate).toHaveBeenCalledTimes(1);
93+
expect(navigate.mock.calls[0][0]).toContain('/api/v1/data/gapdata_item/gd_001');
94+
expect(navigate.mock.calls[0][0]).not.toContain('${');
95+
});
96+
97+
it('a registered non-api handler still receives its target as authored', async () => {
98+
const runner = new ActionRunner(RECORD_PAGE);
99+
const seen: ActionDef[] = [];
100+
runner.registerHandler('flow', async (action) => {
101+
seen.push(action);
102+
return { success: true, silent: true };
103+
});
104+
105+
await runner.execute({ type: 'flow', name: 'run_flow', target: 'flow_${ctx.recordId}' });
106+
107+
expect(seen[0].target).toBe('flow_${ctx.recordId}');
108+
});
109+
});

0 commit comments

Comments
 (0)