zod-openapi 3.3.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,19 +9,125 @@ const isAnyZodType = (zodType) => {
9
9
  (_a = zodType == null ? void 0 : zodType._def) == null ? void 0 : _a.typeName
10
10
  );
11
11
  };
12
- const enhanceWithMetadata = (schema, metadata) => {
12
+ const openApiVersions = [
13
+ "3.0.0",
14
+ "3.0.1",
15
+ "3.0.2",
16
+ "3.0.3",
17
+ "3.1.0"
18
+ ];
19
+ const satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
20
+ const createDescriptionMetadata = (schema, description, state) => {
21
+ if (satisfiesVersion(state.components.openapi, "3.1.0")) {
22
+ return {
23
+ type: "ref",
24
+ schema: {
25
+ $ref: schema.schema.$ref,
26
+ description
27
+ },
28
+ zodType: schema.zodType,
29
+ effects: schema.effects,
30
+ schemaObject: schema.schemaObject
31
+ };
32
+ }
33
+ return {
34
+ type: "schema",
35
+ schema: {
36
+ description,
37
+ allOf: [schema.schema]
38
+ },
39
+ effects: schema.effects
40
+ };
41
+ };
42
+ const isValueEqual = (value, previous) => {
43
+ if (typeof value !== typeof previous) {
44
+ return false;
45
+ }
46
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
47
+ return value === previous;
48
+ }
49
+ if (Array.isArray(value) && Array.isArray(previous)) {
50
+ const sorted = [...value].sort();
51
+ const previousSorted = [...previous].sort();
52
+ return sorted.every((v, i) => isValueEqual(v, previousSorted[i]));
53
+ }
54
+ if (value === null || previous === null) {
55
+ return value === previous;
56
+ }
57
+ if (typeof value === "object" && typeof previous === "object") {
58
+ const keys = Object.keys(value);
59
+ return keys.every(
60
+ (key) => isValueEqual(
61
+ value[key],
62
+ previous[key]
63
+ )
64
+ );
65
+ }
66
+ return value === previous;
67
+ };
68
+ const enhanceWithMetadata = (schema, metadata, state, previous) => {
69
+ const values = Object.entries(metadata).reduce(
70
+ (acc, [key, value]) => {
71
+ if (value === void 0) {
72
+ return acc;
73
+ }
74
+ acc[key] = value;
75
+ return acc;
76
+ },
77
+ {}
78
+ );
79
+ const length = Object.values(values).length;
13
80
  if (schema.type === "ref") {
14
- if (Object.values(metadata).every((val) => val === void 0)) {
81
+ if (length === 0) {
15
82
  return schema;
16
83
  }
84
+ if (length === 1 && metadata.description) {
85
+ return createDescriptionMetadata(schema, metadata.description, state);
86
+ }
17
87
  return {
18
88
  type: "schema",
19
89
  schema: {
20
- allOf: [schema.schema, metadata]
90
+ allOf: [schema.schema],
91
+ ...metadata
21
92
  },
22
93
  effects: schema.effects
23
94
  };
24
95
  }
96
+ if (previous && schema.schema.type !== "object") {
97
+ const diff = Object.entries({ ...schema.schema, ...values }).reduce(
98
+ (acc, [key, value]) => {
99
+ if (previous.schemaObject && isValueEqual(
100
+ previous.schemaObject[key],
101
+ value
102
+ )) {
103
+ return acc;
104
+ }
105
+ acc[key] = value;
106
+ return acc;
107
+ },
108
+ {}
109
+ );
110
+ const diffLength = Object.values(diff).length;
111
+ if (diffLength === 0) {
112
+ return {
113
+ type: "ref",
114
+ schema: {
115
+ $ref: previous.schema.$ref
116
+ },
117
+ effects: schema.effects,
118
+ schemaObject: previous.schemaObject,
119
+ zodType: previous.zodType
120
+ };
121
+ }
122
+ if (diffLength === 1 && typeof diff.description === "string") {
123
+ return createDescriptionMetadata(previous, diff.description, state);
124
+ }
125
+ return {
126
+ type: "schema",
127
+ schema: { allOf: [previous.schema], ...diff },
128
+ effects: schema.effects
129
+ };
130
+ }
25
131
  return {
26
132
  type: "schema",
27
133
  schema: {
@@ -55,17 +161,15 @@ const createBooleanSchema = (_zodBoolean) => ({
55
161
  }
56
162
  });
57
163
  const createBrandedSchema = (zodBranded, state) => createSchemaObject(zodBranded._def.type, state, ["brand"]);
58
- const createCatchSchema = (zodCatch, state) => {
164
+ const createCatchSchema = (zodCatch, state, previous) => {
59
165
  const schemaObject = createSchemaObject(zodCatch._def.innerType, state, [
60
166
  "default"
61
167
  ]);
62
168
  const catchResult = zodCatch.safeParse(void 0);
63
169
  const maybeDefaultValue = catchResult.success ? {
64
170
  default: catchResult.data
65
- } : void 0;
66
- return enhanceWithMetadata(schemaObject, {
67
- ...maybeDefaultValue
68
- });
171
+ } : {};
172
+ return enhanceWithMetadata(schemaObject, maybeDefaultValue, state, previous);
69
173
  };
70
174
  const createDateSchema = (_zodDate, state) => {
71
175
  var _a;
@@ -76,22 +180,19 @@ const createDateSchema = (_zodDate, state) => {
76
180
  }
77
181
  };
78
182
  };
79
- const createDefaultSchema = (zodDefault, state) => {
183
+ const createDefaultSchema = (zodDefault, state, previous) => {
80
184
  const schemaObject = createSchemaObject(zodDefault._def.innerType, state, [
81
185
  "default"
82
186
  ]);
83
- return enhanceWithMetadata(schemaObject, {
84
- default: zodDefault._def.defaultValue()
85
- });
187
+ return enhanceWithMetadata(
188
+ schemaObject,
189
+ {
190
+ default: zodDefault._def.defaultValue()
191
+ },
192
+ state,
193
+ previous
194
+ );
86
195
  };
87
- const openApiVersions = [
88
- "3.0.0",
89
- "3.0.1",
90
- "3.0.2",
91
- "3.0.3",
92
- "3.1.0"
93
- ];
94
- const satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
95
196
  const createNativeEnumSchema = (zodEnum, state) => {
96
197
  const enumValues = getValidEnumValues(zodEnum._def.values);
97
198
  const { numbers, strings } = sortStringsAndNumbers(enumValues);
@@ -143,14 +244,14 @@ const sortStringsAndNumbers = (values) => ({
143
244
  numbers: values.filter((value) => typeof value === "number")
144
245
  });
145
246
  const createTransformSchema = (zodTransform, state) => {
146
- var _a, _b, _c;
147
- if (((_a = zodTransform._def.openapi) == null ? void 0 : _a.effectType) === "output") {
247
+ var _a, _b, _c, _d, _e, _f;
248
+ if (((_b = (_a = zodTransform._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.effectType) === "output") {
148
249
  return {
149
250
  type: "schema",
150
251
  schema: createManualOutputTransformSchema(zodTransform, state)
151
252
  };
152
253
  }
153
- if (((_b = zodTransform._def.openapi) == null ? void 0 : _b.effectType) === "input" || ((_c = zodTransform._def.openapi) == null ? void 0 : _c.effectType) === "same") {
254
+ if (((_d = (_c = zodTransform._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.effectType) === "input" || ((_f = (_e = zodTransform._def.zodOpenApi) == null ? void 0 : _e.openapi) == null ? void 0 : _f.effectType) === "same") {
154
255
  return createSchemaObject(zodTransform._def.schema, state, [
155
256
  "transform input"
156
257
  ]);
@@ -180,18 +281,18 @@ const createTransformSchema = (zodTransform, state) => {
180
281
  };
181
282
  };
182
283
  const createManualOutputTransformSchema = (zodTransform, state) => {
183
- var _a;
184
- if (!((_a = zodTransform._def.openapi) == null ? void 0 : _a.type)) {
284
+ var _a, _b, _c;
285
+ if (!((_b = (_a = zodTransform._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type)) {
185
286
  const zodType = zodTransform.constructor.name;
186
287
  const schemaName = `${zodType} - ${zodTransform._def.effect.type}`;
187
288
  throw new Error(
188
289
  `Failed to determine a type for ${schemaName} at ${state.path.join(
189
290
  " > "
190
- )}. Please change the 'effectType' to 'input', wrap it in a ZodPipeline or assign it a manual 'type'.`
291
+ )}. Please change the 'effectType' to 'same' or 'input', wrap it in a ZodPipeline or assign it a manual 'type'.`
191
292
  );
192
293
  }
193
294
  return {
194
- type: zodTransform._def.openapi.type
295
+ type: (_c = zodTransform._def.zodOpenApi) == null ? void 0 : _c.openapi.type
195
296
  };
196
297
  };
197
298
  const getZodTypeName = (zodType) => {
@@ -383,20 +484,26 @@ const createEnumSchema = (zodEnum) => ({
383
484
  }
384
485
  });
385
486
  const createIntersectionSchema = (zodIntersection, state) => {
386
- const left = createSchemaObject(zodIntersection._def.left, state, [
387
- "intersection left"
388
- ]);
389
- const right = createSchemaObject(zodIntersection._def.right, state, [
390
- "intersection right"
391
- ]);
487
+ const schemas = flattenIntersection(zodIntersection);
488
+ const allOfs = schemas.map(
489
+ (schema, index) => createSchemaObject(schema, state, [`intersection ${index}`])
490
+ );
392
491
  return {
393
492
  type: "schema",
394
493
  schema: {
395
- allOf: [left.schema, right.schema]
494
+ allOf: allOfs.map((schema) => schema.schema)
396
495
  },
397
- effects: flattenEffects([left.effects, right.effects])
496
+ effects: flattenEffects(allOfs.map((schema) => schema.effects))
398
497
  };
399
498
  };
499
+ const flattenIntersection = (zodType) => {
500
+ if (!isZodType(zodType, "ZodIntersection")) {
501
+ return [zodType];
502
+ }
503
+ const leftSchemas = flattenIntersection(zodType._def.left);
504
+ const rightSchemas = flattenIntersection(zodType._def.right);
505
+ return [...leftSchemas, ...rightSchemas];
506
+ };
400
507
  const createLazySchema = (zodLazy, state) => {
401
508
  const innerSchema = zodLazy._def.getter();
402
509
  return createSchemaObject(innerSchema, state, ["lazy schema"]);
@@ -429,8 +536,8 @@ const createLiteralSchema = (zodLiteral, state) => {
429
536
  };
430
537
  };
431
538
  const createManualTypeSchema = (zodSchema, state) => {
432
- var _a;
433
- if (!((_a = zodSchema._def.openapi) == null ? void 0 : _a.type)) {
539
+ var _a, _b, _c;
540
+ if (!((_b = (_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type)) {
434
541
  const schemaName = zodSchema.constructor.name;
435
542
  throw new Error(
436
543
  `Unknown schema ${schemaName} at ${state.path.join(
@@ -441,7 +548,7 @@ const createManualTypeSchema = (zodSchema, state) => {
441
548
  return {
442
549
  type: "schema",
443
550
  schema: {
444
- type: zodSchema._def.openapi.type
551
+ type: (_c = zodSchema._def.zodOpenApi) == null ? void 0 : _c.openapi.type
445
552
  }
446
553
  };
447
554
  };
@@ -534,16 +641,19 @@ const createNumberSchema = (zodNumber, state) => {
534
641
  const zodNumberChecks = getZodNumberChecks(zodNumber);
535
642
  const minimum = mapMinimum(zodNumberChecks, state.components.openapi);
536
643
  const maximum = mapMaximum(zodNumberChecks, state.components.openapi);
644
+ const multipleOf = mapMultipleOf(zodNumberChecks);
537
645
  return {
538
646
  type: "schema",
539
647
  schema: {
540
648
  type: mapNumberType(zodNumberChecks),
649
+ ...multipleOf && multipleOf,
541
650
  ...minimum && minimum,
542
651
  // Union types are not easy to tame
543
652
  ...maximum && maximum
544
653
  }
545
654
  };
546
655
  };
656
+ const mapMultipleOf = (zodNumberCheck) => zodNumberCheck.multipleOf ? { multipleOf: zodNumberCheck.multipleOf.value } : void 0;
547
657
  const mapMaximum = (zodNumberCheck, openapi) => {
548
658
  if (!zodNumberCheck.max) {
549
659
  return void 0;
@@ -577,11 +687,10 @@ const getZodNumberChecks = (zodNumber) => zodNumber._def.checks.reduce((acc, che
577
687
  const mapNumberType = (zodNumberChecks) => zodNumberChecks.int ? "integer" : "number";
578
688
  const createOptionalSchema = (zodOptional, state) => createSchemaObject(zodOptional.unwrap(), state, ["optional"]);
579
689
  const isOptionalObjectKey = (zodSchema) => isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined") || isZodType(zodSchema, "ZodLiteral") && zodSchema._def.value === void 0;
580
- const createObjectSchema = (zodObject, state) => {
581
- var _a;
690
+ const createObjectSchema = (zodObject, previous, state) => {
582
691
  const extendedSchema = createExtendedSchema(
583
692
  zodObject,
584
- (_a = zodObject._def.extendMetadata) == null ? void 0 : _a.extends,
693
+ previous == null ? void 0 : previous.zodType,
585
694
  state
586
695
  );
587
696
  if (extendedSchema) {
@@ -597,12 +706,12 @@ const createObjectSchema = (zodObject, state) => {
597
706
  );
598
707
  };
599
708
  const createExtendedSchema = (zodObject, baseZodObject, state) => {
600
- var _a, _b;
709
+ var _a, _b, _c, _d, _e;
601
710
  if (!baseZodObject) {
602
711
  return void 0;
603
712
  }
604
713
  const component = state.components.schemas.get(baseZodObject);
605
- if (component ?? ((_a = baseZodObject._def.openapi) == null ? void 0 : _a.ref)) {
714
+ if (component ?? ((_b = (_a = baseZodObject._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.ref)) {
606
715
  createSchemaObject(baseZodObject, state, ["extended schema"]);
607
716
  }
608
717
  const completeComponent = state.components.schemas.get(baseZodObject);
@@ -632,8 +741,53 @@ const createExtendedSchema = (zodObject, baseZodObject, state) => {
632
741
  const extendedSchema = createObjectSchemaFromShape(
633
742
  diffShape,
634
743
  diffOpts,
635
- state
744
+ state,
745
+ true
636
746
  );
747
+ const schemaLength = Object.keys(extendedSchema.schema).length;
748
+ const effects = flattenEffects([
749
+ completeComponent.type === "complete" ? completeComponent.effects : [],
750
+ completeComponent.type === "in-progress" ? [
751
+ {
752
+ type: "component",
753
+ zodType: zodObject,
754
+ path: [...state.path]
755
+ }
756
+ ] : [],
757
+ extendedSchema.effects
758
+ ]);
759
+ if (schemaLength === 0) {
760
+ return {
761
+ type: "ref",
762
+ schema: {
763
+ $ref: createComponentSchemaRef(
764
+ completeComponent.ref,
765
+ (_c = state.documentOptions) == null ? void 0 : _c.componentRefPath
766
+ )
767
+ },
768
+ schemaObject: completeComponent.type === "complete" ? completeComponent.schemaObject : void 0,
769
+ zodType: zodObject,
770
+ effects
771
+ };
772
+ }
773
+ if (schemaLength === 1 && extendedSchema.schema.description) {
774
+ return createDescriptionMetadata(
775
+ {
776
+ type: "ref",
777
+ schema: {
778
+ $ref: createComponentSchemaRef(
779
+ completeComponent.ref,
780
+ (_d = state.documentOptions) == null ? void 0 : _d.componentRefPath
781
+ )
782
+ },
783
+ schemaObject: completeComponent.type === "complete" ? completeComponent.schemaObject : void 0,
784
+ zodType: zodObject,
785
+ effects
786
+ },
787
+ extendedSchema.schema.description,
788
+ state
789
+ );
790
+ }
637
791
  return {
638
792
  type: "schema",
639
793
  schema: {
@@ -641,7 +795,7 @@ const createExtendedSchema = (zodObject, baseZodObject, state) => {
641
795
  {
642
796
  $ref: createComponentSchemaRef(
643
797
  completeComponent.ref,
644
- (_b = state.documentOptions) == null ? void 0 : _b.componentRefPath
798
+ (_e = state.documentOptions) == null ? void 0 : _e.componentRefPath
645
799
  )
646
800
  }
647
801
  ],
@@ -684,14 +838,14 @@ const createShapeDiff = (baseObj, extendedObj) => {
684
838
  }
685
839
  return acc;
686
840
  };
687
- const createObjectSchemaFromShape = (shape, { unknownKeys, catchAll }, state) => {
841
+ const createObjectSchemaFromShape = (shape, { unknownKeys, catchAll }, state, omitType) => {
688
842
  const properties = mapProperties(shape, state);
689
843
  const required = mapRequired(properties, shape, state);
690
844
  const additionalProperties = !isZodType(catchAll, "ZodNever") ? createSchemaObject(catchAll, state, ["additional properties"]) : void 0;
691
845
  return {
692
846
  type: "schema",
693
847
  schema: {
694
- type: "object",
848
+ ...!omitType && { type: "object" },
695
849
  ...properties && { properties: properties.properties },
696
850
  ...(required == null ? void 0 : required.required.length) && { required: required.required },
697
851
  ...unknownKeys === "strict" && { additionalProperties: false },
@@ -772,11 +926,11 @@ const mapProperties = (shape, state) => {
772
926
  );
773
927
  };
774
928
  const createPipelineSchema = (zodPipeline, state) => {
775
- var _a, _b, _c;
776
- if (((_a = zodPipeline._def.openapi) == null ? void 0 : _a.effectType) === "input" || ((_b = zodPipeline._def.openapi) == null ? void 0 : _b.effectType) === "same") {
929
+ var _a, _b, _c, _d, _e, _f;
930
+ if (((_b = (_a = zodPipeline._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.effectType) === "input" || ((_d = (_c = zodPipeline._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.effectType) === "same") {
777
931
  return createSchemaObject(zodPipeline._def.in, state, ["pipeline input"]);
778
932
  }
779
- if (((_c = zodPipeline._def.openapi) == null ? void 0 : _c.effectType) === "output") {
933
+ if (((_f = (_e = zodPipeline._def.zodOpenApi) == null ? void 0 : _e.openapi) == null ? void 0 : _f.effectType) === "output") {
780
934
  return createSchemaObject(zodPipeline._def.out, state, ["pipeline output"]);
781
935
  }
782
936
  if (state.type === "input") {
@@ -1105,14 +1259,14 @@ const mapPrefixItems = (items, state) => {
1105
1259
  return void 0;
1106
1260
  };
1107
1261
  const createUnionSchema = (zodUnion, state) => {
1108
- var _a, _b;
1262
+ var _a, _b, _c;
1109
1263
  const schemas = zodUnion.options.reduce((acc, option, index) => {
1110
1264
  if (!isOptionalObjectKey(option)) {
1111
1265
  acc.push(createSchemaObject(option, state, [`union option ${index}`]));
1112
1266
  }
1113
1267
  return acc;
1114
1268
  }, []);
1115
- if (((_a = zodUnion._def.openapi) == null ? void 0 : _a.unionOneOf) ?? ((_b = state.documentOptions) == null ? void 0 : _b.unionOneOf)) {
1269
+ if (((_b = (_a = zodUnion._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.unionOneOf) ?? ((_c = state.documentOptions) == null ? void 0 : _c.unionOneOf)) {
1116
1270
  return {
1117
1271
  type: "schema",
1118
1272
  schema: {
@@ -1133,9 +1287,9 @@ const createUnknownSchema = (_zodUnknown) => ({
1133
1287
  type: "schema",
1134
1288
  schema: {}
1135
1289
  });
1136
- const createSchemaSwitch = (zodSchema, state) => {
1137
- var _a;
1138
- if ((_a = zodSchema._def.openapi) == null ? void 0 : _a.type) {
1290
+ const createSchemaSwitch = (zodSchema, previous, state) => {
1291
+ var _a, _b;
1292
+ if ((_b = (_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type) {
1139
1293
  return createManualTypeSchema(zodSchema, state);
1140
1294
  }
1141
1295
  if (isZodType(zodSchema, "ZodString")) {
@@ -1160,7 +1314,7 @@ const createSchemaSwitch = (zodSchema, state) => {
1160
1314
  return createArraySchema(zodSchema, state);
1161
1315
  }
1162
1316
  if (isZodType(zodSchema, "ZodObject")) {
1163
- return createObjectSchema(zodSchema, state);
1317
+ return createObjectSchema(zodSchema, previous, state);
1164
1318
  }
1165
1319
  if (isZodType(zodSchema, "ZodUnion")) {
1166
1320
  return createUnionSchema(zodSchema, state);
@@ -1181,7 +1335,7 @@ const createSchemaSwitch = (zodSchema, state) => {
1181
1335
  return createReadonlySchema(zodSchema, state);
1182
1336
  }
1183
1337
  if (isZodType(zodSchema, "ZodDefault")) {
1184
- return createDefaultSchema(zodSchema, state);
1338
+ return createDefaultSchema(zodSchema, state, previous);
1185
1339
  }
1186
1340
  if (isZodType(zodSchema, "ZodRecord")) {
1187
1341
  return createRecordSchema(zodSchema, state);
@@ -1211,7 +1365,7 @@ const createSchemaSwitch = (zodSchema, state) => {
1211
1365
  return createIntersectionSchema(zodSchema, state);
1212
1366
  }
1213
1367
  if (isZodType(zodSchema, "ZodCatch")) {
1214
- return createCatchSchema(zodSchema, state);
1368
+ return createCatchSchema(zodSchema, state, previous);
1215
1369
  }
1216
1370
  if (isZodType(zodSchema, "ZodUnknown") || isZodType(zodSchema, "ZodAny")) {
1217
1371
  return createUnknownSchema();
@@ -1227,8 +1381,12 @@ const createSchemaSwitch = (zodSchema, state) => {
1227
1381
  }
1228
1382
  return createManualTypeSchema(zodSchema, state);
1229
1383
  };
1230
- const isDescriptionEqual = (schema, zodSchema) => schema.type === "ref" && zodSchema.description === schema.zodType.description;
1231
- const createNewSchema = (zodSchema, state) => {
1384
+ const createNewSchema = ({
1385
+ zodSchema,
1386
+ previous,
1387
+ state
1388
+ }) => {
1389
+ var _a;
1232
1390
  if (state.visited.has(zodSchema)) {
1233
1391
  throw new Error(
1234
1392
  `The schema at ${state.path.join(
@@ -1245,25 +1403,35 @@ const createNewSchema = (zodSchema, state) => {
1245
1403
  refType,
1246
1404
  unionOneOf,
1247
1405
  ...additionalMetadata
1248
- } = zodSchema._def.openapi ?? {};
1249
- const schema = createSchemaSwitch(zodSchema, state);
1250
- const description = zodSchema.description && !isDescriptionEqual(schema, zodSchema) ? zodSchema.description : void 0;
1251
- const schemaWithMetadata = enhanceWithMetadata(schema, {
1252
- ...description && { description },
1253
- ...additionalMetadata
1254
- });
1406
+ } = ((_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) ?? {};
1407
+ const schema = createSchemaSwitch(zodSchema, previous, state);
1408
+ const schemaWithMetadata = enhanceWithMetadata(
1409
+ schema,
1410
+ additionalMetadata,
1411
+ state,
1412
+ previous
1413
+ );
1255
1414
  state.visited.delete(zodSchema);
1256
1415
  return schemaWithMetadata;
1257
1416
  };
1258
- const createNewRef = (ref, zodSchema, state) => {
1417
+ const createNewRef = ({
1418
+ previous,
1419
+ ref,
1420
+ zodSchema,
1421
+ state
1422
+ }) => {
1259
1423
  var _a;
1260
1424
  state.components.schemas.set(zodSchema, {
1261
1425
  type: "in-progress",
1262
1426
  ref
1263
1427
  });
1264
- const newSchema = createNewSchema(zodSchema, {
1265
- ...state,
1266
- visited: /* @__PURE__ */ new Set()
1428
+ const newSchema = createNewSchema({
1429
+ zodSchema,
1430
+ previous,
1431
+ state: {
1432
+ ...state,
1433
+ visited: /* @__PURE__ */ new Set()
1434
+ }
1267
1435
  });
1268
1436
  state.components.schemas.set(zodSchema, {
1269
1437
  type: "complete",
@@ -1279,6 +1447,7 @@ const createNewRef = (ref, zodSchema, state) => {
1279
1447
  (_a = state.documentOptions) == null ? void 0 : _a.componentRefPath
1280
1448
  )
1281
1449
  },
1450
+ schemaObject: newSchema.schema,
1282
1451
  effects: newSchema.effects ? [
1283
1452
  {
1284
1453
  type: "component",
@@ -1300,6 +1469,7 @@ const createExistingRef = (zodSchema, component, state) => {
1300
1469
  (_a = state.documentOptions) == null ? void 0 : _a.componentRefPath
1301
1470
  )
1302
1471
  },
1472
+ schemaObject: component.schemaObject,
1303
1473
  effects: component.effects ? [
1304
1474
  {
1305
1475
  type: "component",
@@ -1319,6 +1489,7 @@ const createExistingRef = (zodSchema, component, state) => {
1319
1489
  (_b = state.documentOptions) == null ? void 0 : _b.componentRefPath
1320
1490
  )
1321
1491
  },
1492
+ schemaObject: void 0,
1322
1493
  effects: [
1323
1494
  {
1324
1495
  type: "component",
@@ -1331,22 +1502,30 @@ const createExistingRef = (zodSchema, component, state) => {
1331
1502
  }
1332
1503
  return;
1333
1504
  };
1334
- const createSchemaOrRef = (zodSchema, state) => {
1335
- var _a;
1505
+ const createSchemaOrRef = (zodSchema, state, onlyRef) => {
1506
+ var _a, _b, _c, _d;
1336
1507
  const component = state.components.schemas.get(zodSchema);
1337
1508
  const existingRef = createExistingRef(zodSchema, component, state);
1338
1509
  if (existingRef) {
1339
1510
  return existingRef;
1340
1511
  }
1341
- const ref = ((_a = zodSchema._def.openapi) == null ? void 0 : _a.ref) ?? (component == null ? void 0 : component.ref);
1512
+ const previous = ((_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.previous) ? createSchemaOrRef(zodSchema._def.zodOpenApi.previous, state, true) : void 0;
1513
+ const current = ((_b = zodSchema._def.zodOpenApi) == null ? void 0 : _b.current) && zodSchema._def.zodOpenApi.current !== zodSchema ? createSchemaOrRef(zodSchema._def.zodOpenApi.current, state, true) : void 0;
1514
+ const ref = ((_d = (_c = zodSchema._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.ref) ?? (component == null ? void 0 : component.ref);
1342
1515
  if (ref) {
1343
- return createNewRef(ref, zodSchema, state);
1516
+ return current ? createNewSchema({ zodSchema, previous: current, state }) : createNewRef({ ref, zodSchema, previous, state });
1517
+ }
1518
+ if (onlyRef) {
1519
+ return previous ?? current;
1344
1520
  }
1345
- return createNewSchema(zodSchema, state);
1521
+ return createNewSchema({ zodSchema, previous: previous ?? current, state });
1346
1522
  };
1347
1523
  const createSchemaObject = (zodSchema, state, subpath) => {
1348
1524
  state.path.push(...subpath);
1349
1525
  const schema = createSchemaOrRef(zodSchema, state);
1526
+ if (!schema) {
1527
+ throw new Error("Schema does not exist");
1528
+ }
1350
1529
  state.path.pop();
1351
1530
  return schema;
1352
1531
  };
@@ -1409,8 +1588,8 @@ const createContent = (contentObject, components, type, subpath, documentOptions
1409
1588
  );
1410
1589
  const createComponentParamRef = (ref) => `#/components/parameters/${ref}`;
1411
1590
  const createBaseParameter = (schema, components, subpath, documentOptions) => {
1412
- var _a, _b;
1413
- const { ref, ...rest } = ((_a = schema._def.openapi) == null ? void 0 : _a.param) ?? {};
1591
+ var _a, _b, _c, _d;
1592
+ const { ref, ...rest } = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) ?? {};
1414
1593
  const state = {
1415
1594
  components,
1416
1595
  type: "input",
@@ -1420,7 +1599,7 @@ const createBaseParameter = (schema, components, subpath, documentOptions) => {
1420
1599
  };
1421
1600
  const schemaObject = createSchema(schema, state, [...subpath, "schema"]);
1422
1601
  const required = !schema.isOptional();
1423
- const description = ((_b = schema._def.openapi) == null ? void 0 : _b.description) ?? schema._def.description;
1602
+ const description = ((_d = (_c = schema._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.description) ?? schema._def.description;
1424
1603
  return {
1425
1604
  ...description && { description },
1426
1605
  ...rest,
@@ -1431,8 +1610,8 @@ const createBaseParameter = (schema, components, subpath, documentOptions) => {
1431
1610
  const createParamOrRef = (zodSchema, components, subpath, type, name, documentOptions) => {
1432
1611
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1433
1612
  const component = components.parameters.get(zodSchema);
1434
- const paramType = ((_c = (_b = (_a = zodSchema._def) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.in) ?? (component == null ? void 0 : component.in) ?? type;
1435
- const paramName = ((_f = (_e = (_d = zodSchema._def) == null ? void 0 : _d.openapi) == null ? void 0 : _e.param) == null ? void 0 : _f.name) ?? (component == null ? void 0 : component.name) ?? name;
1613
+ const paramType = ((_c = (_b = (_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.in) ?? (component == null ? void 0 : component.in) ?? type;
1614
+ const paramName = ((_f = (_e = (_d = zodSchema._def.zodOpenApi) == null ? void 0 : _d.openapi) == null ? void 0 : _e.param) == null ? void 0 : _f.name) ?? (component == null ? void 0 : component.name) ?? name;
1436
1615
  if (!paramType) {
1437
1616
  throw new Error("Parameter type missing");
1438
1617
  }
@@ -1456,7 +1635,7 @@ const createParamOrRef = (zodSchema, components, subpath, type, name, documentOp
1456
1635
  if ("$ref" in baseParamOrRef) {
1457
1636
  throw new Error("Unexpected Error: received a reference object");
1458
1637
  }
1459
- const ref = ((_i = (_h = (_g = zodSchema == null ? void 0 : zodSchema._def) == null ? void 0 : _g.openapi) == null ? void 0 : _h.param) == null ? void 0 : _i.ref) ?? (component == null ? void 0 : component.ref);
1638
+ const ref = ((_i = (_h = (_g = zodSchema == null ? void 0 : zodSchema._def.zodOpenApi) == null ? void 0 : _g.openapi) == null ? void 0 : _h.param) == null ? void 0 : _i.ref) ?? (component == null ? void 0 : component.ref);
1460
1639
  const paramObject = {
1461
1640
  in: paramType,
1462
1641
  name: paramName,
@@ -1603,7 +1782,7 @@ const createHeaderOrRef = (schema, components, documentOptions) => {
1603
1782
  if ("$ref" in baseHeader) {
1604
1783
  throw new Error("Unexpected Error: received a reference object");
1605
1784
  }
1606
- const ref = ((_c = (_b = (_a = schema._def) == null ? void 0 : _a.openapi) == null ? void 0 : _b.header) == null ? void 0 : _c.ref) ?? (component == null ? void 0 : component.ref);
1785
+ const ref = ((_c = (_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.header) == null ? void 0 : _c.ref) ?? (component == null ? void 0 : component.ref);
1607
1786
  if (ref) {
1608
1787
  components.headers.set(schema, {
1609
1788
  type: "complete",
@@ -1617,8 +1796,8 @@ const createHeaderOrRef = (schema, components, documentOptions) => {
1617
1796
  return baseHeader;
1618
1797
  };
1619
1798
  const createBaseHeader = (schema, components, documentOptions) => {
1620
- var _a;
1621
- const { ref, ...rest } = ((_a = schema._def.openapi) == null ? void 0 : _a.header) ?? {};
1799
+ var _a, _b;
1800
+ const { ref, ...rest } = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.header) ?? {};
1622
1801
  const state = {
1623
1802
  components,
1624
1803
  type: "output",
@@ -1878,14 +2057,14 @@ const getSchemas = (schemas, components) => {
1878
2057
  return;
1879
2058
  }
1880
2059
  Object.entries(schemas).forEach(([key, schema]) => {
1881
- var _a;
2060
+ var _a, _b;
1882
2061
  if (isAnyZodType(schema)) {
1883
2062
  if (components.schemas.has(schema)) {
1884
2063
  throw new Error(
1885
2064
  `Schema ${JSON.stringify(schema._def)} is already registered`
1886
2065
  );
1887
2066
  }
1888
- const ref = ((_a = schema._def.openapi) == null ? void 0 : _a.ref) ?? key;
2067
+ const ref = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.ref) ?? key;
1889
2068
  components.schemas.set(schema, {
1890
2069
  type: "manual",
1891
2070
  ref
@@ -1898,16 +2077,16 @@ const getParameters = (parameters, components) => {
1898
2077
  return;
1899
2078
  }
1900
2079
  Object.entries(parameters).forEach(([key, schema]) => {
1901
- var _a, _b, _c, _d, _e, _f;
2080
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1902
2081
  if (isAnyZodType(schema)) {
1903
2082
  if (components.parameters.has(schema)) {
1904
2083
  throw new Error(
1905
2084
  `Parameter ${JSON.stringify(schema._def)} is already registered`
1906
2085
  );
1907
2086
  }
1908
- const ref = ((_b = (_a = schema._def.openapi) == null ? void 0 : _a.param) == null ? void 0 : _b.ref) ?? key;
1909
- const name = (_d = (_c = schema._def.openapi) == null ? void 0 : _c.param) == null ? void 0 : _d.name;
1910
- const location = (_f = (_e = schema._def.openapi) == null ? void 0 : _e.param) == null ? void 0 : _f.in;
2087
+ const ref = ((_c = (_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.ref) ?? key;
2088
+ const name = (_f = (_e = (_d = schema._def.zodOpenApi) == null ? void 0 : _d.openapi) == null ? void 0 : _e.param) == null ? void 0 : _f.name;
2089
+ const location = (_i = (_h = (_g = schema._def.zodOpenApi) == null ? void 0 : _g.openapi) == null ? void 0 : _h.param) == null ? void 0 : _i.in;
1911
2090
  if (!name || !location) {
1912
2091
  throw new Error("`name` or `in` missing in .openapi()");
1913
2092
  }
@@ -1925,14 +2104,14 @@ const getHeaders = (responseHeaders, components) => {
1925
2104
  return;
1926
2105
  }
1927
2106
  Object.entries(responseHeaders).forEach(([key, schema]) => {
1928
- var _a, _b;
2107
+ var _a, _b, _c;
1929
2108
  if (isAnyZodType(schema)) {
1930
2109
  if (components.parameters.has(schema)) {
1931
2110
  throw new Error(
1932
2111
  `Header ${JSON.stringify(schema._def)} is already registered`
1933
2112
  );
1934
2113
  }
1935
- const ref = ((_b = (_a = schema._def.openapi) == null ? void 0 : _a.param) == null ? void 0 : _b.ref) ?? key;
2114
+ const ref = ((_c = (_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.ref) ?? key;
1936
2115
  components.headers.set(schema, {
1937
2116
  type: "manual",
1938
2117
  ref
@@ -2037,11 +2216,11 @@ const createComponents = (componentsObject, components, documentOptions) => {
2037
2216
  };
2038
2217
  const createSchemaComponents = (componentsObject, components, documentOptions) => {
2039
2218
  Array.from(components.schemas).forEach(([schema, { type }], index) => {
2040
- var _a;
2219
+ var _a, _b;
2041
2220
  if (type === "manual") {
2042
2221
  const state = {
2043
2222
  components,
2044
- type: ((_a = schema._def.openapi) == null ? void 0 : _a.refType) ?? "output",
2223
+ type: ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.refType) ?? "output",
2045
2224
  path: [],
2046
2225
  visited: /* @__PURE__ */ new Set(),
2047
2226
  documentOptions