|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #29277 — a cleared optional single-value option or reference field, through |
| 5 | + * the REST write doors, on a real engine (`ObjectQL` + sqlite `SqlDriver`) and |
| 6 | + * the real `RestServer` routes (the harness `rest-data-optional-unique-blank.test.ts` |
| 7 | + * boots). |
| 8 | + * |
| 9 | + * The defect, measured on `origin/main` 5780fa49: a `select` or `lookup` |
| 10 | + * written `''` stored `''`. On a unique column the second record cleared was |
| 11 | + * refused `409 UNIQUE_VIOLATION` (SQL `UNIQUE` reads `''` as a value and |
| 12 | + * `NULL` as distinct from every `NULL`), and on every column the clear was not |
| 13 | + * empty to `$empty`, whose ruled table reads only `null` as empty on these |
| 14 | + * types. Maintainer ruling B on #29346 (record 6579813296): the write door |
| 15 | + * stores `''` as `null` on every optional column whose type is string-stored |
| 16 | + * single-value on the table's `null_only` row — `select`, `radio`, `lookup`, |
| 17 | + * `master_detail`, `user`, `tree` today. |
| 18 | + * |
| 19 | + * The PHYSICAL column is read with the driver's own query builder. Pinned |
| 20 | + * beside it: the `$empty` table is unchanged (a row that already holds `''` |
| 21 | + * still answers as a value), `required` still refuses `''`, a blank on insert |
| 22 | + * takes a declared `defaultValue`, and a real duplicate is still `409`. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 26 | +import { ObjectQL } from '@objectstack/objectql'; |
| 27 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 28 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 29 | +import { RestServer } from './rest-server'; |
| 30 | + |
| 31 | +const OPTS = [{ value: 'gold', label: 'Gold' }, { value: 'silver', label: 'Silver' }]; |
| 32 | +const TARGET = { |
| 33 | + name: 'svb_target', label: 'Target', systemFields: false, |
| 34 | + fields: { id: { name: 'id', type: 'text' as const, primaryKey: true }, name: { name: 'name', type: 'text' as const } }, |
| 35 | +}; |
| 36 | +// One optional column per ruled type, plus a unique `select` and `lookup` (the |
| 37 | +// 409 the card names) and a unique-index member. |
| 38 | +const OBJ = { |
| 39 | + name: 'svb_obj', label: 'Single value blank', systemFields: false, |
| 40 | + fields: { |
| 41 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 42 | + name: { name: 'name', type: 'text' as const }, |
| 43 | + c_select: { name: 'c_select', type: 'select' as const, options: OPTS }, |
| 44 | + c_radio: { name: 'c_radio', type: 'radio' as const, options: OPTS }, |
| 45 | + c_lookup: { name: 'c_lookup', type: 'lookup' as const, reference: 'svb_target' }, |
| 46 | + c_master: { name: 'c_master', type: 'master_detail' as const, reference: 'svb_target' }, |
| 47 | + c_user: { name: 'c_user', type: 'user' as const }, |
| 48 | + c_tree: { name: 'c_tree', type: 'tree' as const }, |
| 49 | + u_select: { name: 'u_select', type: 'select' as const, options: OPTS, unique: true }, |
| 50 | + u_lookup: { name: 'u_lookup', type: 'lookup' as const, reference: 'svb_target', unique: true }, |
| 51 | + grp: { name: 'grp', type: 'text' as const }, |
| 52 | + i_lookup: { name: 'i_lookup', type: 'lookup' as const, reference: 'svb_target' }, |
| 53 | + }, |
| 54 | + indexes: [{ name: 'idx_svb_obj_grp_lookup', fields: ['grp', 'i_lookup'], unique: 'global' as const }], |
| 55 | +}; |
| 56 | +const PER_TYPE = ['c_select', 'c_radio', 'c_lookup', 'c_master', 'c_user', 'c_tree'] as const; |
| 57 | +const REQ = { |
| 58 | + name: 'svb_req', label: 'Required', systemFields: false, |
| 59 | + fields: { |
| 60 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 61 | + r_select: { name: 'r_select', type: 'select' as const, options: OPTS, required: true }, |
| 62 | + r_lookup: { name: 'r_lookup', type: 'lookup' as const, reference: 'svb_target', required: true, defaultValue: 't1' }, |
| 63 | + }, |
| 64 | +}; |
| 65 | +const DEF = { |
| 66 | + name: 'svb_def', label: 'Defaults', systemFields: false, |
| 67 | + fields: { |
| 68 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 69 | + d_select: { name: 'd_select', type: 'select' as const, options: OPTS, defaultValue: 'silver' }, |
| 70 | + d_lookup: { name: 'd_lookup', type: 'lookup' as const, reference: 'svb_target', defaultValue: 't1' }, |
| 71 | + }, |
| 72 | +}; |
| 73 | + |
| 74 | +const liveEngines: ObjectQL[] = []; |
| 75 | +afterEach(async () => { |
| 76 | + while (liveEngines.length) { |
| 77 | + try { await liveEngines.pop()?.destroy(); } catch { /* noop */ } |
| 78 | + } |
| 79 | +}); |
| 80 | + |
| 81 | +function createMockServer() { |
| 82 | + const noop = () => {}; |
| 83 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 84 | +} |
| 85 | + |
| 86 | +function makeRes() { |
| 87 | + const res: any = { |
| 88 | + write: () => true, end: () => {}, |
| 89 | + header: () => res, |
| 90 | + status: (code: number) => { res._status = code; return res; }, |
| 91 | + json: (body: any) => { res._json = body; return res; }, |
| 92 | + }; |
| 93 | + return res; |
| 94 | +} |
| 95 | + |
| 96 | +async function boot() { |
| 97 | + const engine = new ObjectQL(); |
| 98 | + liveEngines.push(engine); |
| 99 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 100 | + engine.registerDriver(driver, true); |
| 101 | + await engine.init(); |
| 102 | + for (const o of [TARGET, OBJ, REQ, DEF]) engine.registry.registerObject(o as any); |
| 103 | + await engine.syncSchemas(); |
| 104 | + |
| 105 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 106 | + const rest = new RestServer(createMockServer() as any, protocol as any, { |
| 107 | + api: { requireAuth: false }, batch: { enableBatchEndpoint: true }, |
| 108 | + } as any); |
| 109 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 110 | + rest.registerRoutes(); |
| 111 | + const call = async (method: string, path: string, params: Record<string, string>, body: unknown) => { |
| 112 | + const route = rest.getRoutes().find((r: any) => r.method === method && r.path === path); |
| 113 | + expect(route, `${method} ${path}`).toBeDefined(); |
| 114 | + const res = makeRes(); |
| 115 | + await route!.handler({ params, body, query: {}, headers: {} } as any, res); |
| 116 | + return { status: res._status ?? 200, body: res._json }; |
| 117 | + }; |
| 118 | + const knex = (driver as any).knex; |
| 119 | + /** The physical row, read past every engine read coercion. */ |
| 120 | + const stored = async (object: string, id: string) => |
| 121 | + (await knex(object).where({ id }).first()) as Record<string, unknown> | undefined; |
| 122 | + await engine.insert('svb_target', { id: 't1', name: 'T1' }); |
| 123 | + return { engine, call, stored, knex }; |
| 124 | +} |
| 125 | + |
| 126 | +type Ctx = Awaited<ReturnType<typeof boot>>; |
| 127 | +const create = (ctx: Ctx, body: Record<string, unknown>, object = 'svb_obj') => |
| 128 | + ctx.call('POST', '/api/v1/data/:object', { object }, body); |
| 129 | +const patch = (ctx: Ctx, id: string, body: Record<string, unknown>) => |
| 130 | + ctx.call('PATCH', '/api/v1/data/:object/:id', { object: 'svb_obj', id }, body); |
| 131 | +const allBlank = Object.fromEntries(PER_TYPE.map((f) => [f, ''])); |
| 132 | + |
| 133 | +describe('REST write doors on SQLite: a cleared optional single-value option / reference field stores null (#29277)', () => { |
| 134 | + let ctx: Ctx; |
| 135 | + beforeEach(async () => { ctx = await boot(); }); |
| 136 | + |
| 137 | + it('POST create with "" on every ruled type: 201, every column stores NULL', async () => { |
| 138 | + const res = await create(ctx, { id: 'p1', name: 'r1', ...allBlank }); |
| 139 | + expect(res.status).toBe(201); |
| 140 | + const row = await ctx.stored('svb_obj', 'p1'); |
| 141 | + for (const f of PER_TYPE) expect(row?.[f], f).toBeNull(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('PATCH clearing every ruled type: 200, every column stores NULL', async () => { |
| 145 | + const set = { c_select: 'gold', c_radio: 'gold', c_lookup: 't1', c_master: 't1', c_user: 'u1', c_tree: 't1' }; |
| 146 | + expect((await create(ctx, { id: 'e1', name: 'r1', ...set })).status).toBe(201); |
| 147 | + const res = await patch(ctx, 'e1', allBlank); |
| 148 | + expect(res.status).toBe(200); |
| 149 | + const row = await ctx.stored('svb_obj', 'e1'); |
| 150 | + for (const f of PER_TYPE) expect(row?.[f], f).toBeNull(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('unique select and unique lookup: two records created with "" both land (no 409)', async () => { |
| 154 | + const a = await create(ctx, { id: 'u1', name: 'r1', u_select: '', u_lookup: '' }); |
| 155 | + const b = await create(ctx, { id: 'u2', name: 'r2', u_select: '', u_lookup: '' }); |
| 156 | + expect([a.status, b.status]).toEqual([201, 201]); |
| 157 | + for (const id of ['u1', 'u2']) { |
| 158 | + const row = await ctx.stored('svb_obj', id); |
| 159 | + expect([row?.u_select, row?.u_lookup], id).toEqual([null, null]); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it('unique select and unique lookup: clearing two records by PATCH both land', async () => { |
| 164 | + await ctx.engine.insert('svb_target', { id: 't2', name: 'T2' }); |
| 165 | + expect((await create(ctx, { id: 'w1', name: 'r1', u_select: 'gold', u_lookup: 't1' })).status).toBe(201); |
| 166 | + expect((await create(ctx, { id: 'w2', name: 'r2', u_select: 'silver', u_lookup: 't2' })).status).toBe(201); |
| 167 | + const first = await patch(ctx, 'w1', { u_select: '', u_lookup: '' }); |
| 168 | + const second = await patch(ctx, 'w2', { u_select: '', u_lookup: '' }); |
| 169 | + expect([first.status, second.status]).toEqual([200, 200]); |
| 170 | + for (const id of ['w1', 'w2']) expect((await ctx.stored('svb_obj', id))?.u_lookup, id).toBeNull(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('a member of a declared unique index: same group, cleared lookup, both land', async () => { |
| 174 | + const a = await create(ctx, { id: 'i1', name: 'r1', grp: 'g', i_lookup: '' }); |
| 175 | + const b = await create(ctx, { id: 'i2', name: 'r2', grp: 'g', i_lookup: '' }); |
| 176 | + expect([a.status, b.status]).toEqual([201, 201]); |
| 177 | + expect((await ctx.stored('svb_obj', 'i2'))?.i_lookup).toBeNull(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('batch create and createMany: every row lands, every blank stores NULL', async () => { |
| 181 | + const bc = await ctx.call('POST', '/api/v1/data/:object/batch', { object: 'svb_obj' }, { |
| 182 | + operation: 'create', |
| 183 | + records: [{ data: { id: 'b1', name: 'r1', u_select: '' } }, { data: { id: 'b2', name: 'r2', u_select: '' } }], |
| 184 | + }); |
| 185 | + expect(bc.status).toBe(200); |
| 186 | + expect(bc.body.results.map((r: any) => r.success)).toEqual([true, true]); |
| 187 | + const cm = await ctx.call('POST', '/api/v1/data/:object/createMany', { object: 'svb_obj' }, |
| 188 | + [{ id: 'm1', name: 'r3', u_lookup: '' }, { id: 'm2', name: 'r4', u_lookup: '' }]); |
| 189 | + expect(cm.status).toBe(201); |
| 190 | + for (const id of ['b1', 'b2']) expect((await ctx.stored('svb_obj', id))?.u_select, id).toBeNull(); |
| 191 | + for (const id of ['m1', 'm2']) expect((await ctx.stored('svb_obj', id))?.u_lookup, id).toBeNull(); |
| 192 | + }); |
| 193 | + |
| 194 | + it('a real duplicate is still refused: 409 UNIQUE_VIOLATION', async () => { |
| 195 | + expect((await create(ctx, { id: 'd1', name: 'r1', u_lookup: 't1' })).status).toBe(201); |
| 196 | + const dup = await create(ctx, { id: 'd2', name: 'r2', u_lookup: 't1' }); |
| 197 | + expect(dup.status).toBe(409); |
| 198 | + expect(dup.body).toMatchObject({ code: 'UNIQUE_VIOLATION' }); |
| 199 | + expect(await ctx.stored('svb_obj', 'd2')).toBeUndefined(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('a required select / lookup refuses "": 400 VALIDATION_FAILED / required, with or without a defaultValue', async () => { |
| 203 | + const res = await create(ctx, { id: 'q1', r_select: '', r_lookup: '' }, 'svb_req'); |
| 204 | + expect(res.status).toBe(400); |
| 205 | + expect(res.body).toMatchObject({ code: 'VALIDATION_FAILED' }); |
| 206 | + expect(res.body.fields.map((x: any) => [x.field, x.code]).sort()).toEqual([['r_lookup', 'required'], ['r_select', 'required']]); |
| 207 | + expect(await ctx.stored('svb_req', 'q1')).toBeUndefined(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('a blank on insert takes a declared defaultValue, exactly as null does', async () => { |
| 211 | + expect((await create(ctx, { id: 'f1', d_select: '', d_lookup: '' }, 'svb_def')).status).toBe(201); |
| 212 | + expect((await create(ctx, { id: 'f2', d_select: null, d_lookup: null }, 'svb_def')).status).toBe(201); |
| 213 | + for (const id of ['f1', 'f2']) { |
| 214 | + const row = await ctx.stored('svb_def', id); |
| 215 | + expect([row?.d_select, row?.d_lookup], id).toEqual(['silver', 't1']); |
| 216 | + } |
| 217 | + }); |
| 218 | + |
| 219 | + it('the $empty table is unchanged: null is empty, a stored "" is still a value on these types', async () => { |
| 220 | + // Written past the engine, as the before-state stored it. |
| 221 | + await ctx.knex('svb_obj').insert({ id: 'legacy', name: 'old', c_select: '', c_lookup: '' }); |
| 222 | + expect((await create(ctx, { id: 'cleared', name: 'new', c_select: '', c_lookup: '' })).status).toBe(201); |
| 223 | + expect((await create(ctx, { id: 'held', name: 'v', c_select: 'gold', c_lookup: 't1' })).status).toBe(201); |
| 224 | + expect((await ctx.stored('svb_obj', 'legacy'))?.c_select).toBe(''); |
| 225 | + |
| 226 | + const ids = async (where: Record<string, unknown>) => |
| 227 | + ((await ctx.engine.find('svb_obj', { where, fields: ['id'] })) as Array<{ id: string }>).map((r) => r.id).sort(); |
| 228 | + for (const f of ['c_select', 'c_lookup']) { |
| 229 | + expect(await ids({ [f]: { $empty: true } }), f).toEqual(['cleared']); |
| 230 | + expect(await ids({ [f]: { $empty: false } }), f).toEqual(['held', 'legacy']); |
| 231 | + expect(await ids({ [f]: '' }), f).toEqual(['legacy']); |
| 232 | + } |
| 233 | + }); |
| 234 | + |
| 235 | + it('whitespace is a value and stored as sent', async () => { |
| 236 | + expect((await create(ctx, { id: 's1', name: 'r1', c_lookup: ' ' })).status).toBe(201); |
| 237 | + expect((await ctx.stored('svb_obj', 's1'))?.c_lookup).toBe(' '); |
| 238 | + }); |
| 239 | +}); |
0 commit comments