diff --git a/.changeset/33894-connector-pull-undeclared-input-key.md b/.changeset/33894-connector-pull-undeclared-input-key.md new file mode 100644 index 00000000000..2dffd44d535 --- /dev/null +++ b/.changeset/33894-connector-pull-undeclared-input-key.md @@ -0,0 +1,24 @@ +--- +'@objectstack/service-automation': minor +'@objectstack/spec': minor +--- + +fix(service-automation)!: the connector pull door refuses an input key its read action does not declare, as the `connector_action` node does + +Clause-②: no (narrowing) — a mapping whose `connectorSource.input`, or the watermark parameter the pull adds to its `query`, carries a key the read action's `inputSchema` does not declare used to pull and send that key to the provider; the pull now fails before the call. Nothing published widens: no export, no key, no option is added. First-party mappings affected: 0 (no `connectorSource` under `examples/` or `packages/apps/`). +Metadata-protocol: narrowed + + + +**BREAKING** for a mapping pulled through `connectorSource` whose input holds a key the read action does not declare. Every door that dispatches a connector action now judges its input alike: the pull judges the input it is about to send, the watermark parameter included, by the rule the `connector_action` node has applied since 18.5.0, and refuses with `code: 'VALIDATION_ERROR'` (status 400) before the action is called, naming each undeclared key by its dotted path and the keys the action declares at that level. Nothing is pulled and nothing is written. + +A scheduled pull has no `fault` edge: a job whose `pull` names such a mapping fails every run (its `sys_sync_run` row ends `failed`, and the job's `retryPolicy` applies) until the mapping or the action's document is fixed. + +The key the pull adds itself is judged too. With `connectorSource.watermark`, the pull sends the highest stored value of the target field as `query[watermark.param]` — but only once the target holds a value, so a mapping whose first pull wrote rows fails from its second pull on. An `openapi` connector declares only the query parameters its OpenAPI document names, so an operation that does not name the watermark parameter (or names none in `query` while declaring a parameter elsewhere or a body) refuses it; the refusal names the parameter. An operation with no parameter and no body has no `inputSchema`, so nothing is judged and the watermark is sent; it starts refusing the watermark the day the document gives it its first parameter or a body, unless the watermark parameter is declared with it. A `rest` connector's `request` action declares `query` with no `properties`, which reads as open, so its query keys, the watermark included, are not refused. + +Migration, FROM → TO: + +- A stray key the author wrote: `connectorSource: { connector: 'crm_api', action: 'list_contacts', input: { query: { limit: 50, stale: 'x' } } }` → drop `stale` from `input`, or declare it in the action. +- The watermark parameter on an `openapi` connector: `watermark: { field: 'updated_at', param: 'since' }` over an operation whose document lists only `limit` → add `{ name: 'since', in: 'query', schema: { type: 'string' } }` to that operation's `parameters` in the OpenAPI document (its declared type must be the target field's), then let the connector re-materialize. + +`GET /api/v1/automation/connectors` lists each action's `inputSchema`, which is what the pull judges against. Which schemas declare their keys is the `connector_action` node's rule, unchanged: an object schema that lists `properties`, or says `additionalProperties: false`; one that admits more keys (`additionalProperties` true or a schema, `patternProperties`, a composition keyword, `$ref`) or lists no `properties` still sends every key, and so does an action with no `inputSchema`. diff --git a/content/docs/automation/jobs.mdx b/content/docs/automation/jobs.mdx index ecd241bac0a..2ad26f76b78 100644 --- a/content/docs/automation/jobs.mdx +++ b/content/docs/automation/jobs.mdx @@ -285,7 +285,17 @@ export const OrdersPullJob = defineJob({ - A **`connectorSource`** run calls the connector's read action once and reads one response — see [Data sync is defined on the target](https://github.com/objectstack-ai/objectstack/blob/main/packages/spec/docs/SYNC_ARCHITECTURE.md#data-sync-is-defined-on-the-target) - for the watermark and the one-response limit. + for the watermark and the one-response limit. The input it sends is judged + against the action's `inputSchema` first, as a `connector_action` step's is: + a key the action does not declare is refused, never sent. That includes the + watermark parameter the run adds to `query` (`since`, say) once the target + holds a value, so an `openapi` connector's read operation that declares any + parameter or a body must declare it as an `in: query` parameter in the + OpenAPI document. An operation that declares neither has no `inputSchema` + and is not judged, until the day it gains its first parameter or a body; + from then on an undeclared watermark is refused. A scheduled run has no + `fault` edge: such a refusal fails every run until the mapping or the + document is fixed. - A **`datasourceSource`** run reads the federated object in windows cut on `watermark.field`. A built-in driver's windows are read whole, paged by the key the object's `external.columnMap` maps onto `id`. A plugin driver's windows go diff --git a/content/docs/references/data/mapping.mdx b/content/docs/references/data/mapping.mdx index d29ac894edb..9e9bc95663d 100644 --- a/content/docs/references/data/mapping.mdx +++ b/content/docs/references/data/mapping.mdx @@ -75,7 +75,7 @@ const result = ImportFieldMappingSchema.parse(data); | :--- | :--- | :--- | :--- | | **connector** | `string` | ✅ | Name of the connector instance the rows are pulled from — a `connectors[]` entry whose `provider` is `rest` or `openapi` (version 1) | | **action** | `string` | ✅ | Key of the connector action that reads the records: `request` on a `rest` connector, an operation key on an `openapi` connector | -| **input** | `Record` | optional | Fixed input passed to the action on every pull (e.g. `{ method: 'GET', path: '/contacts' }` for `rest`) | +| **input** | `Record` | optional | Fixed input passed to the action on every pull (e.g. `{ method: 'GET', path: '/contacts' }` for `rest`). Judged against the action's `inputSchema` before the call, as a `connector_action` node's input is: a key the action does not declare is refused, never sent — the watermark parameter the pull adds to `query` included, so an `openapi` operation that declares any parameter or a body must declare it in its OpenAPI document. An operation that declares neither has no `inputSchema` and is not judged, until the day it gains its first parameter or a body | | **recordsPath** | `string` | optional | Dot path, within the action's result, to the array of records (e.g. `body.results`); omitted ⇒ the result's `body` is the array | | **watermark** | `{ field: string; param: string }` | optional | Timestamp-incremental pull; omitted ⇒ every pull reads the full set. Either way a pull makes ONE action call and reads ONE response: the connector's paging is not followed, so a paged endpoint yields its first page only — and an incremental pull over a newest-first paged endpoint moves its starting point past the pages it never read | | **paging** | `{ style: Enum<'page' \| 'cursor' \| 'offset' \| 'none'>; param?: string; nextPath?: string }` | optional | The connector's paging convention: page, cursor or offset, or `none` for a source whose one response is the whole set. Omitted ⇒ a pull reads one response and does not treat it as complete | diff --git a/packages/services/service-automation/src/connector-pull-undeclared-input-key.test.ts b/packages/services/service-automation/src/connector-pull-undeclared-input-key.test.ts new file mode 100644 index 00000000000..49ad177b11b --- /dev/null +++ b/packages/services/service-automation/src/connector-pull-undeclared-input-key.test.ts @@ -0,0 +1,200 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33894] The connector pull door refuses an input key the read action does + * not declare, as the `connector_action` node does: every door that + * dispatches a connector action judges its input alike. The undeclared key + * the author wrote is pinned beside the door's other input refusals in + * `connector-pull.test.ts`; this file pins the key the door ADDS itself — + * the watermark parameter, sent as `query[watermark.param]` once the target + * holds a value — which only the action can declare (for an `openapi` + * connector, its OpenAPI document). + * + * The schemas below are the shapes the shipped producers emit: the `openapi` + * generator's `{ path, query, header, body }` sections, where a location the + * operation declares no parameter in is left out, and the `rest` connector's + * `request`, whose `query` lists no `properties` and so declares open input. + * + * Every refusal asserts the ADR-0112 pair (`code` + `status`), the executor's + * `reason`, the named key, and that neither the action nor a write ran. + */ + +import { describe, it, expect, vi } from 'vitest'; +import { + pullConnectorSource, + ConnectorPullError, + type ConnectorPullDeps, +} from './connector-pull.js'; + +const silent = { debug() {}, info() {}, warn() {}, error() {} } as unknown as ConnectorPullDeps['logger']; + +const contactObject = { + name: 'contact', + fields: { + external_id: { name: 'external_id', type: 'text' }, + name: { name: 'name', type: 'text' }, + synced_at: { name: 'synced_at', type: 'text' }, + }, +}; + +/** An incremental mapping: `updated_at` is copied onto `synced_at`, and sent back as `query.since`. */ +function incrementalMapping(input: Record) { + return { + name: 'crm_contacts', + targetObject: 'contact', + fieldMapping: [ + { source: 'id', target: 'external_id' }, + { source: 'name', target: 'name' }, + { source: 'updated_at', target: 'synced_at' }, + ], + mode: 'upsert', + upsertKey: ['external_id'], + connectorSource: { + connector: 'crm_api', action: 'list_contacts', input, + watermark: { field: 'updated_at', param: 'since' }, + }, + }; +} + +/** An `openapi` operation declaring `query.limit` only — the document never names `since`. */ +const OPENAPI_LIMIT_ONLY = { + type: 'object', + properties: { query: { type: 'object', properties: { limit: { type: 'integer' } } } }, +}; + +/** An `openapi` operation declaring only a header parameter: the generator emits no `query` section at all. */ +const OPENAPI_NO_QUERY = { + type: 'object', + properties: { header: { type: 'object', properties: { 'X-Tenant': { type: 'string' } } } }, +}; + +/** The same operation once the document declares `since` as an `in: query` parameter. */ +const OPENAPI_SINCE_DECLARED = { + type: 'object', + properties: { + query: { type: 'object', properties: { limit: { type: 'integer' }, since: { type: 'string' } } }, + }, +}; + +/** The `rest` connector's `request` action: `query` lists no `properties`, so every query key is open. */ +const REST_REQUEST = { + type: 'object', + properties: { + method: { type: 'string' }, + path: { type: 'string' }, + headers: { type: 'object' }, + query: { type: 'object' }, + body: {}, + }, +}; + +const STORED = [{ synced_at: '2026-01-02T00:00:00.000Z' }]; + +function harness(opts: { + /** Absent ⇒ the action declares no `inputSchema`. */ + inputSchema: Record | undefined; + input: Record; + stored?: Array>; +}) { + const writes: unknown[] = []; + const handler = vi.fn(async (_input: Record) => ({ status: 200, ok: true, body: [] }) as Record); + const mapping = incrementalMapping(opts.input); + const deps: ConnectorPullDeps = { + protocol: { + async getMetaItem({ type, name }) { + if (type === 'mapping' && name === mapping.name) return { type, name, item: mapping }; + if (type === 'object' && name === 'contact') return { type, name, item: contactObject }; + return undefined; + }, + async findData() { return { records: opts.stored ?? [] }; }, + async createData(args) { writes.push(args); return { id: 'new' }; }, + async updateData(args) { writes.push(args); return { id: 'upd' }; }, + }, + registry: { + resolveConnectorAction: (connector, action) => + connector === 'crm_api' && action === 'list_contacts' ? handler : undefined, + getConnectorOrigin: (name) => (name === 'crm_api' ? 'declarative' : undefined), + getConnectorDegradedReason: () => undefined, + getConnectorDescriptors: () => [{ + name: 'crm_api', label: 'CRM', type: 'api', origin: 'declarative', state: 'ready', + actions: [{ key: 'list_contacts', label: 'List contacts', inputSchema: opts.inputSchema }], + }], + }, + providerOf: (name) => (name === 'crm_api' ? 'openapi' : undefined), + logger: silent, + }; + return { deps, handler, writes }; +} + +async function refusal(h: ReturnType): Promise { + const err = await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }).then( + () => undefined, + (e: unknown) => e, + ); + expect(err, 'the pull must refuse, not resolve').toBeInstanceOf(ConnectorPullError); + const e = err as ConnectorPullError; + expect({ code: e.code, status: e.status, reason: e.reason, mapping: e.mapping }).toEqual({ + code: 'VALIDATION_ERROR', status: 400, reason: 'input_schema_mismatch', mapping: 'crm_contacts', + }); + expect(h.handler).toHaveBeenCalledTimes(0); + expect(h.writes).toEqual([]); + return e; +} + +describe('[#33894] connector pull — the watermark parameter the door adds is judged like any other input key', () => { + it('a watermark parameter the operation does not declare is refused, naming it and the OpenAPI fix', async () => { + const h = harness({ inputSchema: OPENAPI_LIMIT_ONLY, input: { query: { limit: 50 } }, stored: STORED }); + const err = await refusal(h); + expect(err.message).toContain("'query.since' is not an input the action declares"); + expect(err.message).toContain("watermark parameter 'since'"); + expect(err.message).toContain('OpenAPI document'); + }); + + it('an operation that declares no query parameter at all refuses the watermark the same way', async () => { + const h = harness({ inputSchema: OPENAPI_NO_QUERY, input: {}, stored: STORED }); + const err = await refusal(h); + expect(err.message).toContain("'query' is not an input the action declares"); + expect(err.message).toContain("watermark parameter 'since'"); + }); + + it('an undeclared key the author wrote does not blame the watermark when the watermark is declared', async () => { + const h = harness({ + inputSchema: OPENAPI_SINCE_DECLARED, + input: { query: { limit: 50, stale: 'x' } }, + stored: STORED, + }); + const err = await refusal(h); + expect(err.message).toContain("'query.stale' is not an input the action declares"); + expect(err.message).not.toContain('watermark parameter'); + }); + + it('CONTROL: once the document declares the watermark parameter, the pull sends it', async () => { + const h = harness({ inputSchema: OPENAPI_SINCE_DECLARED, input: { query: { limit: 50 } }, stored: STORED }); + await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); + expect(h.handler).toHaveBeenCalledTimes(1); + expect(h.handler.mock.calls[0][0]).toEqual({ query: { limit: 50, since: '2026-01-02T00:00:00.000Z' } }); + }); + + it('CONTROL: the first pull, with nothing stored yet, sends no watermark and so is not refused for it', async () => { + const h = harness({ inputSchema: OPENAPI_LIMIT_ONLY, input: { query: { limit: 50 } } }); + await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); + expect(h.handler).toHaveBeenCalledTimes(1); + expect(h.handler.mock.calls[0][0]).toEqual({ query: { limit: 50 } }); + }); + + it('CONTROL: an openapi operation with no parameter and no body declares no inputSchema, so nothing is judged and the watermark is sent', async () => { + const h = harness({ inputSchema: undefined, input: {}, stored: STORED }); + await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); + expect(h.handler).toHaveBeenCalledTimes(1); + expect(h.handler.mock.calls[0][0]).toEqual({ query: { since: '2026-01-02T00:00:00.000Z' } }); + }); + + it('CONTROL: a rest connector\'s open query section takes the watermark parameter as before', async () => { + const h = harness({ inputSchema: REST_REQUEST, input: { method: 'GET', path: '/contacts' }, stored: STORED }); + await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); + expect(h.handler).toHaveBeenCalledTimes(1); + expect(h.handler.mock.calls[0][0]).toEqual({ + method: 'GET', path: '/contacts', query: { since: '2026-01-02T00:00:00.000Z' }, + }); + }); +}); diff --git a/packages/services/service-automation/src/connector-pull.test.ts b/packages/services/service-automation/src/connector-pull.test.ts index e60f83c82d0..1991fdf2f74 100644 --- a/packages/services/service-automation/src/connector-pull.test.ts +++ b/packages/services/service-automation/src/connector-pull.test.ts @@ -382,7 +382,7 @@ describe('[#28040] connector pull — the input is checked against the action\'s expect(res.pulled).toBe(0); }); - it('[#33553] an undeclared key is not refused here: the pull door does not opt in to that judgment', async () => { + it('[#33894] an undeclared key is VALIDATION_ERROR 400 naming it, as at the connector_action node, and the action is never called', async () => { const h = harness({ inputSchema: SINCE_REQUIRED, mapping: baseMapping({ @@ -392,11 +392,12 @@ describe('[#28040] connector pull — the input is checked against the action\'s }, }), }); - await pullConnectorSource(h.deps, { mapping: 'crm_contacts' }); - expect(h.handler).toHaveBeenCalledTimes(1); - expect(h.handler.mock.calls[0][0]).toEqual({ - path: '/contacts', query: { since: '2026-01-01', stale: 'x' }, extra: 'kept', - }); + const err = await refusal(h); + expectRefused(err, { code: 'VALIDATION_ERROR', status: 400, reason: 'input_schema_mismatch' }); + expect(err.message).toContain("'query.stale' is not an input the action declares"); + expect(err.message).toContain("'extra' is not an input the action declares"); + expect(h.handler).toHaveBeenCalledTimes(0); + expect(h.writes).toEqual([]); }); it('a valid input still pulls and writes', async () => { diff --git a/packages/services/service-automation/src/connector-pull.ts b/packages/services/service-automation/src/connector-pull.ts index bfb7bad3167..0e84c8b8954 100644 --- a/packages/services/service-automation/src/connector-pull.ts +++ b/packages/services/service-automation/src/connector-pull.ts @@ -20,7 +20,10 @@ * 3. W1 watermark: read the highest value already written to the target * field `fieldMapping` maps `watermark.field` onto, and send it as the * action's `query[watermark.param]`; - * 4. make ONE action call and read the records at `recordsPath`; + * 4. judge the input the call sends, the watermark included, against the + * action's declared `inputSchema` as the `connector_action` node does + * (an undeclared key included), then make ONE action call and read the + * records at `recordsPath`; * 5. project them through the mapping (`applyMappingToRows`) and write them * with `runImport` — the runner, coercion and row verdicts the import * door uses, so a synced row and an uploaded row speak one vocabulary. @@ -786,8 +789,21 @@ async function pullOnce( // hint is the pull door's own: `connectorSource.input` is JSON spread as // written and the watermark is the stored value, so no CEL envelope runs // here and the node's `int()` / `double()` / `bool()` advice cannot apply. - const verdict = judgeConnectorInput(findConnectorActionInputSchema(registry, connector, action, instancePackage), input); + // + // [#33894] Every door that dispatches a connector action judges its input + // alike (maintainer ruling 6896271476), so a key the action's schema does + // not declare is refused here too, as at the `connector_action` node, + // instead of being sent to the provider. That includes the key this door + // adds itself: the watermark parameter, sent as `query[watermark.param]` + // once the target holds a value. Whether THAT key is the undeclared one is + // asked by judging it alone, so the refusal names it and its fix. + const inputSchema = findConnectorActionInputSchema(registry, connector, action, instancePackage); + const verdict = judgeConnectorInput(inputSchema, input, { refuseUndeclaredKeys: true }); if (verdict) { + const addedParam = watermark && watermark.from !== undefined ? watermark.param : undefined; + const addedUndeclared = verdict.undeclaredKey && addedParam !== undefined + && judgeConnectorInput(inputSchema, { query: { [addedParam]: watermark?.from } }, { refuseUndeclaredKeys: true }) + ?.undeclaredKey === true; return refuse( 'VALIDATION_ERROR', 400, 'input_schema_mismatch', `Mapping "${mappingName}": input for '${connector}.${action}' does not match the action's inputSchema — ` @@ -797,6 +813,16 @@ async function pullOnce( ? ' A text value for an integer, number or boolean parameter is not converted: write it in ' + "connectorSource.input as a JSON number or boolean (50, not '50'); a watermark parameter " + "carries the stored target field's value, so its declared type must be that field's." + : '') + + (verdict.undeclaredKey + ? ' A key the action does not declare is not sent to the provider: remove it from ' + + 'connectorSource.input, or declare it in the action (for an openapi connector, as a ' + + 'parameter of the operation in its OpenAPI document).' + : '') + + (addedUndeclared + ? ` The pull adds the watermark parameter '${addedParam}' to input.query itself, so the action ` + + `must declare it: for an openapi connector, declare '${addedParam}' as an in: query ` + + 'parameter of the operation in the OpenAPI document.' : ''), ); } diff --git a/packages/spec/src/data/mapping.zod.ts b/packages/spec/src/data/mapping.zod.ts index a379ebde40a..8a5311cdef9 100644 --- a/packages/spec/src/data/mapping.zod.ts +++ b/packages/spec/src/data/mapping.zod.ts @@ -479,7 +479,11 @@ export const MappingSchema = lazySchema(() => strictObject({ ), input: z.record(z.string(), z.unknown()).optional().describe( 'Fixed input passed to the action on every pull (e.g. `{ method: \'GET\', path: \'/contacts\' }` ' - + 'for `rest`)', + + 'for `rest`). Judged against the action\'s `inputSchema` before the call, as a `connector_action` ' + + 'node\'s input is: a key the action does not declare is refused, never sent — the watermark ' + + 'parameter the pull adds to `query` included, so an `openapi` operation that declares any parameter ' + + 'or a body must declare it in its OpenAPI document. An operation that declares neither has no ' + + '`inputSchema` and is not judged, until the day it gains its first parameter or a body', ), recordsPath: z.string().min(1).optional().describe( 'Dot path, within the action\'s result, to the array of records (e.g. `body.results`); ' diff --git a/packages/spec/src/migrations/entries/semantic/18.connector-pull-undeclared-input-key-refused.ts b/packages/spec/src/migrations/entries/semantic/18.connector-pull-undeclared-input-key-refused.ts new file mode 100644 index 00000000000..5fa8f4fe83c --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/18.connector-pull-undeclared-input-key-refused.ts @@ -0,0 +1,38 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// The connector pull door refuses, at pull time, an input key the read +// action's `inputSchema` does not declare, as the `connector_action` node +// already does: every door that dispatches a connector action judges its input +// alike. A semantic entry and not a conversion: whether a stray key is a +// leftover to drop or a parameter the action's document should declare is the +// author's call — and the watermark parameter the pull adds itself can only be +// declared by the action. +export const entry: SemanticMigration = { + id: 'connector-pull-undeclared-input-key-refused', + surface: + 'A `mapping`\'s `connectorSource.input`, and the watermark parameter the pull adds to it as ' + + '`query[watermark.param]`, when the input sent holds a key (at any object level, array items included) ' + + 'that the read action\'s `inputSchema` does not declare at that level.', + replacement: + 'Drop the key from `connectorSource.input`, or declare it in the action. A key the pull adds itself ' + + 'cannot be dropped from the mapping: for an `openapi` connector, declare the watermark parameter ' + + '(e.g. `since`) as an `in: query` parameter of the read operation in the OpenAPI document. The declared ' + + 'keys are the names in the schema\'s `properties` plus those in `required`, as ' + + '`GET /api/v1/automation/connectors` lists them; a `rest` connector\'s `request` declares `query` with ' + + 'no `properties`, so every query key stays open there.', + reason: + 'The pull door judged the input against the action\'s `inputSchema` for required parameters and types ' + + 'only, so a key no action reads was sent to the provider while a `connector_action` node with the ' + + 'same input refused it. A pull now fails before the call with VALIDATION_ERROR naming the key and ' + + 'the declared keys. A job\'s scheduled pull has no fault edge, so the refusal fails every run (its ' + + '`sys_sync_run` row ends `failed`) until the mapping or the document is fixed; the watermark parameter ' + + 'is sent only once the target holds a value, so a mapping whose first pull wrote rows fails from its ' + + 'second pull on.', + acceptanceCriteria: + 'Every `connectorSource.input` holds only keys its read action declares, at every level, and with a ' + + '`watermark` the action declares `watermark.param` in its `query`. Two pulls of the mapping in a row ' + + 'both complete; a pull whose input still holds an undeclared key fails with `code: \'VALIDATION_ERROR\'` ' + + 'naming that key, and the action is never called.', +};