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