zod-openapi 3.2.0 → 4.0.0-beta.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,16 +9,122 @@ 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
92
+ },
93
+ effects: schema.effects
94
+ };
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;
21
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 },
22
128
  effects: schema.effects
23
129
  };
24
130
  }
@@ -55,7 +161,16 @@ const createBooleanSchema = (_zodBoolean) => ({
55
161
  }
56
162
  });
57
163
  const createBrandedSchema = (zodBranded, state) => createSchemaObject(zodBranded._def.type, state, ["brand"]);
58
- const createCatchSchema = (zodCatch, state) => createSchemaObject(zodCatch._def.innerType, state, ["catch"]);
164
+ const createCatchSchema = (zodCatch, state, previous) => {
165
+ const schemaObject = createSchemaObject(zodCatch._def.innerType, state, [
166
+ "default"
167
+ ]);
168
+ const catchResult = zodCatch.safeParse(void 0);
169
+ const maybeDefaultValue = catchResult.success ? {
170
+ default: catchResult.data
171
+ } : {};
172
+ return enhanceWithMetadata(schemaObject, maybeDefaultValue, state, previous);
173
+ };
59
174
  const createDateSchema = (_zodDate, state) => {
60
175
  var _a;
61
176
  return {
@@ -65,22 +180,19 @@ const createDateSchema = (_zodDate, state) => {
65
180
  }
66
181
  };
67
182
  };
68
- const createDefaultSchema = (zodDefault, state) => {
183
+ const createDefaultSchema = (zodDefault, state, previous) => {
69
184
  const schemaObject = createSchemaObject(zodDefault._def.innerType, state, [
70
185
  "default"
71
186
  ]);
72
- return enhanceWithMetadata(schemaObject, {
73
- default: zodDefault._def.defaultValue()
74
- });
187
+ return enhanceWithMetadata(
188
+ schemaObject,
189
+ {
190
+ default: zodDefault._def.defaultValue()
191
+ },
192
+ state,
193
+ previous
194
+ );
75
195
  };
76
- const openApiVersions = [
77
- "3.0.0",
78
- "3.0.1",
79
- "3.0.2",
80
- "3.0.3",
81
- "3.1.0"
82
- ];
83
- const satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
84
196
  const createNativeEnumSchema = (zodEnum, state) => {
85
197
  const enumValues = getValidEnumValues(zodEnum._def.values);
86
198
  const { numbers, strings } = sortStringsAndNumbers(enumValues);
@@ -132,14 +244,14 @@ const sortStringsAndNumbers = (values) => ({
132
244
  numbers: values.filter((value) => typeof value === "number")
133
245
  });
134
246
  const createTransformSchema = (zodTransform, state) => {
135
- var _a, _b, _c;
136
- 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") {
137
249
  return {
138
250
  type: "schema",
139
251
  schema: createManualOutputTransformSchema(zodTransform, state)
140
252
  };
141
253
  }
142
- 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") {
143
255
  return createSchemaObject(zodTransform._def.schema, state, [
144
256
  "transform input"
145
257
  ]);
@@ -169,18 +281,18 @@ const createTransformSchema = (zodTransform, state) => {
169
281
  };
170
282
  };
171
283
  const createManualOutputTransformSchema = (zodTransform, state) => {
172
- var _a;
173
- 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)) {
174
286
  const zodType = zodTransform.constructor.name;
175
287
  const schemaName = `${zodType} - ${zodTransform._def.effect.type}`;
176
288
  throw new Error(
177
289
  `Failed to determine a type for ${schemaName} at ${state.path.join(
178
290
  " > "
179
- )}. 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'.`
180
292
  );
181
293
  }
182
294
  return {
183
- type: zodTransform._def.openapi.type
295
+ type: (_c = zodTransform._def.zodOpenApi) == null ? void 0 : _c.openapi.type
184
296
  };
185
297
  };
186
298
  const getZodTypeName = (zodType) => {
@@ -372,20 +484,26 @@ const createEnumSchema = (zodEnum) => ({
372
484
  }
373
485
  });
374
486
  const createIntersectionSchema = (zodIntersection, state) => {
375
- const left = createSchemaObject(zodIntersection._def.left, state, [
376
- "intersection left"
377
- ]);
378
- const right = createSchemaObject(zodIntersection._def.right, state, [
379
- "intersection right"
380
- ]);
487
+ const schemas = flattenIntersection(zodIntersection);
488
+ const allOfs = schemas.map(
489
+ (schema, index) => createSchemaObject(schema, state, [`intersection ${index}`])
490
+ );
381
491
  return {
382
492
  type: "schema",
383
493
  schema: {
384
- allOf: [left.schema, right.schema]
494
+ allOf: allOfs.map((schema) => schema.schema)
385
495
  },
386
- effects: flattenEffects([left.effects, right.effects])
496
+ effects: flattenEffects(allOfs.map((schema) => schema.effects))
387
497
  };
388
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
+ };
389
507
  const createLazySchema = (zodLazy, state) => {
390
508
  const innerSchema = zodLazy._def.getter();
391
509
  return createSchemaObject(innerSchema, state, ["lazy schema"]);
@@ -418,8 +536,8 @@ const createLiteralSchema = (zodLiteral, state) => {
418
536
  };
419
537
  };
420
538
  const createManualTypeSchema = (zodSchema, state) => {
421
- var _a;
422
- 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)) {
423
541
  const schemaName = zodSchema.constructor.name;
424
542
  throw new Error(
425
543
  `Unknown schema ${schemaName} at ${state.path.join(
@@ -430,7 +548,7 @@ const createManualTypeSchema = (zodSchema, state) => {
430
548
  return {
431
549
  type: "schema",
432
550
  schema: {
433
- type: zodSchema._def.openapi.type
551
+ type: (_c = zodSchema._def.zodOpenApi) == null ? void 0 : _c.openapi.type
434
552
  }
435
553
  };
436
554
  };
@@ -523,16 +641,19 @@ const createNumberSchema = (zodNumber, state) => {
523
641
  const zodNumberChecks = getZodNumberChecks(zodNumber);
524
642
  const minimum = mapMinimum(zodNumberChecks, state.components.openapi);
525
643
  const maximum = mapMaximum(zodNumberChecks, state.components.openapi);
644
+ const multipleOf = mapMultipleOf(zodNumberChecks);
526
645
  return {
527
646
  type: "schema",
528
647
  schema: {
529
648
  type: mapNumberType(zodNumberChecks),
649
+ ...multipleOf && multipleOf,
530
650
  ...minimum && minimum,
531
651
  // Union types are not easy to tame
532
652
  ...maximum && maximum
533
653
  }
534
654
  };
535
655
  };
656
+ const mapMultipleOf = (zodNumberCheck) => zodNumberCheck.multipleOf ? { multipleOf: zodNumberCheck.multipleOf.value } : void 0;
536
657
  const mapMaximum = (zodNumberCheck, openapi) => {
537
658
  if (!zodNumberCheck.max) {
538
659
  return void 0;
@@ -565,79 +686,11 @@ const getZodNumberChecks = (zodNumber) => zodNumber._def.checks.reduce((acc, che
565
686
  }, {});
566
687
  const mapNumberType = (zodNumberChecks) => zodNumberChecks.int ? "integer" : "number";
567
688
  const createOptionalSchema = (zodOptional, state) => createSchemaObject(zodOptional.unwrap(), state, ["optional"]);
568
- const isOptionalSchema = (zodSchema, state) => {
569
- var _a, _b, _c;
570
- if (isZodType(zodSchema, "ZodOptional") || isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined") || isZodType(zodSchema, "ZodLiteral") && zodSchema._def.value === void 0) {
571
- return { optional: true };
572
- }
573
- if (isZodType(zodSchema, "ZodDefault")) {
574
- if (((_a = zodSchema._def.openapi) == null ? void 0 : _a.effectType) === "input") {
575
- return { optional: true };
576
- }
577
- if (((_b = zodSchema._def.openapi) == null ? void 0 : _b.effectType) === "output") {
578
- return { optional: false };
579
- }
580
- return {
581
- optional: state.type === "input",
582
- effects: [
583
- {
584
- type: "schema",
585
- creationType: state.type,
586
- zodType: zodSchema,
587
- path: [...state.path]
588
- }
589
- ]
590
- };
591
- }
592
- if (isZodType(zodSchema, "ZodNullable") || isZodType(zodSchema, "ZodCatch")) {
593
- return isOptionalSchema(zodSchema._def.innerType, state);
594
- }
595
- if (isZodType(zodSchema, "ZodEffects")) {
596
- return isOptionalSchema(zodSchema._def.schema, state);
597
- }
598
- if (isZodType(zodSchema, "ZodUnion") || isZodType(zodSchema, "ZodDiscriminatedUnion")) {
599
- const results = zodSchema._def.options.map(
600
- (schema) => isOptionalSchema(schema, state)
601
- );
602
- return results.reduce(
603
- (acc, result) => ({
604
- optional: acc.optional || result.optional,
605
- effects: flattenEffects([acc.effects, result.effects])
606
- }),
607
- { optional: false }
608
- );
609
- }
610
- if (isZodType(zodSchema, "ZodIntersection")) {
611
- const results = [zodSchema._def.left, zodSchema._def.right].map(
612
- (schema) => isOptionalSchema(schema, state)
613
- );
614
- return results.reduce(
615
- (acc, result) => ({
616
- optional: acc.optional || result.optional,
617
- effects: flattenEffects([acc.effects, result.effects])
618
- }),
619
- { optional: false }
620
- );
621
- }
622
- if (isZodType(zodSchema, "ZodPipeline")) {
623
- const type = ((_c = zodSchema._def.openapi) == null ? void 0 : _c.effectType) ?? state.type;
624
- if (type === "input") {
625
- return isOptionalSchema(zodSchema._def.in, state);
626
- }
627
- if (type === "output") {
628
- return isOptionalSchema(zodSchema._def.out, state);
629
- }
630
- }
631
- if (isZodType(zodSchema, "ZodLazy")) {
632
- return isOptionalSchema(zodSchema._def.getter(), state);
633
- }
634
- return { optional: zodSchema.isOptional() };
635
- };
636
- const createObjectSchema = (zodObject, state) => {
637
- var _a;
689
+ const isOptionalObjectKey = (zodSchema) => isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined") || isZodType(zodSchema, "ZodLiteral") && zodSchema._def.value === void 0;
690
+ const createObjectSchema = (zodObject, previous, state) => {
638
691
  const extendedSchema = createExtendedSchema(
639
692
  zodObject,
640
- (_a = zodObject._def.extendMetadata) == null ? void 0 : _a.extends,
693
+ previous == null ? void 0 : previous.zodType,
641
694
  state
642
695
  );
643
696
  if (extendedSchema) {
@@ -653,12 +706,12 @@ const createObjectSchema = (zodObject, state) => {
653
706
  );
654
707
  };
655
708
  const createExtendedSchema = (zodObject, baseZodObject, state) => {
656
- var _a, _b;
709
+ var _a, _b, _c, _d, _e;
657
710
  if (!baseZodObject) {
658
711
  return void 0;
659
712
  }
660
713
  const component = state.components.schemas.get(baseZodObject);
661
- 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)) {
662
715
  createSchemaObject(baseZodObject, state, ["extended schema"]);
663
716
  }
664
717
  const completeComponent = state.components.schemas.get(baseZodObject);
@@ -688,8 +741,53 @@ const createExtendedSchema = (zodObject, baseZodObject, state) => {
688
741
  const extendedSchema = createObjectSchemaFromShape(
689
742
  diffShape,
690
743
  diffOpts,
691
- state
744
+ state,
745
+ true
692
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
+ }
693
791
  return {
694
792
  type: "schema",
695
793
  schema: {
@@ -697,7 +795,7 @@ const createExtendedSchema = (zodObject, baseZodObject, state) => {
697
795
  {
698
796
  $ref: createComponentSchemaRef(
699
797
  completeComponent.ref,
700
- (_b = state.documentOptions) == null ? void 0 : _b.componentRefPath
798
+ (_e = state.documentOptions) == null ? void 0 : _e.componentRefPath
701
799
  )
702
800
  }
703
801
  ],
@@ -740,14 +838,14 @@ const createShapeDiff = (baseObj, extendedObj) => {
740
838
  }
741
839
  return acc;
742
840
  };
743
- const createObjectSchemaFromShape = (shape, { unknownKeys, catchAll }, state) => {
841
+ const createObjectSchemaFromShape = (shape, { unknownKeys, catchAll }, state, omitType) => {
744
842
  const properties = mapProperties(shape, state);
745
- const required = mapRequired(shape, state);
843
+ const required = mapRequired(properties, shape, state);
746
844
  const additionalProperties = !isZodType(catchAll, "ZodNever") ? createSchemaObject(catchAll, state, ["additional properties"]) : void 0;
747
845
  return {
748
846
  type: "schema",
749
847
  schema: {
750
- type: "object",
848
+ ...!omitType && { type: "object" },
751
849
  ...properties && { properties: properties.properties },
752
850
  ...(required == null ? void 0 : required.required.length) && { required: required.required },
753
851
  ...unknownKeys === "strict" && { additionalProperties: false },
@@ -762,17 +860,38 @@ const createObjectSchemaFromShape = (shape, { unknownKeys, catchAll }, state) =>
762
860
  ])
763
861
  };
764
862
  };
765
- const mapRequired = (shape, state) => {
766
- const { required, effects: allEffects } = Object.entries(shape).reduce(
767
- (acc, [key, zodSchema]) => {
768
- state.path.push(`property: ${key}`);
769
- const { optional, effects } = isOptionalSchema(zodSchema, state);
770
- state.path.pop();
771
- if (!optional) {
863
+ const mapRequired = (properties, shape, state) => {
864
+ if (!properties) {
865
+ return void 0;
866
+ }
867
+ const { required, effects } = Object.entries(properties.schemas).reduce(
868
+ (acc, [key, schemaOrRef]) => {
869
+ const zodSchema = shape[key];
870
+ if (!zodSchema) {
871
+ throw new Error("Property somehow doesn't exist in shape");
872
+ }
873
+ const result = zodSchema.safeParse(void 0);
874
+ if (!result.success) {
772
875
  acc.required.push(key);
876
+ return acc;
773
877
  }
774
- if (effects) {
775
- acc.effects.push(effects);
878
+ if (result.data !== void 0) {
879
+ const baseEffect = {
880
+ zodType: zodSchema,
881
+ path: [...state.path, `property: ${key}`]
882
+ };
883
+ const effect = schemaOrRef.type === "ref" ? {
884
+ ...baseEffect,
885
+ type: "component"
886
+ } : {
887
+ ...baseEffect,
888
+ type: "schema",
889
+ creationType: state.type
890
+ };
891
+ acc.effects.push(effect);
892
+ if (state.type === "output") {
893
+ acc.required.push(key);
894
+ }
776
895
  }
777
896
  return acc;
778
897
  },
@@ -781,7 +900,7 @@ const mapRequired = (shape, state) => {
781
900
  effects: []
782
901
  }
783
902
  );
784
- return { required, effects: flattenEffects(allEffects) };
903
+ return { required, effects };
785
904
  };
786
905
  const mapProperties = (shape, state) => {
787
906
  const shapeEntries = Object.entries(shape);
@@ -790,28 +909,28 @@ const mapProperties = (shape, state) => {
790
909
  }
791
910
  return shapeEntries.reduce(
792
911
  (acc, [key, zodSchema]) => {
793
- if (isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined")) {
912
+ if (isOptionalObjectKey(zodSchema)) {
794
913
  return acc;
795
914
  }
796
- const property = createSchemaObject(zodSchema, state, [
797
- `property: ${key}`
798
- ]);
799
- acc.properties[key] = property.schema;
800
- acc.effects.push(property.effects);
915
+ const schema = createSchemaObject(zodSchema, state, [`property: ${key}`]);
916
+ acc.schemas[key] = schema;
917
+ acc.properties[key] = schema.schema;
918
+ acc.effects.push(schema.effects);
801
919
  return acc;
802
920
  },
803
921
  {
922
+ schemas: {},
804
923
  properties: {},
805
924
  effects: []
806
925
  }
807
926
  );
808
927
  };
809
928
  const createPipelineSchema = (zodPipeline, state) => {
810
- var _a, _b, _c;
811
- 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") {
812
931
  return createSchemaObject(zodPipeline._def.in, state, ["pipeline input"]);
813
932
  }
814
- 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") {
815
934
  return createSchemaObject(zodPipeline._def.out, state, ["pipeline output"]);
816
935
  }
817
936
  if (state.type === "input") {
@@ -1140,11 +1259,14 @@ const mapPrefixItems = (items, state) => {
1140
1259
  return void 0;
1141
1260
  };
1142
1261
  const createUnionSchema = (zodUnion, state) => {
1143
- var _a, _b;
1144
- const schemas = zodUnion.options.map(
1145
- (option, index) => createSchemaObject(option, state, [`union option ${index}`])
1146
- );
1147
- if (((_a = zodUnion._def.openapi) == null ? void 0 : _a.unionOneOf) ?? ((_b = state.documentOptions) == null ? void 0 : _b.unionOneOf)) {
1262
+ var _a, _b, _c;
1263
+ const schemas = zodUnion.options.reduce((acc, option, index) => {
1264
+ if (!isOptionalObjectKey(option)) {
1265
+ acc.push(createSchemaObject(option, state, [`union option ${index}`]));
1266
+ }
1267
+ return acc;
1268
+ }, []);
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)) {
1148
1270
  return {
1149
1271
  type: "schema",
1150
1272
  schema: {
@@ -1165,9 +1287,9 @@ const createUnknownSchema = (_zodUnknown) => ({
1165
1287
  type: "schema",
1166
1288
  schema: {}
1167
1289
  });
1168
- const createSchemaSwitch = (zodSchema, state) => {
1169
- var _a;
1170
- 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) {
1171
1293
  return createManualTypeSchema(zodSchema, state);
1172
1294
  }
1173
1295
  if (isZodType(zodSchema, "ZodString")) {
@@ -1192,7 +1314,7 @@ const createSchemaSwitch = (zodSchema, state) => {
1192
1314
  return createArraySchema(zodSchema, state);
1193
1315
  }
1194
1316
  if (isZodType(zodSchema, "ZodObject")) {
1195
- return createObjectSchema(zodSchema, state);
1317
+ return createObjectSchema(zodSchema, previous, state);
1196
1318
  }
1197
1319
  if (isZodType(zodSchema, "ZodUnion")) {
1198
1320
  return createUnionSchema(zodSchema, state);
@@ -1213,7 +1335,7 @@ const createSchemaSwitch = (zodSchema, state) => {
1213
1335
  return createReadonlySchema(zodSchema, state);
1214
1336
  }
1215
1337
  if (isZodType(zodSchema, "ZodDefault")) {
1216
- return createDefaultSchema(zodSchema, state);
1338
+ return createDefaultSchema(zodSchema, state, previous);
1217
1339
  }
1218
1340
  if (isZodType(zodSchema, "ZodRecord")) {
1219
1341
  return createRecordSchema(zodSchema, state);
@@ -1243,7 +1365,7 @@ const createSchemaSwitch = (zodSchema, state) => {
1243
1365
  return createIntersectionSchema(zodSchema, state);
1244
1366
  }
1245
1367
  if (isZodType(zodSchema, "ZodCatch")) {
1246
- return createCatchSchema(zodSchema, state);
1368
+ return createCatchSchema(zodSchema, state, previous);
1247
1369
  }
1248
1370
  if (isZodType(zodSchema, "ZodUnknown") || isZodType(zodSchema, "ZodAny")) {
1249
1371
  return createUnknownSchema();
@@ -1259,8 +1381,12 @@ const createSchemaSwitch = (zodSchema, state) => {
1259
1381
  }
1260
1382
  return createManualTypeSchema(zodSchema, state);
1261
1383
  };
1262
- const isDescriptionEqual = (schema, zodSchema) => schema.type === "ref" && zodSchema.description === schema.zodType.description;
1263
- const createNewSchema = (zodSchema, state) => {
1384
+ const createNewSchema = ({
1385
+ zodSchema,
1386
+ previous,
1387
+ state
1388
+ }) => {
1389
+ var _a;
1264
1390
  if (state.visited.has(zodSchema)) {
1265
1391
  throw new Error(
1266
1392
  `The schema at ${state.path.join(
@@ -1277,25 +1403,35 @@ const createNewSchema = (zodSchema, state) => {
1277
1403
  refType,
1278
1404
  unionOneOf,
1279
1405
  ...additionalMetadata
1280
- } = zodSchema._def.openapi ?? {};
1281
- const schema = createSchemaSwitch(zodSchema, state);
1282
- const description = zodSchema.description && !isDescriptionEqual(schema, zodSchema) ? zodSchema.description : void 0;
1283
- const schemaWithMetadata = enhanceWithMetadata(schema, {
1284
- ...description && { description },
1285
- ...additionalMetadata
1286
- });
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
+ );
1287
1414
  state.visited.delete(zodSchema);
1288
1415
  return schemaWithMetadata;
1289
1416
  };
1290
- const createNewRef = (ref, zodSchema, state) => {
1417
+ const createNewRef = ({
1418
+ previous,
1419
+ ref,
1420
+ zodSchema,
1421
+ state
1422
+ }) => {
1291
1423
  var _a;
1292
1424
  state.components.schemas.set(zodSchema, {
1293
1425
  type: "in-progress",
1294
1426
  ref
1295
1427
  });
1296
- const newSchema = createNewSchema(zodSchema, {
1297
- ...state,
1298
- visited: /* @__PURE__ */ new Set()
1428
+ const newSchema = createNewSchema({
1429
+ zodSchema,
1430
+ previous,
1431
+ state: {
1432
+ ...state,
1433
+ visited: /* @__PURE__ */ new Set()
1434
+ }
1299
1435
  });
1300
1436
  state.components.schemas.set(zodSchema, {
1301
1437
  type: "complete",
@@ -1311,6 +1447,7 @@ const createNewRef = (ref, zodSchema, state) => {
1311
1447
  (_a = state.documentOptions) == null ? void 0 : _a.componentRefPath
1312
1448
  )
1313
1449
  },
1450
+ schemaObject: newSchema.schema,
1314
1451
  effects: newSchema.effects ? [
1315
1452
  {
1316
1453
  type: "component",
@@ -1332,6 +1469,7 @@ const createExistingRef = (zodSchema, component, state) => {
1332
1469
  (_a = state.documentOptions) == null ? void 0 : _a.componentRefPath
1333
1470
  )
1334
1471
  },
1472
+ schemaObject: component.schemaObject,
1335
1473
  effects: component.effects ? [
1336
1474
  {
1337
1475
  type: "component",
@@ -1351,6 +1489,7 @@ const createExistingRef = (zodSchema, component, state) => {
1351
1489
  (_b = state.documentOptions) == null ? void 0 : _b.componentRefPath
1352
1490
  )
1353
1491
  },
1492
+ schemaObject: void 0,
1354
1493
  effects: [
1355
1494
  {
1356
1495
  type: "component",
@@ -1363,22 +1502,30 @@ const createExistingRef = (zodSchema, component, state) => {
1363
1502
  }
1364
1503
  return;
1365
1504
  };
1366
- const createSchemaOrRef = (zodSchema, state) => {
1367
- var _a;
1505
+ const createSchemaOrRef = (zodSchema, state, onlyRef) => {
1506
+ var _a, _b, _c, _d;
1368
1507
  const component = state.components.schemas.get(zodSchema);
1369
1508
  const existingRef = createExistingRef(zodSchema, component, state);
1370
1509
  if (existingRef) {
1371
1510
  return existingRef;
1372
1511
  }
1373
- 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);
1374
1515
  if (ref) {
1375
- 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;
1376
1520
  }
1377
- return createNewSchema(zodSchema, state);
1521
+ return createNewSchema({ zodSchema, previous: previous ?? current, state });
1378
1522
  };
1379
1523
  const createSchemaObject = (zodSchema, state, subpath) => {
1380
1524
  state.path.push(...subpath);
1381
1525
  const schema = createSchemaOrRef(zodSchema, state);
1526
+ if (!schema) {
1527
+ throw new Error("Schema does not exist");
1528
+ }
1382
1529
  state.path.pop();
1383
1530
  return schema;
1384
1531
  };
@@ -1441,8 +1588,8 @@ const createContent = (contentObject, components, type, subpath, documentOptions
1441
1588
  );
1442
1589
  const createComponentParamRef = (ref) => `#/components/parameters/${ref}`;
1443
1590
  const createBaseParameter = (schema, components, subpath, documentOptions) => {
1444
- var _a, _b, _c;
1445
- 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) ?? {};
1446
1593
  const state = {
1447
1594
  components,
1448
1595
  type: "input",
@@ -1451,8 +1598,8 @@ const createBaseParameter = (schema, components, subpath, documentOptions) => {
1451
1598
  documentOptions
1452
1599
  };
1453
1600
  const schemaObject = createSchema(schema, state, [...subpath, "schema"]);
1454
- const required = !((_b = isOptionalSchema(schema, state)) == null ? void 0 : _b.optional);
1455
- const description = ((_c = schema._def.openapi) == null ? void 0 : _c.description) ?? schema._def.description;
1601
+ const required = !schema.isOptional();
1602
+ const description = ((_d = (_c = schema._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.description) ?? schema._def.description;
1456
1603
  return {
1457
1604
  ...description && { description },
1458
1605
  ...rest,
@@ -1463,8 +1610,8 @@ const createBaseParameter = (schema, components, subpath, documentOptions) => {
1463
1610
  const createParamOrRef = (zodSchema, components, subpath, type, name, documentOptions) => {
1464
1611
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1465
1612
  const component = components.parameters.get(zodSchema);
1466
- 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;
1467
- 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;
1468
1615
  if (!paramType) {
1469
1616
  throw new Error("Parameter type missing");
1470
1617
  }
@@ -1488,7 +1635,7 @@ const createParamOrRef = (zodSchema, components, subpath, type, name, documentOp
1488
1635
  if ("$ref" in baseParamOrRef) {
1489
1636
  throw new Error("Unexpected Error: received a reference object");
1490
1637
  }
1491
- 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);
1492
1639
  const paramObject = {
1493
1640
  in: paramType,
1494
1641
  name: paramName,
@@ -1635,7 +1782,7 @@ const createHeaderOrRef = (schema, components, documentOptions) => {
1635
1782
  if ("$ref" in baseHeader) {
1636
1783
  throw new Error("Unexpected Error: received a reference object");
1637
1784
  }
1638
- 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);
1639
1786
  if (ref) {
1640
1787
  components.headers.set(schema, {
1641
1788
  type: "complete",
@@ -1650,7 +1797,7 @@ const createHeaderOrRef = (schema, components, documentOptions) => {
1650
1797
  };
1651
1798
  const createBaseHeader = (schema, components, documentOptions) => {
1652
1799
  var _a, _b;
1653
- const { ref, ...rest } = ((_a = schema._def.openapi) == null ? void 0 : _a.header) ?? {};
1800
+ const { ref, ...rest } = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.header) ?? {};
1654
1801
  const state = {
1655
1802
  components,
1656
1803
  type: "output",
@@ -1659,7 +1806,8 @@ const createBaseHeader = (schema, components, documentOptions) => {
1659
1806
  documentOptions
1660
1807
  };
1661
1808
  const schemaObject = createSchema(schema, state, ["header"]);
1662
- const required = !((_b = isOptionalSchema(schema, state)) == null ? void 0 : _b.optional);
1809
+ const optionalResult = schema.safeParse(void 0);
1810
+ const required = !optionalResult.success || optionalResult !== void 0;
1663
1811
  return {
1664
1812
  ...rest,
1665
1813
  ...schema && { schema: schemaObject },
@@ -1909,14 +2057,14 @@ const getSchemas = (schemas, components) => {
1909
2057
  return;
1910
2058
  }
1911
2059
  Object.entries(schemas).forEach(([key, schema]) => {
1912
- var _a;
2060
+ var _a, _b;
1913
2061
  if (isAnyZodType(schema)) {
1914
2062
  if (components.schemas.has(schema)) {
1915
2063
  throw new Error(
1916
2064
  `Schema ${JSON.stringify(schema._def)} is already registered`
1917
2065
  );
1918
2066
  }
1919
- 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;
1920
2068
  components.schemas.set(schema, {
1921
2069
  type: "manual",
1922
2070
  ref
@@ -1929,16 +2077,16 @@ const getParameters = (parameters, components) => {
1929
2077
  return;
1930
2078
  }
1931
2079
  Object.entries(parameters).forEach(([key, schema]) => {
1932
- var _a, _b, _c, _d, _e, _f;
2080
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1933
2081
  if (isAnyZodType(schema)) {
1934
2082
  if (components.parameters.has(schema)) {
1935
2083
  throw new Error(
1936
2084
  `Parameter ${JSON.stringify(schema._def)} is already registered`
1937
2085
  );
1938
2086
  }
1939
- const ref = ((_b = (_a = schema._def.openapi) == null ? void 0 : _a.param) == null ? void 0 : _b.ref) ?? key;
1940
- const name = (_d = (_c = schema._def.openapi) == null ? void 0 : _c.param) == null ? void 0 : _d.name;
1941
- 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;
1942
2090
  if (!name || !location) {
1943
2091
  throw new Error("`name` or `in` missing in .openapi()");
1944
2092
  }
@@ -1956,14 +2104,14 @@ const getHeaders = (responseHeaders, components) => {
1956
2104
  return;
1957
2105
  }
1958
2106
  Object.entries(responseHeaders).forEach(([key, schema]) => {
1959
- var _a, _b;
2107
+ var _a, _b, _c;
1960
2108
  if (isAnyZodType(schema)) {
1961
2109
  if (components.parameters.has(schema)) {
1962
2110
  throw new Error(
1963
2111
  `Header ${JSON.stringify(schema._def)} is already registered`
1964
2112
  );
1965
2113
  }
1966
- 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;
1967
2115
  components.headers.set(schema, {
1968
2116
  type: "manual",
1969
2117
  ref
@@ -2068,11 +2216,11 @@ const createComponents = (componentsObject, components, documentOptions) => {
2068
2216
  };
2069
2217
  const createSchemaComponents = (componentsObject, components, documentOptions) => {
2070
2218
  Array.from(components.schemas).forEach(([schema, { type }], index) => {
2071
- var _a;
2219
+ var _a, _b;
2072
2220
  if (type === "manual") {
2073
2221
  const state = {
2074
2222
  components,
2075
- 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",
2076
2224
  path: [],
2077
2225
  visited: /* @__PURE__ */ new Set(),
2078
2226
  documentOptions