MCPcopy
hub / github.com/colinhacks/zod / flattenRef

Function flattenRef

packages/zod/src/v4/core/to-json-schema.ts:367–447  ·  view source on GitHub ↗
(zodSchema: schemas.$ZodType)

Source from the content-addressed store, hash-verified

365
366 // flatten refs - inherit properties from parent schemas
367 const flattenRef = (zodSchema: schemas.$ZodType) => {
368 const seen = ctx.seen.get(zodSchema)!;
369
370 // already processed
371 if (seen.ref === null) return;
372
373 const schema = seen.def ?? seen.schema;
374 const _cached = { ...schema };
375
376 const ref = seen.ref;
377 seen.ref = null; // prevent infinite recursion
378
379 if (ref) {
380 flattenRef(ref);
381
382 const refSeen = ctx.seen.get(ref)!;
383 const refSchema = refSeen.schema;
384
385 // merge referenced schema into current
386 if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
387 // older drafts can't combine $ref with other properties
388 schema.allOf = schema.allOf ?? [];
389 schema.allOf.push(refSchema);
390 } else {
391 Object.assign(schema, refSchema);
392 }
393 // restore child's own properties (child wins)
394 Object.assign(schema, _cached);
395
396 const isParentRef = zodSchema._zod.parent === ref;
397
398 // For parent chain, child is a refinement - remove parent-only properties
399 if (isParentRef) {
400 for (const key in schema) {
401 if (key === "$ref" || key === "allOf") continue;
402 if (!(key in _cached)) {
403 delete schema[key];
404 }
405 }
406 }
407
408 // When ref was extracted to $defs, remove properties that match the definition
409 if (refSchema.$ref && refSeen.def) {
410 for (const key in schema) {
411 if (key === "$ref" || key === "allOf") continue;
412 if (key in refSeen.def && JSON.stringify(schema[key]) === JSON.stringify(refSeen.def[key])) {
413 delete schema[key];
414 }
415 }
416 }
417 }
418
419 // If parent was extracted (has $ref), propagate $ref to this schema
420 // This handles cases like: readonly().meta({id}).describe()
421 // where processor sets ref to innerType but parent should be referenced
422 const parent = zodSchema._zod.parent;
423 if (parent && parent !== ref) {
424 // Ensure parent is processed first so its def has inherited properties

Callers 1

finalizeFunction · 0.85

Calls 2

getMethod · 0.80
overrideMethod · 0.80

Tested by

no test coverage detected