zod-openapi 4.2.4 → 5.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.
@@ -1,2462 +1,1078 @@
1
1
  "use strict";
2
- const extendZodSymbols = require("./extendZodSymbols.chunk.cjs");
3
- const isZodType = (zodType, typeName) => {
4
- var _a;
5
- return ((_a = zodType == null ? void 0 : zodType._def) == null ? void 0 : _a.typeName) === typeName;
6
- };
7
- const isAnyZodType = (zodType) => {
8
- var _a;
9
- return Boolean(
10
- (_a = zodType == null ? void 0 : zodType._def) == null ? void 0 : _a.typeName
11
- );
12
- };
13
- const openApiVersions = [
14
- "3.0.0",
15
- "3.0.1",
16
- "3.0.2",
17
- "3.0.3",
18
- "3.1.0"
19
- ];
20
- const satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
21
- const createDescriptionMetadata = (schema, description, state) => {
22
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
23
- return {
24
- type: "ref",
25
- schema: {
26
- $ref: schema.schema.$ref,
27
- description
28
- },
29
- zodType: schema.zodType,
30
- effects: schema.effects,
31
- schemaObject: schema.schemaObject
32
- };
33
- }
34
- return {
35
- type: "schema",
36
- schema: {
37
- description,
38
- allOf: [schema.schema]
39
- },
40
- effects: schema.effects
41
- };
42
- };
43
- const isValueEqual = (value, previous) => {
44
- if (typeof value !== typeof previous) {
45
- return false;
46
- }
47
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
48
- return value === previous;
49
- }
50
- if (Array.isArray(value) && Array.isArray(previous)) {
51
- const sorted = [...value].sort();
52
- const previousSorted = [...previous].sort();
53
- return sorted.every((v, i) => isValueEqual(v, previousSorted[i]));
54
- }
55
- if (value === null || previous === null) {
56
- return value === previous;
57
- }
58
- if (typeof value === "object" && typeof previous === "object") {
59
- const keys = Object.keys(value);
60
- return keys.every(
61
- (key) => isValueEqual(
62
- value[key],
63
- previous[key]
64
- )
65
- );
66
- }
67
- return value === previous;
68
- };
69
- const enhanceWithMetadata = (schema, metadata, state, previous) => {
70
- const values = Object.entries(metadata).reduce(
71
- (acc, [key, value]) => {
72
- if (value === void 0) {
73
- return acc;
2
+ const core = require("zod/v4/core");
3
+ const v4 = require("zod/v4");
4
+ const isAnyZodType = (schema) => typeof schema === "object" && schema !== null && "_zod" in schema;
5
+ const unwrapZodObject = (zodType, io, path) => {
6
+ const def = zodType._zod.def;
7
+ switch (def.type) {
8
+ case "object": {
9
+ return zodType;
10
+ }
11
+ case "lazy": {
12
+ return unwrapZodObject(def.getter(), io, path);
13
+ }
14
+ case "pipe": {
15
+ if (io === "input") {
16
+ return unwrapZodObject(def.in, io, path);
74
17
  }
75
- acc[key] = value;
76
- return acc;
77
- },
78
- {}
79
- );
80
- const length = Object.values(values).length;
81
- if (schema.type === "ref") {
82
- if (length === 0) {
83
- return schema;
84
- }
85
- if (length === 1 && metadata.description) {
86
- return createDescriptionMetadata(schema, metadata.description, state);
87
- }
88
- return {
89
- type: "schema",
90
- schema: {
91
- allOf: [schema.schema],
92
- ...metadata
93
- },
94
- effects: schema.effects
95
- };
96
- }
97
- if (previous && schema.schema.type !== "object") {
98
- const diff = Object.entries({ ...schema.schema, ...values }).reduce(
99
- (acc, [key, value]) => {
100
- if (previous.schemaObject && isValueEqual(
101
- previous.schemaObject[key],
102
- value
103
- )) {
104
- return acc;
105
- }
106
- acc[key] = value;
107
- return acc;
108
- },
109
- {}
110
- );
111
- const diffLength = Object.values(diff).length;
112
- if (diffLength === 0) {
113
- return {
114
- type: "ref",
115
- schema: {
116
- $ref: previous.schema.$ref
117
- },
118
- effects: schema.effects,
119
- schemaObject: previous.schemaObject,
120
- zodType: previous.zodType
121
- };
122
- }
123
- if (diffLength === 1 && typeof diff.description === "string") {
124
- return createDescriptionMetadata(previous, diff.description, state);
125
- }
126
- return {
127
- type: "schema",
128
- schema: { allOf: [previous.schema], ...diff },
129
- effects: schema.effects
130
- };
131
- }
132
- return {
133
- type: "schema",
134
- schema: {
135
- ...schema.schema,
136
- ...metadata
137
- },
138
- effects: schema.effects
139
- };
140
- };
141
- const createArraySchema = (zodArray, state) => {
142
- var _a, _b, _c, _d;
143
- const zodType = zodArray._def.type;
144
- const minItems = ((_a = zodArray._def.exactLength) == null ? void 0 : _a.value) ?? ((_b = zodArray._def.minLength) == null ? void 0 : _b.value);
145
- const maxItems = ((_c = zodArray._def.exactLength) == null ? void 0 : _c.value) ?? ((_d = zodArray._def.maxLength) == null ? void 0 : _d.value);
146
- const items = createSchemaObject(zodType, state, ["array items"]);
147
- return {
148
- type: "schema",
149
- schema: {
150
- type: "array",
151
- items: items.schema,
152
- ...minItems !== void 0 && { minItems },
153
- ...maxItems !== void 0 && { maxItems }
154
- },
155
- effects: items.effects
156
- };
157
- };
158
- const createBigIntSchema = (_zodBigInt) => ({
159
- type: "schema",
160
- schema: {
161
- type: "integer",
162
- format: "int64"
163
- }
164
- });
165
- const createBooleanSchema = (_zodBoolean) => ({
166
- type: "schema",
167
- schema: {
168
- type: "boolean"
169
- }
170
- });
171
- const createBrandedSchema = (zodBranded, state) => createSchemaObject(zodBranded._def.type, state, ["brand"]);
172
- const createCatchSchema = (zodCatch, state, previous) => {
173
- const schemaObject = createSchemaObject(zodCatch._def.innerType, state, [
174
- "default"
175
- ]);
176
- const catchResult = zodCatch.safeParse(void 0);
177
- const maybeDefaultValue = catchResult.success ? {
178
- default: catchResult.data
179
- } : {};
180
- return enhanceWithMetadata(schemaObject, maybeDefaultValue, state, previous);
181
- };
182
- const createDateSchema = (_zodDate, state) => {
183
- var _a;
184
- return {
185
- type: "schema",
186
- schema: ((_a = state.documentOptions) == null ? void 0 : _a.defaultDateSchema) ?? {
187
- type: "string"
188
- }
189
- };
190
- };
191
- const createDefaultSchema = (zodDefault, state, previous) => {
192
- const schemaObject = createSchemaObject(zodDefault._def.innerType, state, [
193
- "default"
194
- ]);
195
- return enhanceWithMetadata(
196
- schemaObject,
197
- {
198
- default: zodDefault._def.defaultValue()
199
- },
200
- state,
201
- previous
202
- );
203
- };
204
- const createNativeEnumSchema = (zodEnum, state) => {
205
- const enumValues = getValidEnumValues(zodEnum._def.values);
206
- const { numbers, strings } = sortStringsAndNumbers(enumValues);
207
- if (strings.length && numbers.length) {
208
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
209
- return {
210
- type: "schema",
211
- schema: {
212
- type: ["string", "number"],
213
- enum: [...strings, ...numbers]
214
- }
215
- };
18
+ return unwrapZodObject(def.out, io, path);
216
19
  }
217
- return {
218
- type: "schema",
219
- schema: {
220
- oneOf: [
221
- { type: "string", enum: strings },
222
- { type: "number", enum: numbers }
223
- ]
224
- }
225
- };
226
20
  }
227
- if (strings.length) {
228
- return {
229
- type: "schema",
230
- schema: {
231
- type: "string",
232
- enum: strings
233
- }
234
- };
235
- }
236
- return {
237
- type: "schema",
238
- schema: {
239
- type: "number",
240
- enum: numbers
241
- }
242
- };
243
- };
244
- const getValidEnumValues = (enumValues) => {
245
- const keys = Object.keys(enumValues).filter(
246
- (key) => typeof enumValues[enumValues[key]] !== "number"
21
+ throw new Error(
22
+ `Failed to unwrap ZodObject from type: ${zodType._zod.def.type} at ${path.join(" > ")}`
247
23
  );
248
- return keys.map((key) => enumValues[key]);
249
24
  };
250
- const sortStringsAndNumbers = (values) => ({
251
- strings: values.filter((value) => typeof value === "string"),
252
- numbers: values.filter((value) => typeof value === "number")
253
- });
254
- const createTransformSchema = (zodTransform, state) => {
255
- var _a, _b, _c, _d, _e, _f;
256
- if (((_b = (_a = zodTransform._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.effectType) === "output") {
257
- return {
258
- type: "schema",
259
- schema: createManualOutputTransformSchema(zodTransform, state)
260
- };
261
- }
262
- 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") {
263
- return createSchemaObject(zodTransform._def.schema, state, [
264
- "transform input"
265
- ]);
266
- }
267
- if (state.type === "output") {
268
- return {
269
- type: "schema",
270
- schema: createManualOutputTransformSchema(zodTransform, state)
271
- };
25
+ const isRequired = (zodType, io) => {
26
+ if (io === "input") {
27
+ return zodType._zod.optin === void 0;
272
28
  }
273
- const schema = createSchemaObject(zodTransform._def.schema, state, [
274
- "transform input"
275
- ]);
276
- return {
277
- ...schema,
278
- effects: flattenEffects([
279
- [
280
- {
281
- type: "schema",
282
- creationType: "input",
283
- zodType: zodTransform,
284
- path: [...state.path]
285
- }
286
- ],
287
- schema.effects
288
- ])
289
- };
29
+ return zodType._zod.optout === void 0;
290
30
  };
291
- const createManualOutputTransformSchema = (zodTransform, state) => {
292
- var _a, _b, _c;
293
- if (!((_b = (_a = zodTransform._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type)) {
294
- const zodType = zodTransform.constructor.name;
295
- const schemaName = `${zodType} - ${zodTransform._def.effect.type}`;
31
+ const createParameter = (parameter, location, ctx, path) => {
32
+ var _a, _b;
33
+ const seenParameter = ctx.registry.parameters.seen.get(parameter);
34
+ if (seenParameter) {
35
+ return seenParameter;
36
+ }
37
+ const meta = v4.globalRegistry.get(parameter);
38
+ const name = (location == null ? void 0 : location.name) ?? ((_a = meta == null ? void 0 : meta.param) == null ? void 0 : _a.name);
39
+ const inLocation = (location == null ? void 0 : location.in) ?? ((_b = meta == null ? void 0 : meta.param) == null ? void 0 : _b.in);
40
+ if (!name || !inLocation) {
296
41
  throw new Error(
297
- `Failed to determine a type for ${schemaName} at ${state.path.join(
298
- " > "
299
- )}. Please change the 'effectType' to 'same' or 'input', wrap it in a ZodPipeline or assign it a manual 'type'.`
42
+ `Parameter at ${path.join(" > ")} is missing \`.meta({ param: { name, in } })\` information`
300
43
  );
301
44
  }
302
- return {
303
- type: (_c = zodTransform._def.zodOpenApi) == null ? void 0 : _c.openapi.type
304
- };
305
- };
306
- const getZodTypeName = (zodType) => {
307
- if (isZodType(zodType, "ZodEffects")) {
308
- return `${zodType._def.typeName} - ${zodType._def.effect.type}`;
309
- }
310
- return zodType._def.typeName;
311
- };
312
- const throwTransformError = (effect) => {
313
- const typeName = getZodTypeName(effect.zodType);
314
- const input = effect.creationType;
315
- const opposite = input === "input" ? "output" : "input";
316
- throw new Error(
317
- `The ${typeName} at ${effect.path.join(
318
- " > "
319
- )} is used within a registered compoment schema${effect.component ? ` (${effect.component.ref})` : ""} and contains an ${input} transformation${effect.component ? ` (${getZodTypeName(
320
- effect.component.zodType
321
- )}) defined at ${effect.component.path.join(" > ")}` : ""} which is also used in an ${opposite} schema.
322
-
323
- This may cause the schema to render incorrectly and is most likely a mistake. You can resolve this by:
324
-
325
- 1. Setting an \`effectType\` on one of the transformations to \`same\` (Not applicable for ZodDefault), \`input\` or \`output\` eg. \`.openapi({type: 'same'})\`
326
- 2. Wrapping the transformation in a ZodPipeline
327
- 3. Assigning a manual type to the transformation eg. \`.openapi({type: 'string'})\`
328
- 4. Removing the transformation
329
- 5. Deregister the component containing the transformation`
45
+ const computedPath = [...path, inLocation, name].join(" > ");
46
+ const schemaObject = ctx.registry.schemas.setSchema(
47
+ computedPath,
48
+ parameter,
49
+ ctx.io
330
50
  );
331
- };
332
- const resolveSingleEffect = (effect, state) => {
333
- if (effect.type === "schema") {
334
- return {
335
- creationType: effect.creationType,
336
- path: effect.path,
337
- zodType: effect.zodType
338
- };
339
- }
340
- if (effect.type === "component") {
341
- if (state.visited.has(effect.zodType)) {
342
- return;
343
- }
344
- const component = state.components.schemas.get(effect.zodType);
345
- if ((component == null ? void 0 : component.type) !== "complete") {
346
- throw new Error("Something went wrong, component schema is not complete");
347
- }
348
- if (component.resolvedEffect) {
349
- return {
350
- creationType: component.resolvedEffect.creationType,
351
- path: effect.path,
352
- zodType: effect.zodType,
353
- component: {
354
- ref: component.ref,
355
- zodType: component.resolvedEffect.zodType,
356
- path: component.resolvedEffect.path
357
- }
358
- };
359
- }
360
- if (!component.effects) {
361
- return void 0;
362
- }
363
- state.visited.add(effect.zodType);
364
- const resolved = resolveEffect(component.effects, state);
365
- state.visited.delete(effect.zodType);
366
- if (!resolved) {
367
- return void 0;
368
- }
369
- component.resolvedEffect = resolved;
370
- return resolved;
371
- }
372
- return void 0;
373
- };
374
- const resolveEffect = (effects, state) => {
375
- const { input, output } = effects.reduce(
376
- (acc, effect) => {
377
- const resolvedSchemaEffect = resolveSingleEffect(effect, state);
378
- if ((resolvedSchemaEffect == null ? void 0 : resolvedSchemaEffect.creationType) === "input") {
379
- acc.input.push(resolvedSchemaEffect);
380
- }
381
- if ((resolvedSchemaEffect == null ? void 0 : resolvedSchemaEffect.creationType) === "output") {
382
- acc.output.push(resolvedSchemaEffect);
383
- }
384
- if (resolvedSchemaEffect && acc.input.length > 1 && acc.output.length > 1) {
385
- throwTransformError(resolvedSchemaEffect);
386
- }
387
- return acc;
388
- },
389
- { input: [], output: [] }
390
- );
391
- if (input.length > 0) {
392
- return input[0];
393
- }
394
- if (output.length > 0) {
395
- return output[0];
396
- }
397
- return void 0;
398
- };
399
- const verifyEffects = (effects, state) => {
400
- const resolved = resolveEffect(effects, state);
401
- if ((resolved == null ? void 0 : resolved.creationType) && resolved.creationType !== state.type) {
402
- throwTransformError(resolved);
403
- }
404
- };
405
- const flattenEffects = (effects) => {
406
- const allEffects = effects.reduce((acc, effect) => {
407
- if (effect) {
408
- return acc.concat(effect);
409
- }
410
- return acc;
411
- }, []);
412
- return allEffects.length ? allEffects : void 0;
413
- };
414
- const createDiscriminatedUnionSchema = (zodDiscriminatedUnion, state) => {
415
- const options = zodDiscriminatedUnion.options;
416
- const schemas = options.map(
417
- (option, index) => createSchemaObject(option, state, [`discriminated union option ${index}`])
418
- );
419
- const schemaObjects = schemas.map((schema) => schema.schema);
420
- const discriminator = mapDiscriminator(
421
- schemaObjects,
422
- options,
423
- zodDiscriminatedUnion.discriminator,
424
- state
425
- );
426
- return {
427
- type: "schema",
428
- schema: {
429
- oneOf: schemaObjects,
430
- ...discriminator && { discriminator }
431
- },
432
- effects: flattenEffects(schemas.map((schema) => schema.effects))
51
+ const { id, ...rest } = (meta == null ? void 0 : meta.param) ?? {};
52
+ const parameterObject = {
53
+ ...rest,
54
+ name,
55
+ in: inLocation,
56
+ schema: schemaObject
433
57
  };
434
- };
435
- const unwrapLiterals = (zodType, state) => {
436
- if (isZodType(zodType, "ZodLiteral")) {
437
- if (typeof zodType._def.value !== "string") {
438
- return void 0;
439
- }
440
- return [zodType._def.value];
441
- }
442
- if (isZodType(zodType, "ZodNativeEnum")) {
443
- const schema = createNativeEnumSchema(zodType, state);
444
- if (schema.type === "schema" && schema.schema.type === "string") {
445
- return schema.schema.enum;
446
- }
447
- }
448
- if (isZodType(zodType, "ZodEnum")) {
449
- return zodType._def.values;
58
+ if (isRequired(parameter, ctx.io)) {
59
+ parameterObject.required = true;
450
60
  }
451
- if (isZodType(zodType, "ZodBranded")) {
452
- return unwrapLiterals(zodType._def.type, state);
61
+ if (!parameterObject.description && (meta == null ? void 0 : meta.description)) {
62
+ parameterObject.description = meta.description;
453
63
  }
454
- if (isZodType(zodType, "ZodReadonly")) {
455
- return unwrapLiterals(zodType._def.innerType, state);
456
- }
457
- if (isZodType(zodType, "ZodCatch")) {
458
- return unwrapLiterals(zodType._def.innerType, state);
64
+ if (id) {
65
+ const ref = {
66
+ $ref: `#/components/parameters/${id}`
67
+ };
68
+ ctx.registry.parameters.seen.set(parameter, ref);
69
+ ctx.registry.parameters.ids.set(id, parameterObject);
70
+ return ref;
459
71
  }
460
- return void 0;
72
+ ctx.registry.parameters.seen.set(parameter, parameterObject);
73
+ return parameterObject;
461
74
  };
462
- const mapDiscriminator = (schemas, zodObjects, discriminator, state) => {
463
- var _a;
464
- if (typeof discriminator !== "string") {
75
+ const createManualParameters = (parameters, ctx, path) => {
76
+ if (!parameters) {
465
77
  return void 0;
466
78
  }
467
- const mapping = {};
468
- for (const [index, zodObject] of zodObjects.entries()) {
469
- const schema = schemas[index];
470
- const componentSchemaRef = "$ref" in schema ? schema == null ? void 0 : schema.$ref : void 0;
471
- if (!componentSchemaRef) {
472
- if ((_a = state.documentOptions) == null ? void 0 : _a.enforceDiscriminatedUnionComponents) {
473
- throw new Error(
474
- `Discriminated Union member ${index} at ${state.path.join(" > ")} is not registered as a component`
475
- );
79
+ const parameterObjects = [];
80
+ for (const parameter of parameters) {
81
+ if (isAnyZodType(parameter)) {
82
+ const seenParameter = ctx.registry.parameters.seen.get(parameter);
83
+ if (seenParameter) {
84
+ parameterObjects.push(seenParameter);
85
+ continue;
476
86
  }
477
- return void 0;
478
- }
479
- const value = zodObject.shape[discriminator];
480
- const literals = unwrapLiterals(value, state);
481
- if (!literals) {
482
- return void 0;
483
- }
484
- for (const enumValue of literals) {
485
- mapping[enumValue] = componentSchemaRef;
87
+ const paramObject = createParameter(parameter, void 0, ctx, [
88
+ ...path,
89
+ "parameters"
90
+ ]);
91
+ parameterObjects.push(paramObject);
92
+ continue;
486
93
  }
94
+ parameterObjects.push(parameter);
487
95
  }
488
- return {
489
- propertyName: discriminator,
490
- mapping
491
- };
492
- };
493
- const createEnumSchema = (zodEnum) => ({
494
- type: "schema",
495
- schema: {
496
- type: "string",
497
- enum: zodEnum._def.values
498
- }
499
- });
500
- const createIntersectionSchema = (zodIntersection, state) => {
501
- const schemas = flattenIntersection(zodIntersection);
502
- const allOfs = schemas.map(
503
- (schema, index) => createSchemaObject(schema, state, [`intersection ${index}`])
504
- );
505
- return {
506
- type: "schema",
507
- schema: {
508
- allOf: allOfs.map((schema) => schema.schema)
509
- },
510
- effects: flattenEffects(allOfs.map((schema) => schema.effects))
511
- };
512
- };
513
- const flattenIntersection = (zodType) => {
514
- if (!isZodType(zodType, "ZodIntersection")) {
515
- return [zodType];
516
- }
517
- const leftSchemas = flattenIntersection(zodType._def.left);
518
- const rightSchemas = flattenIntersection(zodType._def.right);
519
- return [...leftSchemas, ...rightSchemas];
520
- };
521
- const createLazySchema = (zodLazy, state) => {
522
- const innerSchema = zodLazy._def.getter();
523
- return createSchemaObject(innerSchema, state, ["lazy schema"]);
524
- };
525
- const createNullSchema = () => ({
526
- type: "schema",
527
- schema: {
528
- type: "null"
529
- }
530
- });
531
- const createLiteralSchema = (zodLiteral, state) => {
532
- if (zodLiteral.value === null) {
533
- return createNullSchema();
534
- }
535
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
536
- return {
537
- type: "schema",
538
- schema: {
539
- type: typeof zodLiteral.value,
540
- const: zodLiteral.value
541
- }
542
- };
543
- }
544
- return {
545
- type: "schema",
546
- schema: {
547
- type: typeof zodLiteral.value,
548
- enum: [zodLiteral.value]
549
- }
550
- };
96
+ return parameterObjects;
551
97
  };
552
- const createManualTypeSchema = (zodSchema, state) => {
553
- var _a, _b, _c;
554
- if (!((_b = (_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type)) {
555
- const schemaName = zodSchema.constructor.name;
556
- throw new Error(
557
- `Unknown schema ${schemaName} at ${state.path.join(
558
- " > "
559
- )}. Please assign it a manual 'type'.`
560
- );
98
+ const createParameters = (requestParams, ctx, path) => {
99
+ if (!requestParams) {
100
+ return void 0;
561
101
  }
562
- return {
563
- type: "schema",
564
- schema: {
565
- type: (_c = zodSchema._def.zodOpenApi) == null ? void 0 : _c.openapi.type
566
- }
567
- };
568
- };
569
- const createNullableSchema = (zodNullable, state) => {
570
- const schemaObject = createSchemaObject(zodNullable.unwrap(), state, [
571
- "nullable"
572
- ]);
573
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
574
- if (schemaObject.type === "ref" || schemaObject.schema.allOf) {
575
- return {
576
- type: "schema",
577
- schema: {
578
- oneOf: mapNullOf([schemaObject.schema], state.components.openapi)
579
- },
580
- effects: schemaObject.effects
581
- };
582
- }
583
- if (schemaObject.schema.oneOf) {
584
- const { oneOf, ...schema3 } = schemaObject.schema;
585
- return {
586
- type: "schema",
587
- schema: {
588
- oneOf: mapNullOf(oneOf, state.components.openapi),
589
- ...schema3
590
- },
591
- effects: schemaObject.effects
592
- };
593
- }
594
- if (schemaObject.schema.anyOf) {
595
- const { anyOf, ...schema3 } = schemaObject.schema;
596
- return {
597
- type: "schema",
598
- schema: {
599
- anyOf: mapNullOf(anyOf, state.components.openapi),
600
- ...schema3
601
- },
602
- effects: schemaObject.effects
603
- };
102
+ const parameterObjects = [];
103
+ for (const [location, schema] of Object.entries(requestParams ?? {})) {
104
+ if (!schema) {
105
+ continue;
604
106
  }
605
- const { type: type2, const: schemaConst, ...schema2 } = schemaObject.schema;
606
- if (schemaConst) {
607
- return {
608
- type: "schema",
609
- schema: {
610
- type: mapNullType(type2),
611
- enum: [schemaConst, null],
612
- ...schema2
107
+ const zodObject = unwrapZodObject(schema, ctx.io, path);
108
+ for (const [name, zodSchema] of Object.entries(zodObject._zod.def.shape)) {
109
+ const seenParameter = ctx.registry.parameters.seen.get(zodSchema);
110
+ if (seenParameter) {
111
+ parameterObjects.push(seenParameter);
112
+ continue;
113
+ }
114
+ const paramObject = createParameter(
115
+ zodSchema,
116
+ {
117
+ in: location,
118
+ name
613
119
  },
614
- effects: schemaObject.effects
615
- };
120
+ ctx,
121
+ [...path, location, name]
122
+ );
123
+ parameterObjects.push(paramObject);
616
124
  }
617
- return {
618
- type: "schema",
619
- schema: {
620
- type: mapNullType(type2),
621
- ...schema2,
622
- // https://github.com/json-schema-org/json-schema-spec/issues/258
623
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
624
- ...schema2.enum && { enum: [...schema2.enum, null] }
625
- },
626
- effects: schemaObject.effects
627
- };
628
125
  }
629
- if (schemaObject.type === "ref") {
126
+ return parameterObjects;
127
+ };
128
+ const createMediaTypeObject = (mediaTypeObject, ctx, path) => {
129
+ const computedPath = path.join(" > ");
130
+ if (isAnyZodType(mediaTypeObject.schema)) {
131
+ const schemaObject = ctx.registry.schemas.setSchema(
132
+ computedPath,
133
+ mediaTypeObject.schema,
134
+ ctx.io
135
+ );
630
136
  return {
631
- type: "schema",
632
- schema: {
633
- allOf: [schemaObject.schema],
634
- nullable: true
635
- },
636
- effects: schemaObject.effects
137
+ ...mediaTypeObject,
138
+ schema: schemaObject
637
139
  };
638
140
  }
639
- const { type, ...schema } = schemaObject.schema;
640
- return {
641
- type: "schema",
642
- schema: {
643
- ...type && { type },
644
- nullable: true,
645
- ...schema,
646
- // https://github.com/OAI/OpenAPI-Specification/blob/main/proposals/2019-10-31-Clarify-Nullable.md#if-a-schema-specifies-nullable-true-and-enum-1-2-3-does-that-schema-allow-null-values-see-1900
647
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
648
- ...schema.enum && { enum: [...schema.enum, null] }
649
- },
650
- effects: schemaObject.effects
651
- };
652
- };
653
- const mapNullType = (type) => {
654
- if (!type) {
655
- return "null";
656
- }
657
- if (Array.isArray(type)) {
658
- return [...type, "null"];
659
- }
660
- return [type, "null"];
141
+ return mediaTypeObject;
661
142
  };
662
- const mapNullOf = (ofSchema, openapi) => {
663
- if (satisfiesVersion(openapi, "3.1.0")) {
664
- return [...ofSchema, { type: "null" }];
665
- }
666
- return [...ofSchema, { nullable: true }];
667
- };
668
- const createNumberSchema = (zodNumber, state) => {
669
- const zodNumberChecks = getZodNumberChecks(zodNumber);
670
- const minimum = mapMinimum(zodNumberChecks, state.components.openapi);
671
- const maximum = mapMaximum(zodNumberChecks, state.components.openapi);
672
- const multipleOf = mapMultipleOf(zodNumberChecks);
673
- return {
674
- type: "schema",
675
- schema: {
676
- type: mapNumberType(zodNumberChecks),
677
- ...multipleOf && multipleOf,
678
- ...minimum && minimum,
679
- // Union types are not easy to tame
680
- ...maximum && maximum
143
+ const createContent = (content, ctx, path) => {
144
+ const contentObject = {};
145
+ for (const [mediaType, mediaTypeObject] of Object.entries(content)) {
146
+ if (mediaTypeObject) {
147
+ contentObject[mediaType] = createMediaTypeObject(mediaTypeObject, ctx, [
148
+ ...path,
149
+ mediaType
150
+ ]);
681
151
  }
682
- };
683
- };
684
- const mapMultipleOf = (zodNumberCheck) => zodNumberCheck.multipleOf ? { multipleOf: zodNumberCheck.multipleOf.value } : void 0;
685
- const mapMaximum = (zodNumberCheck, openapi) => {
686
- if (!zodNumberCheck.max) {
687
- return void 0;
688
- }
689
- const maximum = zodNumberCheck.max.value;
690
- if (zodNumberCheck.max.inclusive) {
691
- return { ...maximum !== void 0 && { maximum } };
692
152
  }
693
- if (satisfiesVersion(openapi, "3.1.0")) {
694
- return { exclusiveMaximum: maximum };
695
- }
696
- return { maximum, exclusiveMaximum: true };
153
+ return contentObject;
697
154
  };
698
- const mapMinimum = (zodNumberCheck, openapi) => {
699
- if (!zodNumberCheck.min) {
155
+ const createRequestBody = (requestBody, ctx, path) => {
156
+ if (!requestBody) {
700
157
  return void 0;
701
158
  }
702
- const minimum = zodNumberCheck.min.value;
703
- if (zodNumberCheck.min.inclusive) {
704
- return { ...minimum !== void 0 && { minimum } };
705
- }
706
- if (satisfiesVersion(openapi, "3.1.0")) {
707
- return { exclusiveMinimum: minimum };
159
+ const seenRequestBody = ctx.registry.requestBodies.seen.get(requestBody);
160
+ if (seenRequestBody) {
161
+ return seenRequestBody;
708
162
  }
709
- return { minimum, exclusiveMinimum: true };
710
- };
711
- const getZodNumberChecks = (zodNumber) => zodNumber._def.checks.reduce((acc, check) => {
712
- acc[check.kind] = check;
713
- return acc;
714
- }, {});
715
- const mapNumberType = (zodNumberChecks) => zodNumberChecks.int ? "integer" : "number";
716
- const createOptionalSchema = (zodOptional, state) => createSchemaObject(zodOptional.unwrap(), state, ["optional"]);
717
- const isOptionalObjectKey = (zodSchema) => isZodType(zodSchema, "ZodNever") || isZodType(zodSchema, "ZodUndefined") || isZodType(zodSchema, "ZodLiteral") && zodSchema._def.value === void 0;
718
- const createObjectSchema = (zodObject, previous, state) => {
719
- const extendedSchema = createExtendedSchema(
720
- zodObject,
721
- previous == null ? void 0 : previous.zodType,
722
- state
163
+ const { content, id, ...rest } = requestBody;
164
+ const requestBodyObject = {
165
+ ...rest,
166
+ content: createContent(content, ctx, [...path, "content"])
167
+ };
168
+ if (id) {
169
+ const ref = {
170
+ $ref: `#/components/requestBodies/${id}`
171
+ };
172
+ ctx.registry.requestBodies.ids.set(id, requestBodyObject);
173
+ ctx.registry.requestBodies.seen.set(requestBody, ref);
174
+ return ref;
175
+ }
176
+ ctx.registry.requestBodies.seen.set(requestBody, requestBodyObject);
177
+ return requestBodyObject;
178
+ };
179
+ const createHeader = (header, ctx, path) => {
180
+ const seenHeader = ctx.registry.headers.seen.get(header);
181
+ if (seenHeader) {
182
+ return seenHeader;
183
+ }
184
+ const meta = v4.globalRegistry.get(header);
185
+ const { id, ...rest } = (meta == null ? void 0 : meta.header) ?? {};
186
+ const headerObject = rest;
187
+ if (isRequired(header, ctx.io)) {
188
+ headerObject.required = true;
189
+ }
190
+ if (!headerObject.description && (meta == null ? void 0 : meta.description)) {
191
+ headerObject.description = meta.description;
192
+ }
193
+ const computedPath = path.join(" > ");
194
+ headerObject.schema = ctx.registry.schemas.setSchema(
195
+ computedPath,
196
+ header,
197
+ ctx.io
723
198
  );
724
- if (extendedSchema) {
725
- return extendedSchema;
199
+ if (id) {
200
+ const ref = {
201
+ $ref: `#/components/headers/${id}`
202
+ };
203
+ ctx.registry.headers.ids.set(id, headerObject);
204
+ ctx.registry.headers.seen.set(header, ref);
205
+ return ref;
726
206
  }
727
- return createObjectSchemaFromShape(
728
- zodObject.shape,
729
- {
730
- unknownKeys: zodObject._def.unknownKeys,
731
- catchAll: zodObject._def.catchall
732
- },
733
- state
734
- );
207
+ ctx.registry.headers.seen.set(header, headerObject);
208
+ return headerObject;
735
209
  };
736
- const createExtendedSchema = (zodObject, baseZodObject, state) => {
737
- var _a, _b, _c, _d, _e;
738
- if (!baseZodObject) {
739
- return void 0;
740
- }
741
- const component = state.components.schemas.get(baseZodObject);
742
- if (component ?? ((_b = (_a = baseZodObject._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.ref)) {
743
- createSchemaObject(baseZodObject, state, ["extended schema"]);
744
- }
745
- const completeComponent = state.components.schemas.get(baseZodObject);
746
- if (!completeComponent) {
210
+ const createHeaders = (headers, ctx, path) => {
211
+ if (!headers) {
747
212
  return void 0;
748
213
  }
749
- const diffOpts = createDiffOpts(
750
- {
751
- unknownKeys: baseZodObject._def.unknownKeys,
752
- catchAll: baseZodObject._def.catchall
753
- },
754
- {
755
- unknownKeys: zodObject._def.unknownKeys,
756
- catchAll: zodObject._def.catchall
214
+ if (isAnyZodType(headers)) {
215
+ const zodObject = unwrapZodObject(headers, ctx.io, path);
216
+ const headersObject = {};
217
+ for (const [key, zodSchema] of Object.entries(zodObject._zod.def.shape)) {
218
+ const header = createHeader(zodSchema, ctx, [...path, key]);
219
+ headersObject[key] = header;
757
220
  }
758
- );
759
- if (!diffOpts) {
760
- return void 0;
221
+ return headersObject;
761
222
  }
762
- const diffShape = createShapeDiff(
763
- baseZodObject._def.shape(),
764
- zodObject._def.shape()
765
- );
766
- if (!diffShape) {
767
- return void 0;
223
+ return headers;
224
+ };
225
+ const isISpecificationExtension = (key) => key.startsWith("x-");
226
+ const createResponse = (response, ctx, path) => {
227
+ const seenResponse = ctx.registry.responses.seen.get(response);
228
+ if (seenResponse) {
229
+ return seenResponse;
768
230
  }
769
- const extendedSchema = createObjectSchemaFromShape(
770
- diffShape,
771
- diffOpts,
772
- state,
773
- true
774
- );
775
- const schemaLength = Object.keys(extendedSchema.schema).length;
776
- const effects = flattenEffects([
777
- completeComponent.type === "complete" ? completeComponent.effects : [],
778
- completeComponent.type === "in-progress" ? [
779
- {
780
- type: "component",
781
- zodType: zodObject,
782
- path: [...state.path]
783
- }
784
- ] : [],
785
- extendedSchema.effects
786
- ]);
787
- if (schemaLength === 0) {
788
- return {
789
- type: "ref",
790
- schema: {
791
- $ref: createComponentSchemaRef(
792
- completeComponent.ref,
793
- (_c = state.documentOptions) == null ? void 0 : _c.componentRefPath
794
- )
795
- },
796
- schemaObject: completeComponent.type === "complete" ? completeComponent.schemaObject : void 0,
797
- zodType: zodObject,
798
- effects
799
- };
231
+ const { content, headers, id, ...rest } = response;
232
+ const responseObject = rest;
233
+ if (id) {
234
+ ctx.registry.responses.ids.set(id, responseObject);
800
235
  }
801
- if (schemaLength === 1 && extendedSchema.schema.description) {
802
- return createDescriptionMetadata(
803
- {
804
- type: "ref",
805
- schema: {
806
- $ref: createComponentSchemaRef(
807
- completeComponent.ref,
808
- (_d = state.documentOptions) == null ? void 0 : _d.componentRefPath
809
- )
810
- },
811
- schemaObject: completeComponent.type === "complete" ? completeComponent.schemaObject : void 0,
812
- zodType: zodObject,
813
- effects
814
- },
815
- extendedSchema.schema.description,
816
- state
817
- );
236
+ ctx.registry.responses.seen.set(response, responseObject);
237
+ const maybeHeaders = createHeaders(headers, ctx, [...path, "headers"]);
238
+ if (maybeHeaders) {
239
+ responseObject.headers = maybeHeaders;
818
240
  }
819
- return {
820
- type: "schema",
821
- schema: {
822
- allOf: [
823
- {
824
- $ref: createComponentSchemaRef(
825
- completeComponent.ref,
826
- (_e = state.documentOptions) == null ? void 0 : _e.componentRefPath
827
- )
828
- }
829
- ],
830
- ...extendedSchema.schema
831
- },
832
- effects: flattenEffects([
833
- completeComponent.type === "complete" ? completeComponent.effects : [],
834
- completeComponent.type === "in-progress" ? [
835
- {
836
- type: "component",
837
- zodType: zodObject,
838
- path: [...state.path]
839
- }
840
- ] : [],
841
- extendedSchema.effects
842
- ])
843
- };
241
+ if (content) {
242
+ responseObject.content = createContent(content, ctx, [...path, "content"]);
243
+ }
244
+ return responseObject;
844
245
  };
845
- const createDiffOpts = (baseOpts, extendedOpts) => {
846
- if (baseOpts.unknownKeys === "strict" || !isZodType(baseOpts.catchAll, "ZodNever")) {
246
+ const createResponses = (responses, ctx, path) => {
247
+ if (!responses) {
847
248
  return void 0;
848
249
  }
849
- return {
850
- catchAll: extendedOpts.catchAll,
851
- unknownKeys: extendedOpts.unknownKeys
852
- };
853
- };
854
- const createShapeDiff = (baseObj, extendedObj) => {
855
- const acc = {};
856
- for (const [key, val] of Object.entries(extendedObj)) {
857
- const baseValue = baseObj[key];
858
- if (val === baseValue) {
250
+ const responsesObject = {};
251
+ for (const [statusCode, response] of Object.entries(responses)) {
252
+ if (!response) {
859
253
  continue;
860
254
  }
861
- if (baseValue === void 0) {
862
- acc[key] = extendedObj[key];
255
+ if (isISpecificationExtension(statusCode)) {
256
+ responsesObject[statusCode] = response;
863
257
  continue;
864
258
  }
865
- return null;
866
- }
867
- return acc;
868
- };
869
- const mapAdditionalProperties = ({ unknownKeys, catchAll }, state) => {
870
- if (!isZodType(catchAll, "ZodNever")) {
871
- return createSchemaObject(catchAll, state, ["additional properties"]);
872
- }
873
- if (unknownKeys === "strict") {
874
- return false;
875
- }
876
- if (unknownKeys === "passthrough") {
877
- return true;
259
+ if ("$ref" in response) {
260
+ responsesObject[statusCode] = response;
261
+ continue;
262
+ }
263
+ const responseObject = createResponse(
264
+ response,
265
+ ctx,
266
+ [...path, statusCode]
267
+ );
268
+ responsesObject[statusCode] = responseObject;
878
269
  }
879
- return void 0;
270
+ return responsesObject;
880
271
  };
881
- const createObjectSchemaFromShape = (shape, { unknownKeys, catchAll }, state, omitType) => {
882
- const properties = mapProperties(shape, state);
883
- const required = mapRequired(properties, shape, state);
884
- const additionalProperties = mapAdditionalProperties(
885
- { catchAll, unknownKeys },
886
- state
272
+ const createOperation = (operationObject, registry, path) => {
273
+ const {
274
+ parameters,
275
+ requestParams,
276
+ requestBody,
277
+ responses,
278
+ callbacks,
279
+ ...rest
280
+ } = operationObject;
281
+ const operation = rest;
282
+ const maybeManualParameters = createManualParameters(
283
+ parameters,
284
+ {
285
+ registry,
286
+ io: "input"
287
+ },
288
+ [...path, "parameters"]
887
289
  );
888
- return {
889
- type: "schema",
890
- schema: {
891
- ...!omitType && { type: "object" },
892
- ...properties && { properties: properties.properties },
893
- ...(required == null ? void 0 : required.required.length) && { required: required.required },
894
- ...additionalProperties !== void 0 && {
895
- additionalProperties: typeof additionalProperties === "object" ? additionalProperties.schema : additionalProperties
896
- }
290
+ const maybeRequestParams = createParameters(
291
+ requestParams,
292
+ {
293
+ registry,
294
+ io: "input"
897
295
  },
898
- effects: flattenEffects([
899
- ...(properties == null ? void 0 : properties.effects) ?? [],
900
- typeof additionalProperties === "object" && (additionalProperties == null ? void 0 : additionalProperties.effects),
901
- required == null ? void 0 : required.effects
902
- ])
903
- };
904
- };
905
- const mapRequired = (properties, shape, state) => {
906
- if (!properties) {
907
- return void 0;
296
+ [...path, "requestParams"]
297
+ );
298
+ if (maybeRequestParams || maybeManualParameters) {
299
+ operation.parameters = [
300
+ ...maybeRequestParams ?? [],
301
+ ...maybeManualParameters ?? []
302
+ ];
908
303
  }
909
- const { required, effects } = Object.entries(properties.schemas).reduce(
910
- (acc, [key, schemaOrRef]) => {
911
- const zodSchema = shape[key];
912
- if (!zodSchema) {
913
- throw new Error("Property somehow doesn't exist in shape");
914
- }
915
- const result = zodSchema.safeParse(void 0);
916
- if (!result.success) {
917
- acc.required.push(key);
918
- return acc;
919
- }
920
- if (result.data !== void 0) {
921
- const baseEffect = {
922
- zodType: zodSchema,
923
- path: [...state.path, `property: ${key}`]
924
- };
925
- const effect = schemaOrRef.type === "ref" ? {
926
- ...baseEffect,
927
- type: "component"
928
- } : {
929
- ...baseEffect,
930
- type: "schema",
931
- creationType: state.type
932
- };
933
- acc.effects.push(effect);
934
- if (state.type === "output") {
935
- acc.required.push(key);
936
- }
937
- }
938
- return acc;
939
- },
304
+ const maybeRequestBody = createRequestBody(
305
+ requestBody,
940
306
  {
941
- required: [],
942
- effects: []
943
- }
307
+ registry,
308
+ io: "input"
309
+ },
310
+ [...path, "requestBody"]
944
311
  );
945
- return { required, effects };
946
- };
947
- const mapProperties = (shape, state) => {
948
- const shapeEntries = Object.entries(shape);
949
- if (!shapeEntries.length) {
950
- return void 0;
312
+ if (maybeRequestBody) {
313
+ operation.requestBody = maybeRequestBody;
951
314
  }
952
- return shapeEntries.reduce(
953
- (acc, [key, zodSchema]) => {
954
- if (isOptionalObjectKey(zodSchema)) {
955
- return acc;
956
- }
957
- const schema = createSchemaObject(zodSchema, state, [`property: ${key}`]);
958
- acc.schemas[key] = schema;
959
- acc.properties[key] = schema.schema;
960
- acc.effects.push(schema.effects);
961
- return acc;
962
- },
315
+ const maybeResponses = createResponses(
316
+ responses,
963
317
  {
964
- schemas: {},
965
- properties: {},
966
- effects: []
967
- }
318
+ registry,
319
+ io: "output"
320
+ },
321
+ [...path, "responses"]
968
322
  );
969
- };
970
- const createPipelineSchema = (zodPipeline, state) => {
971
- var _a, _b, _c, _d, _e, _f;
972
- 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") {
973
- return createSchemaObject(zodPipeline._def.in, state, ["pipeline input"]);
974
- }
975
- if (((_f = (_e = zodPipeline._def.zodOpenApi) == null ? void 0 : _e.openapi) == null ? void 0 : _f.effectType) === "output") {
976
- return createSchemaObject(zodPipeline._def.out, state, ["pipeline output"]);
977
- }
978
- if (state.type === "input") {
979
- const schema2 = createSchemaObject(zodPipeline._def.in, state, [
980
- "pipeline input"
981
- ]);
982
- return {
983
- ...schema2,
984
- effects: flattenEffects([
985
- [
986
- {
987
- type: "schema",
988
- creationType: "input",
989
- path: [...state.path],
990
- zodType: zodPipeline
991
- }
992
- ],
993
- schema2.effects
994
- ])
995
- };
323
+ if (maybeResponses) {
324
+ operation.responses = maybeResponses;
996
325
  }
997
- const schema = createSchemaObject(zodPipeline._def.out, state, [
998
- "pipeline output"
999
- ]);
1000
- return {
1001
- ...schema,
1002
- effects: flattenEffects([
1003
- [
1004
- {
1005
- type: "schema",
1006
- creationType: "output",
1007
- path: [...state.path],
1008
- zodType: zodPipeline
1009
- }
1010
- ],
1011
- schema.effects
1012
- ])
1013
- };
1014
- };
1015
- const createPreprocessSchema = (zodPreprocess, state) => createSchemaObject(zodPreprocess._def.schema, state, ["preprocess schema"]);
1016
- const createReadonlySchema = (zodReadonly, state) => (
1017
- // Readonly doesn't change OpenAPI schema
1018
- createSchemaObject(zodReadonly._def.innerType, state, ["readonly"])
1019
- );
1020
- const createRecordSchema = (zodRecord, state) => {
1021
- const additionalProperties = createSchemaObject(
1022
- zodRecord.valueSchema,
1023
- state,
1024
- ["record value"]
1025
- );
1026
- const keySchema = createSchemaObject(zodRecord.keySchema, state, [
1027
- "record key"
326
+ const maybeCallbacks = createCallbacks(callbacks, registry, [
327
+ ...path,
328
+ "callbacks"
1028
329
  ]);
1029
- const maybeComponent = state.components.schemas.get(zodRecord.keySchema);
1030
- const isComplete = maybeComponent && maybeComponent.type === "complete";
1031
- const maybeSchema = isComplete && maybeComponent.schemaObject;
1032
- const maybeEffects = isComplete && maybeComponent.effects || void 0;
1033
- const renderedKeySchema = maybeSchema || keySchema.schema;
1034
- if ("enum" in renderedKeySchema && renderedKeySchema.enum) {
1035
- return {
1036
- type: "schema",
1037
- schema: {
1038
- type: "object",
1039
- properties: renderedKeySchema.enum.reduce((acc, key) => {
1040
- acc[key] = additionalProperties.schema;
1041
- return acc;
1042
- }, {}),
1043
- additionalProperties: false
1044
- },
1045
- effects: flattenEffects([
1046
- keySchema.effects,
1047
- additionalProperties.effects,
1048
- maybeEffects
1049
- ])
1050
- };
1051
- }
1052
- if (satisfiesVersion(state.components.openapi, "3.1.0") && "type" in renderedKeySchema && renderedKeySchema.type === "string" && Object.keys(renderedKeySchema).length > 1) {
1053
- return {
1054
- type: "schema",
1055
- schema: {
1056
- type: "object",
1057
- propertyNames: keySchema.schema,
1058
- additionalProperties: additionalProperties.schema
1059
- },
1060
- effects: flattenEffects([
1061
- keySchema.effects,
1062
- additionalProperties.effects
1063
- ])
1064
- };
330
+ if (maybeCallbacks) {
331
+ operation.callbacks = maybeCallbacks;
1065
332
  }
1066
- return {
1067
- type: "schema",
1068
- schema: {
1069
- type: "object",
1070
- additionalProperties: additionalProperties.schema
1071
- },
1072
- effects: additionalProperties.effects
1073
- };
1074
- };
1075
- const createRefineSchema = (zodRefine, state) => createSchemaObject(zodRefine._def.schema, state, ["refine schema"]);
1076
- const createSetSchema = (zodSet, state) => {
1077
- var _a, _b;
1078
- const schema = zodSet._def.valueType;
1079
- const minItems = (_a = zodSet._def.minSize) == null ? void 0 : _a.value;
1080
- const maxItems = (_b = zodSet._def.maxSize) == null ? void 0 : _b.value;
1081
- const itemSchema = createSchemaObject(schema, state, ["set items"]);
1082
- return {
1083
- type: "schema",
1084
- schema: {
1085
- type: "array",
1086
- items: itemSchema.schema,
1087
- uniqueItems: true,
1088
- ...minItems !== void 0 && { minItems },
1089
- ...maxItems !== void 0 && { maxItems }
1090
- },
1091
- effects: itemSchema.effects
1092
- };
333
+ return operation;
1093
334
  };
1094
- const createStringSchema = (zodString, state) => {
1095
- var _a, _b, _c, _d, _e, _f, _g, _h;
1096
- const zodStringChecks = getZodStringChecks(zodString);
1097
- const format = mapStringFormat(zodStringChecks);
1098
- const patterns = mapPatterns(zodStringChecks);
1099
- const minLength = ((_b = (_a = zodStringChecks.length) == null ? void 0 : _a[0]) == null ? void 0 : _b.value) ?? ((_d = (_c = zodStringChecks.min) == null ? void 0 : _c[0]) == null ? void 0 : _d.value);
1100
- const maxLength = ((_f = (_e = zodStringChecks.length) == null ? void 0 : _e[0]) == null ? void 0 : _f.value) ?? ((_h = (_g = zodStringChecks.max) == null ? void 0 : _g[0]) == null ? void 0 : _h.value);
1101
- const contentEncoding = satisfiesVersion(state.components.openapi, "3.1.0") ? mapContentEncoding(zodStringChecks) : void 0;
1102
- if (patterns.length <= 1) {
1103
- return {
1104
- type: "schema",
1105
- schema: {
1106
- type: "string",
1107
- ...format && { format },
1108
- ...patterns[0] && { pattern: patterns[0] },
1109
- ...minLength !== void 0 && { minLength },
1110
- ...maxLength !== void 0 && { maxLength },
1111
- ...contentEncoding && { contentEncoding }
1112
- }
1113
- };
1114
- }
1115
- return {
1116
- type: "schema",
1117
- schema: {
1118
- allOf: [
1119
- {
1120
- type: "string",
1121
- ...format && { format },
1122
- ...patterns[0] && { pattern: patterns[0] },
1123
- ...minLength !== void 0 && { minLength },
1124
- ...maxLength !== void 0 && { maxLength },
1125
- ...contentEncoding && { contentEncoding }
1126
- },
1127
- ...patterns.slice(1).map(
1128
- (pattern) => ({
1129
- type: "string",
1130
- pattern
1131
- })
1132
- )
1133
- ]
1134
- }
1135
- };
1136
- };
1137
- const getZodStringChecks = (zodString) => zodString._def.checks.reduce(
1138
- (acc, check) => {
1139
- const mapping = acc[check.kind];
1140
- if (mapping) {
1141
- mapping.push(check);
1142
- return acc;
1143
- }
1144
- acc[check.kind] = [check];
1145
- return acc;
1146
- },
1147
- {}
1148
- );
1149
- const mapPatterns = (zodStringChecks) => {
1150
- const startsWith = mapStartsWith(zodStringChecks);
1151
- const endsWith = mapEndsWith(zodStringChecks);
1152
- const regex = mapRegex(zodStringChecks);
1153
- const includes = mapIncludes(zodStringChecks);
1154
- const patterns = [
1155
- ...regex ?? [],
1156
- ...startsWith ? [startsWith] : [],
1157
- ...endsWith ? [endsWith] : [],
1158
- ...includes ?? []
1159
- ];
1160
- return patterns;
1161
- };
1162
- const mapStartsWith = (zodStringChecks) => {
1163
- var _a, _b;
1164
- if ((_b = (_a = zodStringChecks.startsWith) == null ? void 0 : _a[0]) == null ? void 0 : _b.value) {
1165
- return `^${zodStringChecks.startsWith[0].value}`;
1166
- }
1167
- return void 0;
1168
- };
1169
- const mapEndsWith = (zodStringChecks) => {
1170
- var _a, _b;
1171
- if ((_b = (_a = zodStringChecks.endsWith) == null ? void 0 : _a[0]) == null ? void 0 : _b.value) {
1172
- return `${zodStringChecks.endsWith[0].value}$`;
1173
- }
1174
- return void 0;
1175
- };
1176
- const mapRegex = (zodStringChecks) => {
1177
- var _a;
1178
- return (_a = zodStringChecks.regex) == null ? void 0 : _a.map((regexCheck) => regexCheck.regex.source);
1179
- };
1180
- const mapIncludes = (zodStringChecks) => {
1181
- var _a;
1182
- return (_a = zodStringChecks.includes) == null ? void 0 : _a.map((includeCheck) => {
1183
- if (includeCheck.position === 0) {
1184
- return `^${includeCheck.value}`;
1185
- }
1186
- if (includeCheck.position) {
1187
- return `^.{${includeCheck.position}}${includeCheck.value}`;
335
+ const createPathItem = (pathItem, registry, path) => {
336
+ const pathItemObject = {};
337
+ const { id, ...rest } = pathItem;
338
+ for (const [key, value] of Object.entries(rest)) {
339
+ if (key === "get" || key === "put" || key === "post" || key === "delete" || key === "options" || key === "head" || key === "patch" || key === "trace") {
340
+ pathItemObject[key] = createOperation(
341
+ value,
342
+ registry,
343
+ [...path, key]
344
+ );
345
+ continue;
1188
346
  }
1189
- return includeCheck.value;
1190
- });
1191
- };
1192
- const mapStringFormat = (zodStringChecks) => {
1193
- var _a, _b, _c, _d;
1194
- if (zodStringChecks.uuid) {
1195
- return "uuid";
1196
- }
1197
- if (zodStringChecks.datetime) {
1198
- return "date-time";
1199
- }
1200
- if (zodStringChecks.date) {
1201
- return "date";
1202
- }
1203
- if (zodStringChecks.time) {
1204
- return "time";
1205
- }
1206
- if (zodStringChecks.duration) {
1207
- return "duration";
1208
- }
1209
- if (zodStringChecks.email) {
1210
- return "email";
1211
- }
1212
- if (zodStringChecks.url) {
1213
- return "uri";
1214
- }
1215
- if ((_a = zodStringChecks.ip) == null ? void 0 : _a.every((ip) => ip.version === "v4")) {
1216
- return "ipv4";
1217
- }
1218
- if ((_b = zodStringChecks.ip) == null ? void 0 : _b.every((ip) => ip.version === "v6")) {
1219
- return "ipv6";
1220
- }
1221
- if ((_c = zodStringChecks.cidr) == null ? void 0 : _c.every((ip) => ip.version === "v4")) {
1222
- return "ipv4";
1223
- }
1224
- if ((_d = zodStringChecks.cidr) == null ? void 0 : _d.every((ip) => ip.version === "v6")) {
1225
- return "ipv6";
1226
- }
1227
- return void 0;
1228
- };
1229
- const mapContentEncoding = (zodStringChecks) => {
1230
- if (zodStringChecks.base64) {
1231
- return "base64";
1232
- }
1233
- return void 0;
1234
- };
1235
- const createTupleSchema = (zodTuple, state) => {
1236
- const items = zodTuple.items;
1237
- const rest = zodTuple._def.rest;
1238
- const prefixItems = mapPrefixItems(items, state);
1239
- if (satisfiesVersion(state.components.openapi, "3.1.0")) {
1240
- if (!rest) {
1241
- return {
1242
- type: "schema",
1243
- schema: {
1244
- type: "array",
1245
- maxItems: items.length,
1246
- minItems: items.length,
1247
- ...prefixItems && {
1248
- prefixItems: prefixItems.schemas.map((item) => item.schema)
1249
- }
347
+ if (key === "parameters") {
348
+ pathItemObject[key] = createManualParameters(
349
+ value,
350
+ {
351
+ registry,
352
+ io: "input"
1250
353
  },
1251
- effects: prefixItems == null ? void 0 : prefixItems.effects
1252
- };
354
+ [...path, key]
355
+ );
356
+ continue;
1253
357
  }
1254
- const itemSchema = createSchemaObject(rest, state, ["tuple items"]);
1255
- return {
1256
- type: "schema",
1257
- schema: {
1258
- type: "array",
1259
- items: itemSchema.schema,
1260
- ...prefixItems && {
1261
- prefixItems: prefixItems.schemas.map((item) => item.schema)
1262
- }
1263
- },
1264
- effects: flattenEffects([prefixItems == null ? void 0 : prefixItems.effects, itemSchema.effects])
1265
- };
358
+ pathItemObject[key] = value;
1266
359
  }
1267
- if (!rest) {
1268
- return {
1269
- type: "schema",
1270
- schema: {
1271
- type: "array",
1272
- maxItems: items.length,
1273
- minItems: items.length,
1274
- ...prefixItems && {
1275
- items: { oneOf: prefixItems.schemas.map((item) => item.schema) }
1276
- }
1277
- },
1278
- effects: prefixItems == null ? void 0 : prefixItems.effects
360
+ if (id) {
361
+ const ref = {
362
+ $ref: `#/components/pathItems/${id}`
1279
363
  };
364
+ registry.pathItems.ids.set(id, pathItemObject);
365
+ registry.pathItems.seen.set(pathItem, ref);
366
+ return ref;
1280
367
  }
1281
- if (prefixItems) {
1282
- const restSchema = createSchemaObject(rest, state, ["tuple items"]);
1283
- return {
1284
- type: "schema",
1285
- schema: {
1286
- type: "array",
1287
- items: {
1288
- oneOf: [
1289
- ...prefixItems.schemas.map((item) => item.schema),
1290
- restSchema.schema
1291
- ]
1292
- }
1293
- },
1294
- effects: flattenEffects([restSchema.effects, prefixItems.effects])
1295
- };
1296
- }
1297
- return {
1298
- type: "schema",
1299
- schema: {
1300
- type: "array"
1301
- }
1302
- };
368
+ return pathItemObject;
1303
369
  };
1304
- const mapPrefixItems = (items, state) => {
1305
- if (items.length) {
1306
- const schemas = items.map(
1307
- (item, index) => createSchemaObject(item, state, [`tuple item ${index}`])
1308
- );
1309
- return {
1310
- effects: flattenEffects(schemas.map((s) => s.effects)),
1311
- schemas
1312
- };
370
+ const createPaths = (paths, registry, path) => {
371
+ if (!paths) {
372
+ return void 0;
1313
373
  }
1314
- return void 0;
1315
- };
1316
- const createUnionSchema = (zodUnion, state) => {
1317
- var _a, _b, _c;
1318
- const schemas = zodUnion.options.reduce((acc, option, index) => {
1319
- if (!isOptionalObjectKey(option)) {
1320
- acc.push(createSchemaObject(option, state, [`union option ${index}`]));
374
+ const pathsObject = {};
375
+ for (const [singlePath, pathItemObject] of Object.entries(paths)) {
376
+ if (isISpecificationExtension(singlePath)) {
377
+ pathsObject[singlePath] = pathItemObject;
378
+ continue;
1321
379
  }
1322
- return acc;
1323
- }, []);
1324
- if (((_b = (_a = zodUnion._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.unionOneOf) ?? ((_c = state.documentOptions) == null ? void 0 : _c.unionOneOf)) {
1325
- return {
1326
- type: "schema",
1327
- schema: {
1328
- oneOf: schemas.map((s) => s.schema)
1329
- },
1330
- effects: flattenEffects(schemas.map((s) => s.effects))
1331
- };
1332
- }
1333
- return {
1334
- type: "schema",
1335
- schema: {
1336
- anyOf: schemas.map((s) => s.schema)
1337
- },
1338
- effects: flattenEffects(schemas.map((s) => s.effects))
1339
- };
1340
- };
1341
- const createUnknownSchema = (_zodUnknown) => ({
1342
- type: "schema",
1343
- schema: {}
1344
- });
1345
- const createSchemaSwitch = (zodSchema, previous, state) => {
1346
- var _a, _b;
1347
- if ((_b = (_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.type) {
1348
- return createManualTypeSchema(zodSchema, state);
1349
- }
1350
- if (isZodType(zodSchema, "ZodString")) {
1351
- return createStringSchema(zodSchema, state);
1352
- }
1353
- if (isZodType(zodSchema, "ZodNumber")) {
1354
- return createNumberSchema(zodSchema, state);
1355
- }
1356
- if (isZodType(zodSchema, "ZodBoolean")) {
1357
- return createBooleanSchema();
1358
- }
1359
- if (isZodType(zodSchema, "ZodEnum")) {
1360
- return createEnumSchema(zodSchema);
1361
- }
1362
- if (isZodType(zodSchema, "ZodLiteral")) {
1363
- return createLiteralSchema(zodSchema, state);
1364
- }
1365
- if (isZodType(zodSchema, "ZodNativeEnum")) {
1366
- return createNativeEnumSchema(zodSchema, state);
1367
- }
1368
- if (isZodType(zodSchema, "ZodArray")) {
1369
- return createArraySchema(zodSchema, state);
1370
- }
1371
- if (isZodType(zodSchema, "ZodObject")) {
1372
- return createObjectSchema(zodSchema, previous, state);
1373
- }
1374
- if (isZodType(zodSchema, "ZodUnion")) {
1375
- return createUnionSchema(zodSchema, state);
1376
- }
1377
- if (isZodType(zodSchema, "ZodDiscriminatedUnion")) {
1378
- return createDiscriminatedUnionSchema(zodSchema, state);
1379
- }
1380
- if (isZodType(zodSchema, "ZodNull")) {
1381
- return createNullSchema();
1382
- }
1383
- if (isZodType(zodSchema, "ZodNullable")) {
1384
- return createNullableSchema(zodSchema, state);
1385
- }
1386
- if (isZodType(zodSchema, "ZodOptional")) {
1387
- return createOptionalSchema(zodSchema, state);
1388
- }
1389
- if (isZodType(zodSchema, "ZodReadonly")) {
1390
- return createReadonlySchema(zodSchema, state);
1391
- }
1392
- if (isZodType(zodSchema, "ZodDefault")) {
1393
- return createDefaultSchema(zodSchema, state, previous);
1394
- }
1395
- if (isZodType(zodSchema, "ZodRecord")) {
1396
- return createRecordSchema(zodSchema, state);
1397
- }
1398
- if (isZodType(zodSchema, "ZodTuple")) {
1399
- return createTupleSchema(zodSchema, state);
1400
- }
1401
- if (isZodType(zodSchema, "ZodDate")) {
1402
- return createDateSchema(zodSchema, state);
1403
- }
1404
- if (isZodType(zodSchema, "ZodPipeline")) {
1405
- return createPipelineSchema(zodSchema, state);
1406
- }
1407
- if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "transform") {
1408
- return createTransformSchema(zodSchema, state);
1409
- }
1410
- if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "preprocess") {
1411
- return createPreprocessSchema(zodSchema, state);
1412
- }
1413
- if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "refinement") {
1414
- return createRefineSchema(zodSchema, state);
1415
- }
1416
- if (isZodType(zodSchema, "ZodNativeEnum")) {
1417
- return createNativeEnumSchema(zodSchema, state);
1418
- }
1419
- if (isZodType(zodSchema, "ZodIntersection")) {
1420
- return createIntersectionSchema(zodSchema, state);
1421
- }
1422
- if (isZodType(zodSchema, "ZodCatch")) {
1423
- return createCatchSchema(zodSchema, state, previous);
1424
- }
1425
- if (isZodType(zodSchema, "ZodUnknown") || isZodType(zodSchema, "ZodAny")) {
1426
- return createUnknownSchema();
1427
- }
1428
- if (isZodType(zodSchema, "ZodLazy")) {
1429
- return createLazySchema(zodSchema, state);
1430
- }
1431
- if (isZodType(zodSchema, "ZodBranded")) {
1432
- return createBrandedSchema(zodSchema, state);
1433
- }
1434
- if (isZodType(zodSchema, "ZodSet")) {
1435
- return createSetSchema(zodSchema, state);
1436
- }
1437
- if (isZodType(zodSchema, "ZodBigInt")) {
1438
- return createBigIntSchema();
380
+ pathsObject[singlePath] = createPathItem(pathItemObject, registry, [
381
+ ...path,
382
+ singlePath
383
+ ]);
1439
384
  }
1440
- return createManualTypeSchema(zodSchema, state);
385
+ return pathsObject;
1441
386
  };
1442
- const createNewSchema = ({
1443
- zodSchema,
1444
- previous,
1445
- state
1446
- }) => {
1447
- var _a;
1448
- if (state.visited.has(zodSchema)) {
1449
- throw new Error(
1450
- `The schema at ${state.path.join(
1451
- " > "
1452
- )} needs to be registered because it's circularly referenced`
1453
- );
387
+ const createCallback = (callbackObject, registry, path) => {
388
+ const seenCallback = registry.callbacks.seen.get(callbackObject);
389
+ if (seenCallback) {
390
+ return seenCallback;
1454
391
  }
1455
- state.visited.add(zodSchema);
1456
- const {
1457
- effectType,
1458
- param,
1459
- header,
1460
- ref,
1461
- refType,
1462
- unionOneOf,
1463
- ...additionalMetadata
1464
- } = ((_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a.openapi) ?? {};
1465
- const schema = createSchemaSwitch(zodSchema, previous, state);
1466
- const schemaWithMetadata = enhanceWithMetadata(
1467
- schema,
1468
- additionalMetadata,
1469
- state,
1470
- previous
1471
- );
1472
- state.visited.delete(zodSchema);
1473
- return schemaWithMetadata;
1474
- };
1475
- const createNewRef = ({
1476
- previous,
1477
- ref,
1478
- zodSchema,
1479
- state
1480
- }) => {
1481
- var _a;
1482
- state.components.schemas.set(zodSchema, {
1483
- type: "in-progress",
1484
- ref
1485
- });
1486
- const newSchema = createNewSchema({
1487
- zodSchema,
1488
- previous,
1489
- state: {
1490
- ...state,
1491
- visited: /* @__PURE__ */ new Set()
392
+ const { id, ...rest } = callbackObject;
393
+ const callback = {};
394
+ for (const [name, pathItem] of Object.entries(rest)) {
395
+ if (isISpecificationExtension(name)) {
396
+ callback[name] = pathItem;
397
+ continue;
1492
398
  }
1493
- });
1494
- state.components.schemas.set(zodSchema, {
1495
- type: "complete",
1496
- ref,
1497
- schemaObject: newSchema.schema,
1498
- effects: newSchema.effects
1499
- });
1500
- return {
1501
- type: "ref",
1502
- schema: {
1503
- $ref: createComponentSchemaRef(
1504
- ref,
1505
- (_a = state.documentOptions) == null ? void 0 : _a.componentRefPath
1506
- )
1507
- },
1508
- schemaObject: newSchema.schema,
1509
- effects: newSchema.effects ? [
1510
- {
1511
- type: "component",
1512
- zodType: zodSchema,
1513
- path: [...state.path]
1514
- }
1515
- ] : void 0,
1516
- zodType: zodSchema
1517
- };
1518
- };
1519
- const createExistingRef = (zodSchema, component, state) => {
1520
- var _a, _b;
1521
- if (component && component.type === "complete") {
1522
- return {
1523
- type: "ref",
1524
- schema: {
1525
- $ref: createComponentSchemaRef(
1526
- component.ref,
1527
- (_a = state.documentOptions) == null ? void 0 : _a.componentRefPath
1528
- )
1529
- },
1530
- schemaObject: component.schemaObject,
1531
- effects: component.effects ? [
1532
- {
1533
- type: "component",
1534
- zodType: zodSchema,
1535
- path: [...state.path]
1536
- }
1537
- ] : void 0,
1538
- zodType: zodSchema
1539
- };
399
+ callback[name] = createPathItem(
400
+ pathItem,
401
+ registry,
402
+ [...path, name]
403
+ );
1540
404
  }
1541
- if (component && component.type === "in-progress") {
1542
- return {
1543
- type: "ref",
1544
- schema: {
1545
- $ref: createComponentSchemaRef(
1546
- component.ref,
1547
- (_b = state.documentOptions) == null ? void 0 : _b.componentRefPath
1548
- )
1549
- },
1550
- schemaObject: void 0,
1551
- effects: [
1552
- {
1553
- type: "component",
1554
- zodType: zodSchema,
1555
- path: [...state.path]
1556
- }
1557
- ],
1558
- zodType: zodSchema
405
+ if (id) {
406
+ const ref = {
407
+ $ref: `#/components/callbacks/${id}`
1559
408
  };
409
+ registry.callbacks.ids.set(id, callback);
410
+ registry.callbacks.seen.set(callbackObject, ref);
411
+ return ref;
1560
412
  }
1561
- return;
1562
- };
1563
- const createSchemaOrRef = (zodSchema, state, onlyRef) => {
1564
- var _a, _b, _c, _d;
1565
- const component = state.components.schemas.get(zodSchema);
1566
- const existingRef = createExistingRef(zodSchema, component, state);
1567
- if (existingRef) {
1568
- return existingRef;
1569
- }
1570
- const previous = ((_a = zodSchema._def.zodOpenApi) == null ? void 0 : _a[extendZodSymbols.previousSymbol]) ? createSchemaOrRef(
1571
- zodSchema._def.zodOpenApi[extendZodSymbols.previousSymbol],
1572
- state,
1573
- true
1574
- ) : void 0;
1575
- const current = ((_b = zodSchema._def.zodOpenApi) == null ? void 0 : _b[extendZodSymbols.currentSymbol]) && zodSchema._def.zodOpenApi[extendZodSymbols.currentSymbol] !== zodSchema ? createSchemaOrRef(
1576
- zodSchema._def.zodOpenApi[extendZodSymbols.currentSymbol],
1577
- state,
1578
- true
1579
- ) : void 0;
1580
- const ref = ((_d = (_c = zodSchema._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.ref) ?? (component == null ? void 0 : component.ref);
1581
- if (ref) {
1582
- return current ? createNewSchema({ zodSchema, previous: current, state }) : createNewRef({ ref, zodSchema, previous, state });
1583
- }
1584
- if (onlyRef) {
1585
- return previous ?? current;
1586
- }
1587
- return createNewSchema({ zodSchema, previous: previous ?? current, state });
1588
- };
1589
- const createSchemaObject = (zodSchema, state, subpath) => {
1590
- state.path.push(...subpath);
1591
- const schema = createSchemaOrRef(zodSchema, state);
1592
- if (!schema) {
1593
- throw new Error("Schema does not exist");
1594
- }
1595
- state.path.pop();
1596
- return schema;
1597
- };
1598
- const createSchema = (zodSchema, state, subpath) => {
1599
- const schema = createSchemaObject(zodSchema, state, subpath);
1600
- if (schema.effects) {
1601
- verifyEffects(schema.effects, state);
1602
- }
1603
- return schema.schema;
1604
- };
1605
- const createMediaTypeSchema = (schemaObject, components, type, subpath, documentOptions) => {
1606
- if (!schemaObject) {
1607
- return void 0;
1608
- }
1609
- if (!isAnyZodType(schemaObject)) {
1610
- return schemaObject;
1611
- }
1612
- return createSchema(
1613
- schemaObject,
1614
- {
1615
- components,
1616
- type,
1617
- path: [],
1618
- visited: /* @__PURE__ */ new Set(),
1619
- documentOptions
1620
- },
1621
- subpath
1622
- );
413
+ registry.callbacks.seen.set(callbackObject, callback);
414
+ return callback;
1623
415
  };
1624
- const createMediaTypeObject = (mediaTypeObject, components, type, subpath, documentOptions) => {
1625
- if (!mediaTypeObject) {
416
+ const createCallbacks = (callbacks, registry, path) => {
417
+ if (!callbacks) {
1626
418
  return void 0;
1627
419
  }
1628
- return {
1629
- ...mediaTypeObject,
1630
- schema: createMediaTypeSchema(
1631
- mediaTypeObject.schema,
1632
- components,
1633
- type,
1634
- [...subpath, "schema"],
1635
- documentOptions
1636
- )
1637
- };
1638
- };
1639
- const createContent = (contentObject, components, type, subpath, documentOptions) => Object.entries(contentObject).reduce(
1640
- (acc, [mediaType, zodOpenApiMediaTypeObject]) => {
1641
- const mediaTypeObject = createMediaTypeObject(
1642
- zodOpenApiMediaTypeObject,
1643
- components,
1644
- type,
1645
- [...subpath, mediaType],
1646
- documentOptions
1647
- );
1648
- if (mediaTypeObject) {
1649
- acc[mediaType] = mediaTypeObject;
1650
- }
1651
- return acc;
1652
- },
1653
- {}
1654
- );
1655
- const createComponentParamRef = (ref) => `#/components/parameters/${ref}`;
1656
- const createBaseParameter = (schema, components, subpath, documentOptions) => {
1657
- var _a, _b, _c, _d;
1658
- const { ref, ...rest } = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) ?? {};
1659
- const state = {
1660
- components,
1661
- type: "input",
1662
- path: [],
1663
- visited: /* @__PURE__ */ new Set(),
1664
- documentOptions
1665
- };
1666
- const schemaObject = createSchema(schema, state, [...subpath, "schema"]);
1667
- const required = !schema.isOptional();
1668
- const description = ((_d = (_c = schema._def.zodOpenApi) == null ? void 0 : _c.openapi) == null ? void 0 : _d.description) ?? schema._def.description;
1669
- return {
1670
- ...description && { description },
1671
- ...rest,
1672
- ...schema && { schema: schemaObject },
1673
- ...required && { required }
1674
- };
1675
- };
1676
- const createParamOrRef = (zodSchema, components, subpath, type, name, documentOptions) => {
1677
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1678
- const component = components.parameters.get(zodSchema);
1679
- 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;
1680
- 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;
1681
- if (!paramType) {
1682
- throw new Error("Parameter type missing");
1683
- }
1684
- if (!paramName) {
1685
- throw new Error("Parameter name missing");
1686
- }
1687
- if (component && component.type === "complete") {
1688
- if (!("$ref" in component.paramObject) && (component.in !== paramType || component.name !== paramName)) {
1689
- throw new Error(`parameterRef "${component.ref}" is already registered`);
420
+ const callbacksObject = {};
421
+ for (const [name, value] of Object.entries(callbacks)) {
422
+ if (isISpecificationExtension(name)) {
423
+ callbacksObject[name] = value;
424
+ continue;
1690
425
  }
1691
- return {
1692
- $ref: createComponentParamRef(component.ref)
1693
- };
1694
- }
1695
- const baseParamOrRef = createBaseParameter(
1696
- zodSchema,
1697
- components,
1698
- subpath,
1699
- documentOptions
1700
- );
1701
- if ("$ref" in baseParamOrRef) {
1702
- throw new Error("Unexpected Error: received a reference object");
1703
- }
1704
- 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);
1705
- const paramObject = {
1706
- in: paramType,
1707
- name: paramName,
1708
- ...baseParamOrRef
1709
- };
1710
- if (ref) {
1711
- components.parameters.set(zodSchema, {
1712
- type: "complete",
1713
- paramObject,
1714
- ref,
1715
- in: paramType,
1716
- name: paramName
1717
- });
1718
- return {
1719
- $ref: createComponentParamRef(ref)
1720
- };
1721
- }
1722
- return paramObject;
1723
- };
1724
- const createParameters = (type, zodObjectType, components, subpath, documentOptions) => {
1725
- if (!zodObjectType) {
1726
- return [];
1727
- }
1728
- const zodObject = getZodObject(zodObjectType, "input").shape;
1729
- return Object.entries(zodObject).map(
1730
- ([key, zodSchema]) => createParamOrRef(
1731
- zodSchema,
1732
- components,
1733
- [...subpath, key],
1734
- type,
1735
- key,
1736
- documentOptions
1737
- )
1738
- );
1739
- };
1740
- const createRequestParams = (requestParams, components, subpath, documentOptions) => {
1741
- if (!requestParams) {
1742
- return [];
1743
- }
1744
- const pathParams = createParameters(
1745
- "path",
1746
- requestParams.path,
1747
- components,
1748
- [...subpath, "path"],
1749
- documentOptions
1750
- );
1751
- const queryParams = createParameters(
1752
- "query",
1753
- requestParams.query,
1754
- components,
1755
- [...subpath, "query"],
1756
- documentOptions
1757
- );
1758
- const cookieParams = createParameters(
1759
- "cookie",
1760
- requestParams.cookie,
1761
- components,
1762
- [...subpath, "cookie"],
1763
- documentOptions
1764
- );
1765
- const headerParams = createParameters(
1766
- "header",
1767
- requestParams.header,
1768
- components,
1769
- [...subpath, "header"],
1770
- documentOptions
1771
- );
1772
- return [...pathParams, ...queryParams, ...cookieParams, ...headerParams];
1773
- };
1774
- const createManualParameters = (parameters, components, subpath, documentOptions) => (parameters == null ? void 0 : parameters.map((param, index) => {
1775
- if (isAnyZodType(param)) {
1776
- return createParamOrRef(
1777
- param,
1778
- components,
1779
- [...subpath, `param index ${index}`],
1780
- void 0,
1781
- void 0,
1782
- documentOptions
426
+ callbacksObject[name] = createCallback(
427
+ value,
428
+ registry,
429
+ [...path, name]
1783
430
  );
1784
431
  }
1785
- return param;
1786
- })) ?? [];
1787
- const createParametersObject = (parameters, requestParams, components, subpath, documentOptions) => {
1788
- const manualParameters = createManualParameters(
1789
- parameters,
1790
- components,
1791
- subpath,
1792
- documentOptions
1793
- );
1794
- const createdParams = createRequestParams(
1795
- requestParams,
1796
- components,
1797
- subpath,
1798
- documentOptions
1799
- );
1800
- const combinedParameters = [
1801
- ...manualParameters,
1802
- ...createdParams
1803
- ];
1804
- return combinedParameters.length ? combinedParameters : void 0;
432
+ return callbacksObject;
433
+ };
434
+ const override = (ctx) => {
435
+ const def = ctx.zodSchema._zod.def;
436
+ switch (def.type) {
437
+ case "bigint": {
438
+ ctx.jsonSchema.type = "integer";
439
+ ctx.jsonSchema.format = "int64";
440
+ break;
441
+ }
442
+ case "union": {
443
+ if ("discriminator" in def && typeof def.discriminator === "string") {
444
+ ctx.jsonSchema.oneOf = ctx.jsonSchema.anyOf;
445
+ delete ctx.jsonSchema.anyOf;
446
+ ctx.jsonSchema.type = "object";
447
+ ctx.jsonSchema.discriminator = {
448
+ propertyName: def.discriminator
449
+ };
450
+ const mapping = {};
451
+ for (const [index, obj] of Object.entries(
452
+ ctx.jsonSchema.oneOf
453
+ )) {
454
+ const ref = obj.$ref;
455
+ if (!ref) {
456
+ return;
457
+ }
458
+ const discriminatorValues = def.options[Number(index)]._zod.propValues[def.discriminator];
459
+ if (!(discriminatorValues == null ? void 0 : discriminatorValues.size)) {
460
+ return;
461
+ }
462
+ for (const value of [...discriminatorValues ?? []]) {
463
+ if (typeof value !== "string") {
464
+ return;
465
+ }
466
+ mapping[value] = ref;
467
+ }
468
+ }
469
+ ctx.jsonSchema.discriminator.mapping = mapping;
470
+ }
471
+ const meta = ctx.zodSchema.meta();
472
+ if (typeof (meta == null ? void 0 : meta.unionOneOf) === "boolean") {
473
+ if (meta.unionOneOf) {
474
+ ctx.jsonSchema.oneOf = ctx.jsonSchema.anyOf;
475
+ delete ctx.jsonSchema.anyOf;
476
+ }
477
+ delete ctx.jsonSchema.unionOneOf;
478
+ }
479
+ break;
480
+ }
481
+ case "date": {
482
+ ctx.jsonSchema.type = "string";
483
+ break;
484
+ }
485
+ case "literal": {
486
+ if (def.values.includes(void 0)) {
487
+ break;
488
+ }
489
+ break;
490
+ }
491
+ }
1805
492
  };
1806
- const getZodObject = (schema, type) => {
1807
- if (isZodType(schema, "ZodObject")) {
1808
- return schema;
1809
- }
1810
- if (isZodType(schema, "ZodLazy")) {
1811
- return getZodObject(schema.schema, type);
1812
- }
1813
- if (isZodType(schema, "ZodEffects")) {
1814
- return getZodObject(schema.innerType(), type);
493
+ const validate = (ctx, opts) => {
494
+ var _a;
495
+ if (Object.keys(ctx.jsonSchema).length) {
496
+ return;
1815
497
  }
1816
- if (isZodType(schema, "ZodBranded")) {
1817
- return getZodObject(schema.unwrap(), type);
498
+ const def = ctx.zodSchema._zod.def;
499
+ if ((_a = opts.allowEmptySchema) == null ? void 0 : _a[def.type]) {
500
+ return;
1818
501
  }
1819
- if (isZodType(schema, "ZodPipeline")) {
1820
- if (type === "input") {
1821
- return getZodObject(schema._def.in, type);
502
+ switch (def.type) {
503
+ case "any": {
504
+ return;
505
+ }
506
+ case "unknown": {
507
+ return;
508
+ }
509
+ case "pipe": {
510
+ if (ctx.io === "output") {
511
+ throw new Error(
512
+ "Zod transform schemas are not supported in output schemas. Please use `.overwrite()` or wrap the schema in a `.pipe()`"
513
+ );
514
+ }
515
+ return;
516
+ }
517
+ case "transform": {
518
+ if (ctx.io === "output") {
519
+ return;
520
+ }
521
+ break;
522
+ }
523
+ case "literal": {
524
+ if (def.values.includes(void 0)) {
525
+ throw new Error(
526
+ "Zod literal schemas cannot include `undefined` as a value. Please use `z.undefined()` or `.optional()` instead."
527
+ );
528
+ }
529
+ return;
1822
530
  }
1823
- return getZodObject(schema._def.out, type);
1824
531
  }
1825
- throw new Error("failed to find ZodObject in schema");
532
+ throw new Error(
533
+ `Zod schema of type \`${def.type}\` cannot be represented in OpenAPI. Please assign it metadata with \`.meta()\``
534
+ );
1826
535
  };
1827
- const isISpecificationExtension = (key) => key.startsWith("x-");
1828
- const createResponseHeaders = (responseHeaders, components, documentOptions) => {
1829
- if (!responseHeaders) {
1830
- return void 0;
536
+ const renameComponents = (components, outputIds, ctx) => {
537
+ const componentsToRename = /* @__PURE__ */ new Map();
538
+ const componentDependencies = /* @__PURE__ */ new Map();
539
+ const stringifiedComponents = /* @__PURE__ */ new Map();
540
+ for (const [key, value] of Object.entries(components)) {
541
+ const stringified = JSON.stringify(value);
542
+ const matches = stringified.matchAll(/"#\/components\/schemas\/([^"]+)"/g);
543
+ const dependencies = /* @__PURE__ */ new Set();
544
+ for (const match of matches) {
545
+ const dep = match[1];
546
+ if (dep !== key) {
547
+ dependencies.add(dep);
548
+ }
549
+ }
550
+ stringifiedComponents.set(key, stringified);
551
+ componentDependencies.set(key, {
552
+ dependencies
553
+ });
1831
554
  }
1832
- if (isAnyZodType(responseHeaders)) {
1833
- return Object.entries(responseHeaders.shape).reduce((acc, [key, zodSchema]) => {
1834
- acc[key] = createHeaderOrRef(zodSchema, components, documentOptions);
1835
- return acc;
1836
- }, {});
555
+ for (const [key] of stringifiedComponents) {
556
+ const registeredComponent = ctx.registry.schemas.ids.get(key);
557
+ if (!registeredComponent) {
558
+ continue;
559
+ }
560
+ if (isDependencyPure(
561
+ componentDependencies,
562
+ stringifiedComponents,
563
+ ctx.registry,
564
+ key
565
+ )) {
566
+ continue;
567
+ }
568
+ const newName = outputIds.get(key) ?? `${key}Output`;
569
+ componentsToRename.set(key, newName);
570
+ components[newName] = components[key];
571
+ delete components[key];
572
+ continue;
1837
573
  }
1838
- return responseHeaders;
574
+ return componentsToRename;
1839
575
  };
1840
- const createHeaderOrRef = (schema, components, documentOptions) => {
1841
- var _a, _b, _c;
1842
- const component = components.headers.get(schema);
1843
- if (component && component.type === "complete") {
1844
- return {
1845
- $ref: createComponentHeaderRef(component.ref)
1846
- };
576
+ const isDependencyPure = (componentDependencies, stringifiedComponents, registry, key, visited = /* @__PURE__ */ new Set()) => {
577
+ if (visited.has(key)) {
578
+ return true;
1847
579
  }
1848
- const baseHeader = createBaseHeader(schema, components, documentOptions);
1849
- if ("$ref" in baseHeader) {
1850
- throw new Error("Unexpected Error: received a reference object");
580
+ const dependencies = componentDependencies.get(key);
581
+ if (dependencies.pure !== void 0) {
582
+ return dependencies.pure;
1851
583
  }
1852
- 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);
1853
- if (ref) {
1854
- components.headers.set(schema, {
1855
- type: "complete",
1856
- headerObject: baseHeader,
1857
- ref
1858
- });
1859
- return {
1860
- $ref: createComponentHeaderRef(ref)
1861
- };
584
+ const stringified = stringifiedComponents.get(key);
585
+ const component = registry.schemas.ids.get(key);
586
+ if (component && stringified !== JSON.stringify(component)) {
587
+ dependencies.pure = false;
588
+ return false;
1862
589
  }
1863
- return baseHeader;
1864
- };
1865
- const createBaseHeader = (schema, components, documentOptions) => {
1866
- var _a, _b;
1867
- const { ref, ...rest } = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.header) ?? {};
1868
- const state = {
1869
- components,
1870
- type: "output",
1871
- path: [],
1872
- visited: /* @__PURE__ */ new Set(),
1873
- documentOptions
590
+ visited.add(key);
591
+ const result = [...dependencies.dependencies].every(
592
+ (dep) => isDependencyPure(
593
+ componentDependencies,
594
+ stringifiedComponents,
595
+ registry,
596
+ dep,
597
+ new Set(visited)
598
+ )
599
+ );
600
+ dependencies.pure = result;
601
+ return result;
602
+ };
603
+ const deleteZodOpenApiMeta = (jsonSchema) => {
604
+ delete jsonSchema.param;
605
+ delete jsonSchema.header;
606
+ delete jsonSchema.unusedIO;
607
+ delete jsonSchema.override;
608
+ delete jsonSchema.outputId;
609
+ };
610
+ const createSchema = (schema, ctx = {
611
+ registry: createRegistry(),
612
+ io: "output",
613
+ opts: {}
614
+ }) => {
615
+ ctx.registry ?? (ctx.registry = createRegistry());
616
+ ctx.opts ?? (ctx.opts = {});
617
+ ctx.io ?? (ctx.io = "output");
618
+ const registrySchemas = Object.fromEntries(
619
+ ctx.registry.schemas[ctx.io].schemas
620
+ );
621
+ const schemas = {
622
+ zodOpenApiCreateSchema: { zodType: schema }
1874
623
  };
1875
- const schemaObject = createSchema(schema, state, ["header"]);
1876
- const optionalResult = schema.safeParse(void 0);
1877
- const required = !optionalResult.success || optionalResult !== void 0;
624
+ Object.assign(schemas, registrySchemas);
625
+ const jsonSchemas = createSchemas(schemas, {
626
+ registry: ctx.registry,
627
+ io: ctx.io,
628
+ opts: ctx.opts
629
+ });
1878
630
  return {
1879
- ...rest,
1880
- ...schema && { schema: schemaObject },
1881
- ...required && { required }
631
+ schema: jsonSchemas.schemas.zodOpenApiCreateSchema,
632
+ components: jsonSchemas.components
1882
633
  };
1883
634
  };
1884
- const createComponentHeaderRef = (ref) => `#/components/headers/${ref}`;
1885
- const createResponse = (responseObject, components, subpath, documentOptions) => {
1886
- if ("$ref" in responseObject) {
1887
- return responseObject;
1888
- }
1889
- const component = components.responses.get(responseObject);
1890
- if (component && component.type === "complete") {
1891
- return { $ref: createComponentResponseRef(component.ref) };
1892
- }
1893
- const { content, headers, ref, ...rest } = responseObject;
1894
- const maybeHeaders = createResponseHeaders(
1895
- headers,
1896
- components,
1897
- documentOptions
1898
- );
1899
- const response = {
1900
- ...rest,
1901
- ...maybeHeaders && { headers: maybeHeaders },
1902
- ...content && {
1903
- content: createContent(
1904
- content,
1905
- components,
1906
- "output",
1907
- [...subpath, "content"],
1908
- documentOptions
1909
- )
635
+ const createSchemas = (schemas, {
636
+ registry,
637
+ io,
638
+ opts
639
+ }) => {
640
+ var _a, _b, _c, _d, _e, _f;
641
+ const schemaRegistry = v4.registry();
642
+ const globalsInSchemas = /* @__PURE__ */ new Map();
643
+ for (const [name, { zodType }] of Object.entries(schemas)) {
644
+ const id = (_a = v4.globalRegistry.get(zodType)) == null ? void 0 : _a.id;
645
+ if (id) {
646
+ globalsInSchemas.set(name, id);
647
+ }
648
+ schemaRegistry.add(zodType, { id: name });
649
+ }
650
+ const outputIds = /* @__PURE__ */ new Map();
651
+ const jsonSchema = v4.toJSONSchema(schemaRegistry, {
652
+ override(ctx) {
653
+ const meta = ctx.zodSchema.meta();
654
+ if ((meta == null ? void 0 : meta.outputId) && (meta == null ? void 0 : meta.id)) {
655
+ outputIds.set(meta.id, meta.outputId);
656
+ }
657
+ if (ctx.jsonSchema.$ref) {
658
+ return;
659
+ }
660
+ const enrichedContext = { ...ctx, io };
661
+ override(enrichedContext);
662
+ if (typeof opts.override === "function") {
663
+ opts.override(enrichedContext);
664
+ }
665
+ if (typeof (meta == null ? void 0 : meta.override) === "function") {
666
+ meta.override(enrichedContext);
667
+ delete ctx.jsonSchema.override;
668
+ }
669
+ if (typeof (meta == null ? void 0 : meta.override) === "object" && meta.override !== null) {
670
+ Object.assign(ctx.jsonSchema, meta.override);
671
+ delete ctx.jsonSchema.override;
672
+ }
673
+ delete ctx.jsonSchema.$schema;
674
+ delete ctx.jsonSchema.id;
675
+ deleteZodOpenApiMeta(ctx.jsonSchema);
676
+ validate(enrichedContext, opts);
677
+ },
678
+ io,
679
+ unrepresentable: "any",
680
+ uri: (id) => `#/components/schemas/${id}`
681
+ });
682
+ const sharedDefs = ((_b = jsonSchema.schemas.__shared) == null ? void 0 : _b.$defs) ?? {};
683
+ (_c = jsonSchema.schemas).__shared ?? (_c.__shared = { $defs: sharedDefs });
684
+ const componentsToReplace = /* @__PURE__ */ new Map();
685
+ for (const [key, value] of Object.entries(sharedDefs)) {
686
+ if (/^schema\d+$/.exec(key)) {
687
+ const componentName = `__schema${registry.schemas.dynamicSchemaCount++}`;
688
+ componentsToReplace.set(`__shared#/$defs/${key}`, componentName);
689
+ delete sharedDefs[key];
690
+ sharedDefs[componentName] = value;
691
+ continue;
1910
692
  }
1911
- };
1912
- const responseRef = ref ?? (component == null ? void 0 : component.ref);
1913
- if (responseRef) {
1914
- components.responses.set(responseObject, {
1915
- responseObject: response,
1916
- ref: responseRef,
1917
- type: "complete"
1918
- });
1919
- return {
1920
- $ref: createComponentResponseRef(responseRef)
1921
- };
693
+ componentsToReplace.set(`__shared#/$defs/${key}`, key);
1922
694
  }
1923
- return response;
1924
- };
1925
- const createResponses = (responsesObject, components, subpath, documentOptions) => Object.entries(responsesObject).reduce(
1926
- (acc, [statusCode, responseObject]) => {
1927
- if (isISpecificationExtension(statusCode)) {
1928
- acc[statusCode] = responseObject;
1929
- return acc;
695
+ for (const value of Object.values(jsonSchema.schemas)) {
696
+ delete value.$schema;
697
+ delete value.id;
698
+ }
699
+ const dynamicComponent = /* @__PURE__ */ new Map();
700
+ const patched = JSON.stringify(jsonSchema).replace(
701
+ /"#\/components\/schemas\/([^"]+)"/g,
702
+ (_, match) => {
703
+ const replacement = componentsToReplace.get(match);
704
+ if (replacement) {
705
+ return `"#/components/schemas/${replacement}"`;
706
+ }
707
+ const component = registry.schemas.ids.get(match);
708
+ if (component) {
709
+ return `"#/components/schemas/${match}`;
710
+ }
711
+ const globalInSchema = globalsInSchemas.get(match);
712
+ if (globalInSchema) {
713
+ componentsToReplace.set(match, globalInSchema);
714
+ dynamicComponent.set(match, globalInSchema);
715
+ return `"#/components/schemas/${globalInSchema}"`;
716
+ }
717
+ const manualSchema = registry.schemas.manual.get(match);
718
+ if (manualSchema) {
719
+ componentsToReplace.set(match, manualSchema.key);
720
+ dynamicComponent.set(match, manualSchema.key);
721
+ manualSchema.io[io].used = true;
722
+ return `"#/components/schemas/${manualSchema.key}"`;
723
+ }
724
+ const componentName = `__schema${registry.schemas.dynamicSchemaCount++}`;
725
+ componentsToReplace.set(match, componentName);
726
+ dynamicComponent.set(match, componentName);
727
+ return `"#/components/schemas/${componentName}"`;
728
+ }
729
+ );
730
+ const patchedJsonSchema = JSON.parse(patched);
731
+ const components = ((_d = patchedJsonSchema.schemas.__shared) == null ? void 0 : _d.$defs) ?? {};
732
+ (_e = patchedJsonSchema.schemas).__shared ?? (_e.__shared = { $defs: components });
733
+ for (const [key, value] of registry.schemas.manual) {
734
+ if (value.io[io].used) {
735
+ dynamicComponent.set(key, value.key);
1930
736
  }
1931
- acc[statusCode] = createResponse(
1932
- responseObject,
1933
- components,
1934
- [...subpath, statusCode],
1935
- documentOptions
1936
- );
1937
- return acc;
1938
- },
1939
- {}
1940
- );
1941
- const createRequestBody = (requestBodyObject, components, subpath, documentOptions) => {
1942
- if (!requestBodyObject) {
1943
- return void 0;
1944
737
  }
1945
- const component = components.requestBodies.get(requestBodyObject);
1946
- if (component && component.type === "complete") {
1947
- return {
1948
- $ref: createComponentRequestBodyRef(component.ref)
738
+ for (const [key, value] of globalsInSchemas) {
739
+ dynamicComponent.set(key, value);
740
+ }
741
+ for (const [key, value] of dynamicComponent) {
742
+ const component = patchedJsonSchema.schemas[key];
743
+ patchedJsonSchema.schemas[key] = {
744
+ $ref: `#/components/schemas/${value}`
1949
745
  };
746
+ components[value] = component;
1950
747
  }
1951
- const ref = requestBodyObject.ref ?? (component == null ? void 0 : component.ref);
1952
- const requestBody = {
1953
- ...requestBodyObject,
1954
- content: createContent(
1955
- requestBodyObject.content,
1956
- components,
1957
- "input",
1958
- [...subpath, "content"],
1959
- documentOptions
1960
- )
1961
- };
1962
- if (ref) {
1963
- components.requestBodies.set(requestBodyObject, {
1964
- type: "complete",
1965
- ref,
1966
- requestBodyObject: requestBody
1967
- });
748
+ const renamedComponents = renameComponents(components, outputIds, {
749
+ registry
750
+ });
751
+ if (renamedComponents.size === 0) {
752
+ delete patchedJsonSchema.schemas.__shared;
1968
753
  return {
1969
- $ref: createComponentRequestBodyRef(ref)
754
+ schemas: patchedJsonSchema.schemas,
755
+ components
1970
756
  };
1971
757
  }
1972
- return requestBody;
1973
- };
1974
- const createOperation = (operationObject, components, subpath, documentOptions) => {
1975
- const { parameters, requestParams, requestBody, responses, ...rest } = operationObject;
1976
- const maybeParameters = createParametersObject(
1977
- parameters,
1978
- requestParams,
1979
- components,
1980
- [...subpath, "parameters"],
1981
- documentOptions
1982
- );
1983
- const maybeRequestBody = createRequestBody(
1984
- operationObject.requestBody,
1985
- components,
1986
- [...subpath, "request body"],
1987
- documentOptions
1988
- );
1989
- const maybeResponses = createResponses(
1990
- operationObject.responses,
1991
- components,
1992
- [...subpath, "responses"],
1993
- documentOptions
1994
- );
1995
- const maybeCallbacks = createCallbacks(
1996
- operationObject.callbacks,
1997
- components,
1998
- [...subpath, "callbacks"],
1999
- documentOptions
758
+ const renamedStringified = JSON.stringify(patchedJsonSchema).replace(
759
+ /"#\/components\/schemas\/([^"]+)"/g,
760
+ (_, match) => {
761
+ const newName = renamedComponents.get(match);
762
+ if (newName) {
763
+ return `"#/components/schemas/${newName}"`;
764
+ }
765
+ return `"#/components/schemas/${match}"`;
766
+ }
2000
767
  );
768
+ const renamedJsonSchema = JSON.parse(renamedStringified);
769
+ const renamedJsonSchemaComponents = ((_f = renamedJsonSchema.schemas.__shared) == null ? void 0 : _f.$defs) ?? {};
770
+ delete renamedJsonSchema.schemas.__shared;
2001
771
  return {
2002
- ...rest,
2003
- ...maybeParameters && { parameters: maybeParameters },
2004
- ...maybeRequestBody && { requestBody: maybeRequestBody },
2005
- ...maybeResponses && { responses: maybeResponses },
2006
- ...maybeCallbacks && { callbacks: maybeCallbacks }
772
+ schemas: renamedJsonSchema.schemas,
773
+ components: renamedJsonSchemaComponents
2007
774
  };
2008
775
  };
2009
- const createPathItem = (pathObject, components, path, documentOptions) => Object.entries(pathObject).reduce(
2010
- (acc, [key, value]) => {
2011
- if (!value) {
2012
- return acc;
2013
- }
2014
- if (key === "get" || key === "put" || key === "post" || key === "delete" || key === "options" || key === "head" || key === "patch" || key === "trace") {
2015
- acc[key] = createOperation(
2016
- value,
2017
- components,
2018
- [...path, key],
2019
- documentOptions
2020
- );
2021
- return acc;
2022
- }
2023
- acc[key] = value;
2024
- return acc;
2025
- },
2026
- {}
2027
- );
2028
- const createPaths = (pathsObject, components, documentOptions) => {
2029
- if (!pathsObject) {
2030
- return void 0;
2031
- }
2032
- return Object.entries(pathsObject).reduce(
2033
- (acc, [path, pathItemObject]) => {
2034
- if (isISpecificationExtension(path)) {
2035
- acc[path] = pathItemObject;
2036
- return acc;
776
+ const createRegistry = (components) => {
777
+ const registry = {
778
+ schemas: {
779
+ dynamicSchemaCount: 0,
780
+ input: {
781
+ seen: /* @__PURE__ */ new WeakMap(),
782
+ schemas: /* @__PURE__ */ new Map()
783
+ },
784
+ output: {
785
+ seen: /* @__PURE__ */ new WeakMap(),
786
+ schemas: /* @__PURE__ */ new Map()
787
+ },
788
+ ids: /* @__PURE__ */ new Map(),
789
+ manual: /* @__PURE__ */ new Map(),
790
+ setSchema: (key, schema, io) => {
791
+ const seenSchema = registry.schemas[io].seen.get(schema);
792
+ if (seenSchema) {
793
+ if (seenSchema.type === "manual") {
794
+ const manualSchema = registry.schemas.manual.get(seenSchema.id);
795
+ if (!manualSchema) {
796
+ throw new Error(
797
+ `Manual schema "${key}" not found in registry for ${io} IO.`
798
+ );
799
+ }
800
+ manualSchema.io[io].used = true;
801
+ }
802
+ return seenSchema.schemaObject;
803
+ }
804
+ const schemaObject = {};
805
+ registry.schemas[io].schemas.set(key, {
806
+ schemaObject,
807
+ zodType: schema
808
+ });
809
+ registry.schemas[io].seen.set(schema, { type: "schema", schemaObject });
810
+ return schemaObject;
2037
811
  }
2038
- acc[path] = createPathItem(
2039
- pathItemObject,
2040
- components,
2041
- [path],
2042
- documentOptions
2043
- );
2044
- return acc;
2045
812
  },
2046
- {}
2047
- );
2048
- };
2049
- const createCallback = (callbackObject, components, subpath, documentOptions) => {
2050
- const { ref, ...callbacks } = callbackObject;
2051
- const callback = Object.entries(
2052
- callbacks
2053
- ).reduce((acc, [callbackName, pathItemObject]) => {
2054
- if (isISpecificationExtension(callbackName)) {
2055
- acc[callbackName] = pathItemObject;
2056
- return acc;
2057
- }
2058
- acc[callbackName] = createPathItem(
2059
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
2060
- pathItemObject,
2061
- components,
2062
- [...subpath, callbackName],
2063
- documentOptions
2064
- );
2065
- return acc;
2066
- }, {});
2067
- if (ref) {
2068
- components.callbacks.set(callbackObject, {
2069
- type: "complete",
2070
- ref,
2071
- callbackObject: callback
2072
- });
2073
- return {
2074
- $ref: createComponentCallbackRef(ref)
2075
- };
2076
- }
2077
- return callback;
2078
- };
2079
- const createCallbacks = (callbacksObject, components, subpath, documentOptions) => {
2080
- if (!callbacksObject) {
2081
- return void 0;
2082
- }
2083
- return Object.entries(callbacksObject).reduce(
2084
- (acc, [callbackName, callbackObject]) => {
2085
- if (isISpecificationExtension(callbackName)) {
2086
- acc[callbackName] = callbackObject;
2087
- return acc;
2088
- }
2089
- acc[callbackName] = createCallback(
2090
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
2091
- callbackObject,
2092
- components,
2093
- [...subpath, callbackName],
2094
- documentOptions
2095
- );
2096
- return acc;
813
+ headers: {
814
+ ids: /* @__PURE__ */ new Map(),
815
+ seen: /* @__PURE__ */ new WeakMap()
2097
816
  },
2098
- {}
2099
- );
2100
- };
2101
- const getDefaultComponents = (componentsObject, openapi = "3.1.0") => {
2102
- const defaultComponents = {
2103
- schemas: /* @__PURE__ */ new Map(),
2104
- parameters: /* @__PURE__ */ new Map(),
2105
- headers: /* @__PURE__ */ new Map(),
2106
- requestBodies: /* @__PURE__ */ new Map(),
2107
- responses: /* @__PURE__ */ new Map(),
2108
- callbacks: /* @__PURE__ */ new Map(),
2109
- openapi
817
+ requestBodies: {
818
+ ids: /* @__PURE__ */ new Map(),
819
+ seen: /* @__PURE__ */ new WeakMap()
820
+ },
821
+ responses: {
822
+ ids: /* @__PURE__ */ new Map(),
823
+ seen: /* @__PURE__ */ new WeakMap()
824
+ },
825
+ parameters: {
826
+ ids: /* @__PURE__ */ new Map(),
827
+ seen: /* @__PURE__ */ new WeakMap()
828
+ },
829
+ callbacks: {
830
+ ids: /* @__PURE__ */ new Map(),
831
+ seen: /* @__PURE__ */ new WeakMap()
832
+ },
833
+ pathItems: {
834
+ ids: /* @__PURE__ */ new Map(),
835
+ seen: /* @__PURE__ */ new WeakMap()
836
+ }
2110
837
  };
2111
- if (!componentsObject) {
2112
- return defaultComponents;
2113
- }
2114
- getSchemas(componentsObject.schemas, defaultComponents);
2115
- getParameters(componentsObject.parameters, defaultComponents);
2116
- getRequestBodies(componentsObject.requestBodies, defaultComponents);
2117
- getHeaders(componentsObject.headers, defaultComponents);
2118
- getResponses(componentsObject.responses, defaultComponents);
2119
- getCallbacks(componentsObject.callbacks, defaultComponents);
2120
- return defaultComponents;
2121
- };
2122
- const getSchemas = (schemas, components) => {
838
+ registerSchemas(components == null ? void 0 : components.schemas, registry);
839
+ registerParameters(components == null ? void 0 : components.parameters, registry);
840
+ registerHeaders(components == null ? void 0 : components.headers, registry);
841
+ registerResponses(components == null ? void 0 : components.responses, registry);
842
+ registerPathItems(components == null ? void 0 : components.pathItems, registry);
843
+ registerRequestBodies(components == null ? void 0 : components.requestBodies, registry);
844
+ registerCallbacks(components == null ? void 0 : components.callbacks, registry);
845
+ return registry;
846
+ };
847
+ const registerSchemas = (schemas, registry) => {
2123
848
  if (!schemas) {
2124
849
  return;
2125
850
  }
2126
- Object.entries(schemas).forEach(([key, schema]) => {
2127
- var _a, _b;
851
+ for (const [key, schema] of Object.entries(schemas)) {
852
+ if (registry.schemas.ids.has(key)) {
853
+ throw new Error(`Schema "${key}" is already registered`);
854
+ }
2128
855
  if (isAnyZodType(schema)) {
2129
- if (components.schemas.has(schema)) {
2130
- throw new Error(
2131
- `Schema ${JSON.stringify(schema._def)} is already registered`
2132
- );
2133
- }
2134
- const ref = ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.ref) ?? key;
2135
- components.schemas.set(schema, {
856
+ const inputSchemaObject = {};
857
+ const outputSchemaObject = {};
858
+ const identifier = `components > schemas > ${key}`;
859
+ registry.schemas.input.schemas.set(identifier, {
860
+ zodType: schema,
861
+ schemaObject: inputSchemaObject
862
+ });
863
+ registry.schemas.input.seen.set(schema, {
864
+ type: "manual",
865
+ schemaObject: inputSchemaObject,
866
+ id: identifier
867
+ });
868
+ registry.schemas.output.schemas.set(identifier, {
869
+ zodType: schema,
870
+ schemaObject: outputSchemaObject
871
+ });
872
+ registry.schemas.output.seen.set(schema, {
2136
873
  type: "manual",
2137
- ref
874
+ schemaObject: outputSchemaObject,
875
+ id: identifier
2138
876
  });
877
+ registry.schemas.manual.set(identifier, {
878
+ key,
879
+ io: {
880
+ input: {
881
+ schemaObject: inputSchemaObject
882
+ },
883
+ output: {
884
+ schemaObject: outputSchemaObject
885
+ }
886
+ },
887
+ zodType: schema
888
+ });
889
+ continue;
2139
890
  }
2140
- });
891
+ registry.schemas.ids.set(key, schema);
892
+ }
2141
893
  };
2142
- const getParameters = (parameters, components) => {
894
+ const registerParameters = (parameters, registry) => {
2143
895
  if (!parameters) {
2144
896
  return;
2145
897
  }
2146
- Object.entries(parameters).forEach(([key, schema]) => {
2147
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
898
+ for (const [key, schema] of Object.entries(parameters)) {
899
+ if (registry.parameters.ids.has(key)) {
900
+ throw new Error(`Parameter "${key}" is already registered`);
901
+ }
2148
902
  if (isAnyZodType(schema)) {
2149
- if (components.parameters.has(schema)) {
2150
- throw new Error(
2151
- `Parameter ${JSON.stringify(schema._def)} is already registered`
2152
- );
2153
- }
2154
- const ref = ((_c = (_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.ref) ?? key;
2155
- const name = (_f = (_e = (_d = schema._def.zodOpenApi) == null ? void 0 : _d.openapi) == null ? void 0 : _e.param) == null ? void 0 : _f.name;
2156
- const location = (_i = (_h = (_g = schema._def.zodOpenApi) == null ? void 0 : _g.openapi) == null ? void 0 : _h.param) == null ? void 0 : _i.in;
2157
- if (!name || !location) {
2158
- throw new Error("`name` or `in` missing in .openapi()");
2159
- }
2160
- components.parameters.set(schema, {
2161
- type: "manual",
2162
- ref,
2163
- in: location,
2164
- name
2165
- });
903
+ const path = ["components", "parameters", key];
904
+ const paramObject = createParameter(
905
+ schema,
906
+ void 0,
907
+ {
908
+ registry,
909
+ io: "input"
910
+ },
911
+ path
912
+ );
913
+ registry.parameters.ids.set(key, paramObject);
914
+ registry.parameters.seen.set(schema, paramObject);
915
+ continue;
2166
916
  }
2167
- });
917
+ registry.parameters.ids.set(key, schema);
918
+ }
2168
919
  };
2169
- const getHeaders = (responseHeaders, components) => {
2170
- if (!responseHeaders) {
920
+ const registerHeaders = (headers, registry) => {
921
+ if (!headers) {
2171
922
  return;
2172
923
  }
2173
- Object.entries(responseHeaders).forEach(([key, schema]) => {
2174
- var _a, _b, _c;
924
+ for (const [key, schema] of Object.entries(headers)) {
925
+ if (registry.headers.ids.has(key)) {
926
+ throw new Error(`Header "${key}" is already registered`);
927
+ }
2175
928
  if (isAnyZodType(schema)) {
2176
- if (components.parameters.has(schema)) {
2177
- throw new Error(
2178
- `Header ${JSON.stringify(schema._def)} is already registered`
2179
- );
2180
- }
2181
- const ref = ((_c = (_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.param) == null ? void 0 : _c.ref) ?? key;
2182
- components.headers.set(schema, {
2183
- type: "manual",
2184
- ref
2185
- });
929
+ const path = ["components", "headers", key];
930
+ const headerObject = createHeader(
931
+ schema,
932
+ {
933
+ registry,
934
+ io: "output"
935
+ },
936
+ path
937
+ );
938
+ registry.headers.ids.set(key, headerObject);
939
+ registry.headers.seen.set(schema, headerObject);
940
+ continue;
2186
941
  }
2187
- });
942
+ registry.headers.ids.set(key, schema);
943
+ }
2188
944
  };
2189
- const getResponses = (responses, components) => {
945
+ const registerResponses = (responses, registry) => {
2190
946
  if (!responses) {
2191
947
  return;
2192
948
  }
2193
- Object.entries(responses).forEach(([key, responseObject]) => {
2194
- if (components.responses.has(responseObject)) {
2195
- throw new Error(
2196
- `Header ${JSON.stringify(responseObject)} is already registered`
2197
- );
949
+ for (const [key, schema] of Object.entries(responses)) {
950
+ const path = ["components", "responses", key];
951
+ if (registry.responses.ids.has(key)) {
952
+ throw new Error(`Response "${key}" is already registered`);
2198
953
  }
2199
- const ref = (responseObject == null ? void 0 : responseObject.ref) ?? key;
2200
- components.responses.set(responseObject, {
2201
- type: "manual",
2202
- ref
2203
- });
2204
- });
954
+ const responseObject = createResponse(
955
+ schema,
956
+ {
957
+ registry,
958
+ io: "output"
959
+ },
960
+ path
961
+ );
962
+ registry.responses.ids.set(key, responseObject);
963
+ registry.responses.seen.set(schema, responseObject);
964
+ }
2205
965
  };
2206
- const getRequestBodies = (requestBodies, components) => {
966
+ const registerRequestBodies = (requestBodies, registry) => {
2207
967
  if (!requestBodies) {
2208
968
  return;
2209
969
  }
2210
- Object.entries(requestBodies).forEach(([key, requestBody]) => {
2211
- if (components.requestBodies.has(requestBody)) {
2212
- throw new Error(
2213
- `Header ${JSON.stringify(requestBody)} is already registered`
970
+ for (const [key, schema] of Object.entries(requestBodies)) {
971
+ if (registry.requestBodies.ids.has(key)) {
972
+ throw new Error(`RequestBody "${key}" is already registered`);
973
+ }
974
+ if (isAnyZodType(schema)) {
975
+ const path = ["components", "requestBodies", key];
976
+ const requestBodyObject = createRequestBody(
977
+ schema,
978
+ {
979
+ registry,
980
+ io: "input"
981
+ },
982
+ path
2214
983
  );
984
+ registry.requestBodies.ids.set(key, requestBodyObject);
985
+ continue;
2215
986
  }
2216
- const ref = (requestBody == null ? void 0 : requestBody.ref) ?? key;
2217
- components.requestBodies.set(requestBody, {
2218
- type: "manual",
2219
- ref
2220
- });
2221
- });
987
+ registry.requestBodies.ids.set(key, schema);
988
+ }
2222
989
  };
2223
- const getCallbacks = (callbacks, components) => {
990
+ const registerCallbacks = (callbacks, registry) => {
2224
991
  if (!callbacks) {
2225
992
  return;
2226
993
  }
2227
- Object.entries(callbacks).forEach(([key, callback]) => {
2228
- if (components.callbacks.has(callback)) {
2229
- throw new Error(
2230
- `Callback ${JSON.stringify(callback)} is already registered`
2231
- );
994
+ for (const [key, schema] of Object.entries(callbacks)) {
995
+ if (registry.callbacks.ids.has(key)) {
996
+ throw new Error(`Callback "${key}" is already registered`);
2232
997
  }
2233
- const ref = (callback == null ? void 0 : callback.ref) ?? key;
2234
- components.callbacks.set(callback, {
2235
- type: "manual",
2236
- ref
2237
- });
2238
- });
2239
- };
2240
- const createComponentSchemaRef = (schemaRef, componentPath) => `${componentPath ?? "#/components/schemas/"}${schemaRef}`;
2241
- const createComponentResponseRef = (responseRef) => `#/components/responses/${responseRef}`;
2242
- const createComponentRequestBodyRef = (requestBodyRef) => `#/components/requestBodies/${requestBodyRef}`;
2243
- const createComponentCallbackRef = (callbackRef) => `#/components/callbacks/${callbackRef}`;
2244
- const createComponents = (componentsObject, components, documentOptions) => {
2245
- const combinedSchemas = createSchemaComponents(
2246
- componentsObject,
2247
- components,
2248
- documentOptions
2249
- );
2250
- const combinedParameters = createParamComponents(
2251
- componentsObject,
2252
- components,
2253
- documentOptions
2254
- );
2255
- const combinedHeaders = createHeaderComponents(
2256
- componentsObject,
2257
- components,
2258
- documentOptions
2259
- );
2260
- const combinedResponses = createResponseComponents(
2261
- components,
2262
- documentOptions
2263
- );
2264
- const combinedRequestBodies = createRequestBodiesComponents(
2265
- components,
2266
- documentOptions
2267
- );
2268
- const combinedCallbacks = createCallbackComponents(
2269
- components,
2270
- documentOptions
2271
- );
2272
- const { schemas, parameters, headers, responses, requestBodies, ...rest } = componentsObject;
2273
- const finalComponents = {
2274
- ...rest,
2275
- ...combinedSchemas && { schemas: combinedSchemas },
2276
- ...combinedParameters && { parameters: combinedParameters },
2277
- ...combinedRequestBodies && { requestBodies: combinedRequestBodies },
2278
- ...combinedHeaders && { headers: combinedHeaders },
2279
- ...combinedResponses && { responses: combinedResponses },
2280
- ...combinedCallbacks && { callbacks: combinedCallbacks }
2281
- };
2282
- return Object.keys(finalComponents).length ? finalComponents : void 0;
998
+ const path = ["components", "callbacks", key];
999
+ const callbackObject = createCallback(schema, registry, path);
1000
+ registry.callbacks.ids.set(key, callbackObject);
1001
+ registry.callbacks.seen.set(schema, callbackObject);
1002
+ }
2283
1003
  };
2284
- const createSchemaComponents = (componentsObject, components, documentOptions) => {
2285
- Array.from(components.schemas).forEach(([schema, { type }], index) => {
2286
- var _a, _b;
2287
- if (type === "manual") {
2288
- const state = {
2289
- components,
2290
- type: ((_b = (_a = schema._def.zodOpenApi) == null ? void 0 : _a.openapi) == null ? void 0 : _b.refType) ?? "output",
2291
- path: [],
2292
- visited: /* @__PURE__ */ new Set(),
2293
- documentOptions
2294
- };
2295
- createSchema(schema, state, [`component schema index ${index}`]);
2296
- }
2297
- });
2298
- const customComponents = Object.entries(
2299
- componentsObject.schemas ?? {}
2300
- ).reduce(
2301
- (acc, [key, value]) => {
2302
- if (isAnyZodType(value)) {
2303
- return acc;
2304
- }
2305
- if (acc[key]) {
2306
- throw new Error(`Schema "${key}" is already registered`);
2307
- }
2308
- acc[key] = value;
2309
- return acc;
2310
- },
2311
- {}
2312
- );
2313
- const finalComponents = Array.from(components.schemas).reduce((acc, [_zodType, component]) => {
2314
- if (component.type === "complete") {
2315
- if (acc[component.ref]) {
2316
- throw new Error(`Schema "${component.ref}" is already registered`);
2317
- }
2318
- acc[component.ref] = component.schemaObject;
1004
+ const registerPathItems = (pathItems, registry) => {
1005
+ if (!pathItems) {
1006
+ return;
1007
+ }
1008
+ for (const [key, schema] of Object.entries(pathItems)) {
1009
+ if (registry.pathItems.ids.has(key)) {
1010
+ throw new Error(`PathItem "${key}" is already registered`);
2319
1011
  }
2320
- return acc;
2321
- }, customComponents);
2322
- return Object.keys(finalComponents).length ? finalComponents : void 0;
1012
+ const path = ["components", "pathItems", key];
1013
+ const pathItemObject = createPathItem(schema, registry, path);
1014
+ registry.pathItems.ids.set(key, pathItemObject);
1015
+ registry.pathItems.seen.set(schema, pathItemObject);
1016
+ continue;
1017
+ }
2323
1018
  };
2324
- const createParamComponents = (componentsObject, components, documentOptions) => {
2325
- Array.from(components.parameters).forEach(([schema, component], index) => {
2326
- if (component.type === "manual") {
2327
- createParamOrRef(
2328
- schema,
2329
- components,
2330
- [`component parameter index ${index}`],
2331
- component.in,
2332
- component.ref,
2333
- documentOptions
2334
- );
2335
- }
2336
- });
2337
- const customComponents = Object.entries(
2338
- componentsObject.parameters ?? {}
2339
- ).reduce(
2340
- (acc, [key, value]) => {
2341
- if (!isAnyZodType(value)) {
2342
- if (acc[key]) {
2343
- throw new Error(`Parameter "${key}" is already registered`);
2344
- }
2345
- acc[key] = value;
2346
- }
2347
- return acc;
2348
- },
2349
- {}
1019
+ const createIOSchemas = (ctx) => {
1020
+ const { schemas, components } = createSchemas(
1021
+ Object.fromEntries(ctx.registry.schemas[ctx.io].schemas),
1022
+ ctx
2350
1023
  );
2351
- const finalComponents = Array.from(components.parameters).reduce((acc, [_zodType, component]) => {
2352
- if (component.type === "complete") {
2353
- if (acc[component.ref]) {
2354
- throw new Error(`Parameter "${component.ref}" is already registered`);
2355
- }
2356
- acc[component.ref] = component.paramObject;
2357
- }
2358
- return acc;
2359
- }, customComponents);
2360
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2361
- };
2362
- const createHeaderComponents = (componentsObject, components, documentOptions) => {
2363
- Array.from(components.headers).forEach(([schema, component]) => {
2364
- if (component.type === "manual") {
2365
- createHeaderOrRef(schema, components, documentOptions);
2366
- }
2367
- });
2368
- const headers = componentsObject.headers ?? {};
2369
- const customComponents = Object.entries(headers).reduce((acc, [key, value]) => {
2370
- if (!isAnyZodType(value)) {
2371
- if (acc[key]) {
2372
- throw new Error(`Header Ref "${key}" is already registered`);
2373
- }
2374
- acc[key] = value;
2375
- }
2376
- return acc;
2377
- }, {});
2378
- const finalComponents = Array.from(components.headers).reduce((acc, [_zodType, component]) => {
2379
- if (component.type === "complete") {
2380
- if (acc[component.ref]) {
2381
- throw new Error(`Header "${component.ref}" is already registered`);
2382
- }
2383
- acc[component.ref] = component.headerObject;
2384
- }
2385
- return acc;
2386
- }, customComponents);
2387
- return Object.keys(finalComponents).length ? finalComponents : void 0;
2388
- };
2389
- const createResponseComponents = (components, documentOptions) => {
2390
- Array.from(components.responses).forEach(([schema, component], index) => {
2391
- if (component.type === "manual") {
2392
- createResponse(
2393
- schema,
2394
- components,
2395
- [`component response index ${index}`],
2396
- documentOptions
2397
- );
2398
- }
2399
- });
2400
- const finalComponents = Array.from(components.responses).reduce((acc, [_zodType, component]) => {
2401
- if (component.type === "complete") {
2402
- if (acc[component.ref]) {
2403
- throw new Error(`Response "${component.ref}" is already registered`);
2404
- }
2405
- acc[component.ref] = component.responseObject;
1024
+ for (const [key, schema] of Object.entries(components)) {
1025
+ ctx.registry.schemas.ids.set(key, schema);
1026
+ }
1027
+ for (const [key, schema] of Object.entries(schemas)) {
1028
+ const ioSchema = ctx.registry.schemas[ctx.io].schemas.get(key);
1029
+ if (ioSchema) {
1030
+ Object.assign(ioSchema.schemaObject, schema);
2406
1031
  }
2407
- return acc;
2408
- }, {});
2409
- return Object.keys(finalComponents).length ? finalComponents : void 0;
1032
+ }
2410
1033
  };
2411
- const createRequestBodiesComponents = (components, documentOptions) => {
2412
- Array.from(components.requestBodies).forEach(([schema, component], index) => {
2413
- if (component.type === "manual") {
2414
- createRequestBody(
2415
- schema,
2416
- components,
2417
- [`component request body ${index}`],
2418
- documentOptions
2419
- );
2420
- }
2421
- });
2422
- const finalComponents = Array.from(components.requestBodies).reduce((acc, [_zodType, component]) => {
2423
- if (component.type === "complete") {
2424
- if (acc[component.ref]) {
2425
- throw new Error(`RequestBody "${component.ref}" is already registered`);
2426
- }
2427
- acc[component.ref] = component.requestBodyObject;
1034
+ const createManualSchemas = (registry) => {
1035
+ var _a;
1036
+ for (const [, value] of registry.schemas.manual) {
1037
+ if (!value.io.input.used && !value.io.output.used) {
1038
+ const io = ((_a = core.globalRegistry.get(value.zodType)) == null ? void 0 : _a.unusedIO) ?? "output";
1039
+ const schema = value.io[io].schemaObject;
1040
+ registry.schemas.ids.set(value.key, schema);
2428
1041
  }
2429
- return acc;
2430
- }, {});
2431
- return Object.keys(finalComponents).length ? finalComponents : void 0;
1042
+ }
2432
1043
  };
2433
- const createCallbackComponents = (components, documentOptions) => {
2434
- Array.from(components.callbacks).forEach(([schema, component], index) => {
2435
- if (component.type === "manual") {
2436
- createCallback(
2437
- schema,
2438
- components,
2439
- [`component callback ${index}`],
2440
- documentOptions
2441
- );
2442
- }
2443
- });
2444
- const finalComponents = Array.from(components.callbacks).reduce((acc, [_zodType, component]) => {
2445
- if (component.type === "complete") {
2446
- if (acc[component.ref]) {
2447
- throw new Error(`Callback "${component.ref}" is already registered`);
2448
- }
2449
- acc[component.ref] = component.callbackObject;
2450
- }
2451
- return acc;
2452
- }, {});
2453
- return Object.keys(finalComponents).length ? finalComponents : void 0;
1044
+ const createComponents = (registry, opts) => {
1045
+ createIOSchemas({ registry, io: "input", opts });
1046
+ createIOSchemas({ registry, io: "output", opts });
1047
+ createManualSchemas(registry);
1048
+ const components = {};
1049
+ if (registry.schemas.ids.size > 0) {
1050
+ components.schemas = Object.fromEntries(registry.schemas.ids);
1051
+ }
1052
+ if (registry.headers.ids.size > 0) {
1053
+ components.headers = Object.fromEntries(registry.headers.ids);
1054
+ }
1055
+ if (registry.requestBodies.ids.size > 0) {
1056
+ components.requestBodies = Object.fromEntries(registry.requestBodies.ids);
1057
+ }
1058
+ if (registry.responses.ids.size > 0) {
1059
+ components.responses = Object.fromEntries(registry.responses.ids);
1060
+ }
1061
+ if (registry.parameters.ids.size > 0) {
1062
+ components.parameters = Object.fromEntries(registry.parameters.ids);
1063
+ }
1064
+ if (registry.callbacks.ids.size > 0) {
1065
+ components.callbacks = Object.fromEntries(registry.callbacks.ids);
1066
+ }
1067
+ if (registry.pathItems.ids.size > 0) {
1068
+ components.pathItems = Object.fromEntries(registry.pathItems.ids);
1069
+ }
1070
+ return components;
2454
1071
  };
2455
1072
  exports.createComponents = createComponents;
2456
- exports.createMediaTypeSchema = createMediaTypeSchema;
2457
- exports.createParamOrRef = createParamOrRef;
1073
+ exports.createMediaTypeObject = createMediaTypeObject;
1074
+ exports.createParameter = createParameter;
2458
1075
  exports.createPaths = createPaths;
1076
+ exports.createRegistry = createRegistry;
2459
1077
  exports.createSchema = createSchema;
2460
- exports.createSchemaComponents = createSchemaComponents;
2461
- exports.getDefaultComponents = getDefaultComponents;
2462
- exports.getZodObject = getZodObject;
1078
+ exports.unwrapZodObject = unwrapZodObject;