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