( ctx: ToJSONSchemaContext, schema: T )
| 357 | } |
| 358 | |
| 359 | export function finalize<T extends schemas.$ZodType>( |
| 360 | ctx: ToJSONSchemaContext, |
| 361 | schema: T |
| 362 | ): ZodStandardJSONSchemaPayload<T> { |
| 363 | const root = ctx.seen.get(schema); |
| 364 | if (!root) throw new Error("Unprocessed schema. This is a bug in Zod."); |
| 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 | } |
no test coverage detected