Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
import { suite, test, before, after } from 'node:test';
import { ok, strictEqual, deepStrictEqual } from 'node:assert';
import { resolve } from 'node:path';
import { unpack } from 'msgpackr';
import { unpack, unpackMultiple } from 'msgpackr';
import { decode as decodeCbor } from 'cbor-x';
import { setTimeout as sleep } from 'node:timers/promises';
import { setupHarperWithFixture, teardownHarper, type ContextWithHarper } from '@harperfast/integration-testing';
// @ts-expect-error utils/client.mjs has no type declarations; runtime resolves fine
Expand Down Expand Up @@ -428,14 +429,13 @@ suite(
: unpack(Buffer.from(await response.arrayBuffer()));
const storedPrototype =
path.startsWith('/OrderItem/') && selection === 'metadata{constructor{city},__proto__{city}}';
const jsonExpected = storedPrototype
? { metadata: { constructor: { city: 'Denver' }, ['__proto__']: {} } }
: expected;
// a stored record's `__proto__` key decodes renamed (`__proto_`), so selecting `__proto__` finds no data
const jsonExpected = storedPrototype ? { metadata: { constructor: { city: 'Denver' } } } : expected;
const binaryExpectations = {
'metadata{constructor{city},__proto__{city}}': {
metadata: {
constructor: { city: 'Denver' },
__proto_: { city: storedPrototype ? undefined : 'Denver' },
__proto_: storedPrototype ? undefined : { city: 'Denver' },
},
},
'notes{city}': { notes: [{ city: 'Denver' }, null, 'literal', { city: undefined }] },
Expand All @@ -452,6 +452,55 @@ suite(
}
});

test('filtered REST select projects plain JSON like the single-record URL', async () => {
await clearAll();
strictEqual((await restPut('/Order/ord-collide', { id: 'ord-collide', total: 5 })).status, 204);
const metadata = {
label: 'kept',
orderId: 'ord-collide',
order: { id: 'json-order', name: 'Plain JSON Order', hidden: 'omit' },
$updatedtime: 'nested-updated',
hasOwnProperty: 'ordinary data',
};
strictEqual((await restPut('/OrderItem/json-collide', { id: 'json-collide', metadata })).status, 204);
const selection =
'id,metadata{label,order{id,name},$updatedtime,hasOwnProperty,valueOf,toString,constructor,__proto__{x}}';
for (const accept of ['application/json', 'application/cbor', 'application/x-msgpack']) {
// a streamed msgpack collection is a sequence of values, not one array
const decode = async (response: Response, collection = false) =>
accept === 'application/json'
? response.json()
: accept === 'application/cbor'
? decodeCbor(Buffer.from(await response.arrayBuffer()))
: (collection ? unpackMultiple : unpack)(Buffer.from(await response.arrayBuffer()));
const single = await fetch(`${httpURL}/OrderItem/json-collide?select(${selection})`, {
headers: { Accept: accept, Authorization: auth },
});
const collection = await fetch(`${httpURL}/OrderItem/?id=json-collide&select(${selection})`, {
headers: { Accept: accept, Authorization: auth },
});
strictEqual(single.status, 200, accept);
strictEqual(collection.status, 200, accept);
const singleBody = await decode(single);
const rows = await decode(collection, true);
strictEqual(rows.length, 1, accept);
const row = rows[0];
deepStrictEqual(row, singleBody, `collection row equals single-record projection (${accept})`);
strictEqual(row.error, undefined, accept);
strictEqual(row.id, 'json-collide');
strictEqual(row.metadata.label, 'kept');
deepStrictEqual(row.metadata.order, { id: 'json-order', name: 'Plain JSON Order' }, accept);
strictEqual(row.metadata.$updatedtime, 'nested-updated');
strictEqual(row.metadata.hasOwnProperty, 'ordinary data');
for (const inherited of ['valueOf', 'toString', 'constructor', '__proto__', '__proto_'])
strictEqual(
Object.hasOwn(row.metadata, inherited) ? row.metadata[inherited] : undefined,
undefined,
`${inherited} (${accept})`
);
}
});

test('classless nested projections omit function values across native encoders', async () => {
const expectations = [
['application/json', { id: 'json', metadata: {} }],
Expand Down
2 changes: 1 addition & 1 deletion resources/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ A contract is `{ path, record?, get/post/put/patch/delete: { query?, body?, resp

- Nested relationship `conditions`, `sort`, `offset`, and `limit` share `Table._prepareRelationshipSelection` across search and single-record GET. Predicates keep parent inclusion and inherited relationship candidates; only unsorted eligible pagination bounds reads (sorting or unmatched predicates can scan every candidate). Nested predicates qualify only the immediate selected children; descendant narrowing requires its own subselect, while outer-join candidate restrictions remain inherited. Predicate compilation must not mutate projection maps. Candidates and sort keys retain top-level cache-local semantics; predicates are rechecked after source refresh before offset/limit counts a returned child. Cancellation must terminate the raw source before an upstream mapper reads again. Lazy resolver mode is per-call so ordinary property access stays synchronous. Unsorted FK pagination preserves missing slots; sorting drops them. `unitTests/resources/nestedRelationshipConstraints.test.js` pins authorization, iterator cleanup, missing-value behavior, and bounded entry reads.

- Single-record nested selections use `Resource.transformForSelect`; searches use `Table.transformEntryForSelect`. Their contracts for missing targets, `forceNulls`, Dates, callable values, resolver dictionaries, `Resource.transformForSelect`'s `WeakMap` sub-transform cache (keyed by the selector object so no request-supplied name becomes a key), and a selected `__proto__` are pinned by `../unitTests/resources/nestedSelectMissingValues.test.js` and `../integrationTests/resources/relationship-and-status-contract.test.ts`. The `select` branch in `parseBlock` (`search.ts`) unwraps a single-argument `select(x)` only when that argument is not a named sub-select, keeping a single nested selector (`select(rel{a,b})` or `select(rel[select(a,b)])`) wrapped in its outer array and preserving named bracket queries for table-relationship filtering while bare comparisons in selector lists remain syntax errors (pinned by `../unitTests/resources/query-parse.test.js` and `../unitTests/resources/restSelectProjectionFixes.test.js`); [harper#1415](https://github.com/HarperFast/harper/issues/1415) remains open for single-attribute `select(attr)` returning 404 when the selected scalar value is null or missing. Still open, outside these helpers: [asynchronous arrays and `forceNulls`](https://github.com/HarperFast/harper/issues/3168) and [a `__proto__` key poisoning a table, and proto-named attributes rejected at insert](https://github.com/HarperFast/harper/issues/1295).
- Single-record nested selections use `Resource.transformForSelect`; searches use `Table.transformEntryForSelect`. Only table records and `@relationship` targets project through a table's resolvers: a plain JSON sub-object, or what a `directReturn` (`@computed`) resolver returns, goes through the resolver-free `nestedSelectTransform(selector, undefined)`, because an outer resolver must never run against nested data that shares its name (search/single-record parity is pinned by `../unitTests/resources/searchSelectProjection.test.js`). Table `propertyResolvers` has a null prototype, so no lookup, the plain-column select fast path included, finds an `Object.prototype` member. Both transform caches are keyed by the selector, so one name selected twice with different sub-selects gets two transforms. A lone `*` returns the value unchanged, except a resource-backed `['*']`, which copies; mixed with names `*` copies data keys (a tracked instance's from `toJSON()`, none from a Buffer, Blob or Date). Positional and array projections settle as one promise, `forceNulls` applies after a value settles, function values project as absent, and a selected `__proto__` is read only as an own property. Missing targets, Dates and callable values are pinned by `../unitTests/resources/nestedSelectMissingValues.test.js` and `../integrationTests/resources/relationship-and-status-contract.test.ts`. The `select` branch in `parseBlock` (`search.ts`) unwraps a single-argument `select(x)` only when that argument is not a named sub-select, keeping a single nested selector (`select(rel{a,b})` or `select(rel[select(a,b)])`) wrapped in its outer array and preserving named bracket queries for table-relationship filtering while bare comparisons in selector lists remain syntax errors (pinned by `../unitTests/resources/query-parse.test.js` and `../unitTests/resources/restSelectProjectionFixes.test.js`); [harper#1415](https://github.com/HarperFast/harper/issues/1415) remains open for single-attribute `select(attr)` returning 404 when the selected scalar value is null or missing. Still open, outside these helpers: [a `__proto__` key poisoning a table, and proto-named attributes rejected at insert](https://github.com/HarperFast/harper/issues/1295).
- A record's `toJSON` is a response projection, not durable state. `recordUpdater` (and the cache-fill and audit paths feeding it) projects records to stored fields via `storedFieldsOnly`, so no durable form carries a resolver-owned (`@computed`/`@relationship`) name; materialization skips such names when promoting a decoded record (`assignStoredFields`), which is what keeps rows written by affected releases readable. See harper#2359 and `integrationTests/resources/cachedComputedAttribute.test.ts`.
- **Never** remove `transactional()` from a static method on `Resource` β€” it owns transaction context lifetime.
- New `Resource` subclasses should override **instance** methods (`get`, `put`, ...) for behavior; static methods are the protocol entry points and stay generic.
Expand Down
Loading
Loading