Skip to content

Commit 88d7f78

Browse files
committed
fix(objectql): refuse a projection whose every column is unknown, as an empty one is
Claude-Session: https://claude.ai/code/session_01L6bhgXcN9HaZYSEUdJWEjw Co-authored-by: Claude <noreply@anthropic.com>
1 parent 8993840 commit 88d7f78

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

‎packages/objectql/src/empty-projection-door.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
* the bare foreign key when a sub-read throws, so the refusal would reach the
3737
* caller as a silently unexpanded field. Judged here, before the parent read,
3838
* it reaches the caller as a refusal.
39+
*
40+
* ## A projection emptied by the unknown-column tolerance
41+
*
42+
* [#27165] `find` / `findOne` drop a requested plain column the object does not
43+
* have (the #7589 tolerance). When that leaves nothing, the projection is
44+
* empty, and it gets this same refusal, naming what was asked for. Before, the
45+
* engine fell back to every column there, so `fields: ['nope']` answered wider
46+
* than any named projection. A mix of known and unknown columns keeps the
47+
* tolerance. An `expand` entry needs no judgement of its own here: its
48+
* sub-read always names `id`, which is never dropped.
3949
*/
4050

4151
import { EngineQueryOptionsSchema } from '@objectstack/spec/data';
@@ -48,9 +58,17 @@ function projectionNamesNoField(): string | undefined {
4858
return declaredRefusal;
4959
}
5060

51-
function refuseEmptyProjection(object: string, operation: 'find' | 'findOne', param: string): never {
61+
function refuseEmptyProjection(
62+
object: string,
63+
operation: 'find' | 'findOne',
64+
param: string,
65+
requested: readonly unknown[] = [],
66+
): never {
67+
const dropped = requested.length > 0
68+
? `, and none of them is a field of '${object}' (an unknown column is dropped from a projection)`
69+
: '';
5270
const err: any = new Error(
53-
`ObjectQL.${operation}('${object}') names no field in ${param}: got []. The query was not run: `
71+
`ObjectQL.${operation}('${object}') names no field in ${param}: got ${JSON.stringify(requested)}${dropped}. The query was not run: `
5472
+ 'an empty projection was read as no projection, which answers every column. '
5573
+ `${projectionNamesNoField()}, or leave the projection out to read every column.`,
5674
);
@@ -93,3 +111,16 @@ export function assertProjectionNamesAField(
93111
if (Array.isArray(query.fields) && query.fields.length === 0) refuseEmptyProjection(object, operation, "'fields'");
94112
assertExpandProjections(object, operation, query.expand, '');
95113
}
114+
115+
/**
116+
* [#27165] Refuse a projection the unknown-column tolerance emptied: every
117+
* entry of `requested` named a column `object` does not have. Same refusal as
118+
* `fields: []`, naming what was asked for.
119+
*/
120+
export function refuseProjectionOfUnknownColumns(
121+
object: string,
122+
operation: 'find' | 'findOne',
123+
requested: readonly unknown[],
124+
): never {
125+
return refuseEmptyProjection(object, operation, "'fields'", requested);
126+
}

‎packages/objectql/src/engine.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ import {
303303
// each body the members it reads (`CountAggregateHost`).
304304
import { count, rejectCredentialAggregation } from './engine-count-aggregate.js';
305305
import { assertGroupByNamesNoJsonStoredField } from './group-by-structured-json-door.js';
306-
import { assertProjectionNamesAField } from './empty-projection-door.js';
306+
import { assertProjectionNamesAField, refuseProjectionOfUnknownColumns } from './empty-projection-door.js';
307307
import { assertAggregationFieldTypesAccepted } from './aggregate-field-type-door.js';
308308
import { assertAggregatedColumnsAreDistinct, assertDateBucketTargetsAreTemporal } from './aggregate-query-shape-door.js';
309309
import { applyProtection, pluralToSingular, ExternalWriteForbiddenError } from '@objectstack/spec/shared';
@@ -13591,10 +13591,10 @@ export class ObjectQL implements IObjectQLEngine {
1359113591
// Whole names, no head-splitting: only plain entries reach here (the
1359213592
// dotted refusal above fired on anything carrying a '.').
1359313593
const filtered = ast.fields.filter(f => known.has(f));
13594-
// Guard against an empty projection — fall back to `*` so the
13595-
// request still returns rows. An empty SELECT list would either
13596-
// 400 in Postgres or silently project nothing.
13597-
ast.fields = filtered.length > 0 ? filtered : undefined;
13594+
// [#27165] Nothing left is an empty projection, refused as `fields: []`
13595+
// is — never read as every column.
13596+
if (filtered.length === 0) refuseProjectionOfUnknownColumns(object, 'find', ast.fields);
13597+
ast.fields = filtered;
1359813598
}
1359913599

1360013600
const opCtx: OperationContext = {
@@ -13897,7 +13897,9 @@ export class ObjectQL implements IObjectQLEngine {
1389713897
// in schema.fields (matches `find()` behavior, one list).
1389813898
const known = new Set<string>([...Object.keys(_findOneSchema.fields), ...PLATFORM_PROVISIONED_COLUMNS]);
1389913899
const filtered = ast.fields.filter(f => known.has(f));
13900-
ast.fields = filtered.length > 0 ? filtered : undefined;
13900+
// [#27165] Same refusal as `find`.
13901+
if (filtered.length === 0) refuseProjectionOfUnknownColumns(objectName, 'findOne', ast.fields);
13902+
ast.fields = filtered;
1390113903
}
1390213904

1390313905
const opCtx: OperationContext = {

0 commit comments

Comments
 (0)