|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#18977] `$orderby` is declared TWICE, and the two declarations are |
| 5 | + * COMPLEMENTARY REFUSALS — each accepts exactly what the other rejects. |
| 6 | + * |
| 7 | + * | declaration | accepts | refuses | |
| 8 | + * |:---|:---|:---| |
| 9 | + * | `ODataQuerySchema.$orderby` (`api/odata.zod.ts`) | `string`, `string[]` | the record maps, `SortNode[]` | |
| 10 | + * | `QueryTransportParamsSchema.$orderby` = `DataEngineSortSchema` (`data/data-engine.zod.ts`) | the record maps, `SortNode[]` | `string`, `string[]` — deliberately, #18704 | |
| 11 | + * |
| 12 | + * Neither file pointed at the other, so reading one of them carefully and |
| 13 | + * completely still produced the wrong answer about the other — measured, at a |
| 14 | + * price: objectui#9554 was filed, triaged, graded and dispatched against a |
| 15 | + * shipped `object-grid` producer that had been sending the canonical shape all |
| 16 | + * along, because the filing seat read the OData declaration and correctly |
| 17 | + * quoted it. The cross-references landed in both files with this card; this |
| 18 | + * file is their MECHANICAL half. |
| 19 | + * |
| 20 | + * ## What this pins, and what it deliberately does not |
| 21 | + * |
| 22 | + * It pins the two accept sets AS THEY ARE, and their disjointness. It is not |
| 23 | + * an argument that either set is right: |
| 24 | + * |
| 25 | + * - §1/§2 hold each side's accept set, so widening or narrowing either one |
| 26 | + * turns this red and lands the author on the cross-reference that explains |
| 27 | + * why the gap is a decision rather than a defect. Widening the TRANSPORT |
| 28 | + * side is the one the source argues against in its own words — lowering a |
| 29 | + * sort expression means PARSING, and a second parser beside the door's is |
| 30 | + * how one rule gets two implementations that disagree. |
| 31 | + * - §3 is the disjointness itself: no value parses under both. That is the |
| 32 | + * property a reader cannot get from either file alone, and the one that |
| 33 | + * makes "I read the declaration" insufficient. |
| 34 | + * - §4 says which of the two grades a query bag, through the slot the REST |
| 35 | + * door actually parses (`FindDataRequestSchema.query`). |
| 36 | + * |
| 37 | + * ⛔ It pins nothing about what the RUNTIME serves. The OData string forms are |
| 38 | + * not unserved — `normalizeSortNodes` (`@objectstack/metadata-protocol`) reads |
| 39 | + * them at the GET querystring ingress and for in-process `findData`. What this |
| 40 | + * file measures is the two SCHEMAS, which is where the card's trap lives. |
| 41 | + */ |
| 42 | + |
| 43 | +import { describe, it, expect } from 'vitest'; |
| 44 | +import { ODataQuerySchema } from './odata.zod'; |
| 45 | +import { FindDataRequestSchema } from './protocol.zod'; |
| 46 | +import { DataEngineSortSchema, QueryTransportParamsSchema } from '../data/data-engine.zod'; |
| 47 | + |
| 48 | +/** The two shapes `ODataQuerySchema` declares and the transport schema refuses. */ |
| 49 | +const ODATA_SPELLINGS: ReadonlyArray<readonly [string, unknown]> = [ |
| 50 | + ["the 'field direction' expression", 'name desc'], |
| 51 | + ["the '-field' shorthand", '-created_at'], |
| 52 | + ['the expression array', ['name desc', 'email asc']], |
| 53 | + ['a single-element expression array', ['name']], |
| 54 | +]; |
| 55 | + |
| 56 | +/** The three shapes `DataEngineSortSchema` declares and the OData schema refuses. */ |
| 57 | +const TRANSPORT_SPELLINGS: ReadonlyArray<readonly [string, unknown]> = [ |
| 58 | + ['the asc/desc record map', { name: 'desc' }], |
| 59 | + ['the 1/-1 record map', { name: 1 }], |
| 60 | + ['the SortNode array', [{ field: 'name', order: 'desc' }]], |
| 61 | +]; |
| 62 | + |
| 63 | +describe('[#18977] $orderby is declared twice — the two accept sets', () => { |
| 64 | + describe('§1 ODataQuerySchema.$orderby — the OData URL-convention vocabulary', () => { |
| 65 | + it.each(ODATA_SPELLINGS)('accepts %s', (_label, value) => { |
| 66 | + const parsed = ODataQuerySchema.safeParse({ $orderby: value }); |
| 67 | + expect(parsed.success).toBe(true); |
| 68 | + }); |
| 69 | + |
| 70 | + it.each(TRANSPORT_SPELLINGS)('refuses %s, at the $orderby member', (_label, value) => { |
| 71 | + const parsed = ODataQuerySchema.safeParse({ $orderby: value }); |
| 72 | + expect(parsed.success).toBe(false); |
| 73 | + if (parsed.success) return; |
| 74 | + expect(parsed.error.issues.map((i) => i.path.join('.'))).toContain('$orderby'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('§2 DataEngineSortSchema — what QueryTransportParamsSchema.$orderby is', () => { |
| 79 | + it.each(TRANSPORT_SPELLINGS)('accepts %s', (_label, value) => { |
| 80 | + expect(DataEngineSortSchema.safeParse(value).success).toBe(true); |
| 81 | + expect(QueryTransportParamsSchema.safeParse({ $orderby: value }).success).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + it.each(ODATA_SPELLINGS)('refuses %s, at the $orderby member', (_label, value) => { |
| 85 | + expect(DataEngineSortSchema.safeParse(value).success).toBe(false); |
| 86 | + const parsed = QueryTransportParamsSchema.safeParse({ $orderby: value }); |
| 87 | + expect(parsed.success).toBe(false); |
| 88 | + if (parsed.success) return; |
| 89 | + expect(parsed.error.issues.map((i) => i.path.join('.'))).toContain('$orderby'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('refuses the OData spelling on `sort` too — the bare transport alias of the same slot', () => { |
| 93 | + // A reader who works around the `$orderby` refusal by re-spelling the key |
| 94 | + // gets the same answer: the refusal is on the VALUE, not on the spelling. |
| 95 | + for (const [, value] of ODATA_SPELLINGS) { |
| 96 | + expect(QueryTransportParamsSchema.safeParse({ sort: value }).success).toBe(false); |
| 97 | + } |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('§3 the two accept sets are DISJOINT — this is the trap', () => { |
| 102 | + it('no declared $orderby value parses under both', () => { |
| 103 | + const every = [...ODATA_SPELLINGS, ...TRANSPORT_SPELLINGS]; |
| 104 | + const bothAccept = every.filter(([, value]) => |
| 105 | + ODataQuerySchema.safeParse({ $orderby: value }).success |
| 106 | + && DataEngineSortSchema.safeParse(value).success); |
| 107 | + expect(bothAccept.map(([label]) => label)).toEqual([]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('every declared $orderby value parses under exactly one of them — neither set is empty', () => { |
| 111 | + // The lit control for the emptiness above: a disjointness assertion is |
| 112 | + // also satisfied by two schemas that accept nothing at all. |
| 113 | + const every = [...ODATA_SPELLINGS, ...TRANSPORT_SPELLINGS]; |
| 114 | + const accepted = every.map(([, value]) => |
| 115 | + Number(ODataQuerySchema.safeParse({ $orderby: value }).success) |
| 116 | + + Number(DataEngineSortSchema.safeParse(value).success)); |
| 117 | + expect(accepted).toEqual(every.map(() => 1)); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('§4 which one grades a query bag', () => { |
| 122 | + // `POST /data/:object/query` parses its body through this schema and answers |
| 123 | + // `400 VALIDATION_FAILED` on a refusal (`rest-server.ts`), so this is the |
| 124 | + // declaration an author's stored query bag is actually judged against. |
| 125 | + const findInput = (query: Record<string, unknown>) => ({ object: 't', query: { ...query, object: 't' } }); |
| 126 | + |
| 127 | + it.each(TRANSPORT_SPELLINGS)('FindDataRequest.query accepts %s on $orderby', (_label, value) => { |
| 128 | + expect(FindDataRequestSchema.safeParse(findInput({ $orderby: value })).success).toBe(true); |
| 129 | + }); |
| 130 | + |
| 131 | + it.each(ODATA_SPELLINGS)('FindDataRequest.query refuses %s on $orderby', (_label, value) => { |
| 132 | + const parsed = FindDataRequestSchema.safeParse(findInput({ $orderby: value })); |
| 133 | + expect(parsed.success).toBe(false); |
| 134 | + if (parsed.success) return; |
| 135 | + expect(parsed.error.issues.map((i) => i.path.join('.'))).toContain('query.$orderby'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('ODataQuerySchema grades nothing here — it is not on any path into FindDataRequest', () => { |
| 139 | + // The canonical AST key is the one the output carries, whichever declared |
| 140 | + // spelling arrived: the transport spelling folds onto `orderBy`. |
| 141 | + const parsed = FindDataRequestSchema.safeParse(findInput({ $orderby: { created_at: 'desc' } })); |
| 142 | + expect(parsed.success).toBe(true); |
| 143 | + if (!parsed.success) return; |
| 144 | + expect((parsed.data as { query: { orderBy?: unknown } }).query.orderBy) |
| 145 | + .toEqual([{ field: 'created_at', order: 'desc' }]); |
| 146 | + expect((parsed.data as { query: Record<string, unknown> }).query.$orderby).toBeUndefined(); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments