|
1 | 1 | // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
| 3 | +import { execFileSync } from 'node:child_process'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
3 | 5 | import { describe, it, expect, vi } from 'vitest'; |
4 | 6 | import { z } from 'zod'; |
5 | 7 | import { lazySchema } from './lazy-schema'; |
@@ -117,3 +119,100 @@ describe('lazySchema × z.toJSONSchema identity', () => { |
117 | 119 | expect((schema as any)._zod).toBe((schema as any)._zod); |
118 | 120 | }); |
119 | 121 | }); |
| 122 | + |
| 123 | +/** |
| 124 | + * #19101 — a schema referenced through the Proxy converts with the SAME |
| 125 | + * metadata as the eager instance. zod reads `.describe()` / `.meta()` from its |
| 126 | + * registry by node identity; the node is the Proxy, the metadata sits on the |
| 127 | + * real instance, so before the facade aliased it every lazy reference lost its |
| 128 | + * `description` while `OS_EAGER_SCHEMAS=1` (no Proxy at all) kept it. |
| 129 | + */ |
| 130 | +describe('lazySchema × z.toJSONSchema metadata (#19101)', () => { |
| 131 | + it('a lazy reference converts exactly like the eager instance — nested and as the root', () => { |
| 132 | + const factory = () => z.record(z.string(), z.unknown()).describe('lazy-described record'); |
| 133 | + const lazy = lazySchema(factory); |
| 134 | + const eager = factory(); |
| 135 | + expect(z.toJSONSchema(z.object({ rows: z.array(lazy) }))).toEqual( |
| 136 | + z.toJSONSchema(z.object({ rows: z.array(eager) })), |
| 137 | + ); |
| 138 | + expect(z.toJSONSchema(lazy)).toEqual(z.toJSONSchema(eager)); |
| 139 | + }); |
| 140 | + |
| 141 | + it("the real instance's own describe wins over the one it inherits from its parent", () => { |
| 142 | + const Base = z.string().describe('inherited'); |
| 143 | + const lazy = lazySchema(() => Base.describe('own')); |
| 144 | + const json = z.toJSONSchema(z.object({ v: lazy })) as { properties: { v: { description?: string } } }; |
| 145 | + expect(json.properties.v.description).toBe('own'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('`id` is NOT aliased onto the Proxy — aliasing it makes zod throw "Duplicate schema id"', () => { |
| 149 | + const Leaf: z.ZodType<any> = lazySchema(() => |
| 150 | + z.object({ x: z.string() }).meta({ id: 'LazySchemaIdProbe19101', description: 'probe' }), |
| 151 | + ); |
| 152 | + const Doc = z.object({ a: (Leaf as any).optional(), b: z.lazy(() => Leaf) }); |
| 153 | + const json = z.toJSONSchema(Doc) as { properties: { b: { description?: string } } }; |
| 154 | + expect(json.properties.b.description).toBe('probe'); |
| 155 | + expect(z.globalRegistry.get(Leaf)?.id).toBeUndefined(); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +/** |
| 160 | + * The same property on the real contract, against a REAL eager run: a child |
| 161 | + * process imports the spec with `OS_EAGER_SCHEMAS=1` (no Proxy anywhere) and |
| 162 | + * prints the conversions; this process converts the same schemas lazily. The |
| 163 | + * child enters through `kernel/metadata-type-schemas.ts`, because an eager |
| 164 | + * load that starts at `api/` or `data/` dies on the filter.zod → strict-object |
| 165 | + * → suggestions.zod → field.zod cycle filed as #19930. |
| 166 | + * |
| 167 | + * Measured at the fix (lazy before → after, eager unchanged): the nine OpenAPI |
| 168 | + * components gain 2 descriptions, every `/meta/types` schema 170, the |
| 169 | + * `os generate` IDE schema 445; description is the only key that moved. |
| 170 | + */ |
| 171 | +describe('lazy == eager on the real contract (#19101)', () => { |
| 172 | + const PKG_ROOT = fileURLToPath(new URL('../..', import.meta.url)); |
| 173 | + const CONTRACT = new URL('../api/contract.zod.ts', import.meta.url).href; |
| 174 | + const METADATA_TYPES = new URL('../kernel/metadata-type-schemas.ts', import.meta.url).href; |
| 175 | + const OPTS = { unrepresentable: 'any' } as const; |
| 176 | + // tsx compiles .ts to CJS, so a namespace may arrive under `default`. |
| 177 | + const pick = (mod: any, key: string): any => mod[key] ?? mod.default?.[key]; |
| 178 | + |
| 179 | + it('RecordDataSchema inside ListRecordResponse, and the `dataset` /meta/types schema', async () => { |
| 180 | + const eager = JSON.parse( |
| 181 | + execFileSync( |
| 182 | + process.execPath, |
| 183 | + ['--import', 'tsx', '--input-type=module', '-e', |
| 184 | + `import ${JSON.stringify(METADATA_TYPES)}; |
| 185 | + const { z } = await import('zod'); |
| 186 | + const pick = (mod, key) => mod[key] ?? mod.default?.[key]; |
| 187 | + const contract = await import(${JSON.stringify(CONTRACT)}); |
| 188 | + const types = await import(${JSON.stringify(METADATA_TYPES)}); |
| 189 | + const opts = ${JSON.stringify(OPTS)}; |
| 190 | + process.stdout.write(JSON.stringify({ |
| 191 | + listRecordResponse: z.toJSONSchema(pick(contract, 'ListRecordResponseSchema'), opts), |
| 192 | + dataset: z.toJSONSchema(pick(types, 'getMetadataTypeSchema')('dataset'), opts), |
| 193 | + }));`], |
| 194 | + { |
| 195 | + cwd: PKG_ROOT, |
| 196 | + env: { ...process.env, OS_EAGER_SCHEMAS: '1' }, |
| 197 | + encoding: 'utf8', |
| 198 | + maxBuffer: 64 * 1024 * 1024, |
| 199 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 200 | + }, |
| 201 | + ), |
| 202 | + ); |
| 203 | + |
| 204 | + const contract = await import('../api/contract.zod'); |
| 205 | + const types = await import('../kernel/metadata-type-schemas'); |
| 206 | + const lazy = JSON.parse(JSON.stringify({ |
| 207 | + listRecordResponse: z.toJSONSchema(pick(contract, 'ListRecordResponseSchema'), OPTS), |
| 208 | + dataset: z.toJSONSchema(pick(types, 'getMetadataTypeSchema')('dataset'), OPTS), |
| 209 | + })); |
| 210 | + |
| 211 | + // The named leaf first, for a failure that says what went missing. |
| 212 | + const declared = pick(contract, 'RecordDataSchema').description; |
| 213 | + expect(typeof declared === 'string' && declared.length > 0).toBe(true); |
| 214 | + expect(eager.listRecordResponse.properties.data.items.description).toBe(declared); |
| 215 | + expect(lazy.listRecordResponse.properties.data.items.description).toBe(declared); |
| 216 | + expect(lazy).toStrictEqual(eager); |
| 217 | + }, 60_000); |
| 218 | +}); |
0 commit comments