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