zod-openapi 5.0.0-beta.0 → 5.0.0-beta.10

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.
@@ -0,0 +1,805 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+ const zod_v4_core = __toESM(require("zod/v4/core"));
25
+ const zod_v4 = __toESM(require("zod/v4"));
26
+
27
+ //#region src/zod.ts
28
+ const isAnyZodType = (schema) => typeof schema === "object" && schema !== null && "_zod" in schema;
29
+
30
+ //#endregion
31
+ //#region src/create/content.ts
32
+ const createMediaTypeObject = (mediaTypeObject, ctx, path) => {
33
+ if (isAnyZodType(mediaTypeObject.schema)) {
34
+ const schemaObject = ctx.registry.addSchema(mediaTypeObject.schema, [...path, "schema"], { io: ctx.io });
35
+ return {
36
+ ...mediaTypeObject,
37
+ schema: schemaObject
38
+ };
39
+ }
40
+ return mediaTypeObject;
41
+ };
42
+ const createContent = (content, ctx, path) => {
43
+ const contentObject = {};
44
+ for (const [mediaType, mediaTypeObject] of Object.entries(content)) if (mediaTypeObject) contentObject[mediaType] = createMediaTypeObject(mediaTypeObject, ctx, [...path, mediaType]);
45
+ return contentObject;
46
+ };
47
+
48
+ //#endregion
49
+ //#region src/create/object.ts
50
+ const unwrapZodObject = (zodType, io, path) => {
51
+ const def = zodType._zod.def;
52
+ switch (def.type) {
53
+ case "object": return zodType;
54
+ case "lazy": return unwrapZodObject(def.getter(), io, path);
55
+ case "pipe": {
56
+ if (io === "input") return unwrapZodObject(def.in, io, path);
57
+ return unwrapZodObject(def.out, io, path);
58
+ }
59
+ }
60
+ throw new Error(`Failed to unwrap ZodObject from type: ${zodType._zod.def.type} at ${path.join(" > ")}`);
61
+ };
62
+ const isRequired = (zodType, io) => {
63
+ if (io === "input") return zodType._zod.optin === void 0;
64
+ return zodType._zod.optout === void 0;
65
+ };
66
+
67
+ //#endregion
68
+ //#region src/create/headers.ts
69
+ const createHeaders = (headers, registry$1, path) => {
70
+ if (!headers) return void 0;
71
+ if (isAnyZodType(headers)) {
72
+ const zodObject = unwrapZodObject(headers, "output", path);
73
+ const headersObject = {};
74
+ for (const [key, zodSchema] of Object.entries(zodObject._zod.def.shape)) {
75
+ const header = registry$1.addHeader(zodSchema, [...path, key]);
76
+ headersObject[key] = header;
77
+ }
78
+ return headersObject;
79
+ }
80
+ return headers;
81
+ };
82
+
83
+ //#endregion
84
+ //#region src/create/parameters.ts
85
+ const createManualParameters = (parameters, registry$1, path) => {
86
+ if (!parameters) return void 0;
87
+ const parameterObjects = [];
88
+ for (const parameter of parameters) {
89
+ if (isAnyZodType(parameter)) {
90
+ const paramObject = registry$1.addParameter(parameter, [...path, "parameters"]);
91
+ parameterObjects.push(paramObject);
92
+ continue;
93
+ }
94
+ parameterObjects.push(parameter);
95
+ }
96
+ return parameterObjects;
97
+ };
98
+ const createParameters = (requestParams, registry$1, path) => {
99
+ if (!requestParams) return void 0;
100
+ const parameterObjects = [];
101
+ for (const [location, schema] of Object.entries(requestParams ?? {})) {
102
+ const zodObject = unwrapZodObject(schema, "input", path);
103
+ for (const [name, zodSchema] of Object.entries(zodObject._zod.def.shape)) {
104
+ const paramObject = registry$1.addParameter(zodSchema, [
105
+ ...path,
106
+ location,
107
+ name
108
+ ], { location: {
109
+ in: location,
110
+ name
111
+ } });
112
+ parameterObjects.push(paramObject);
113
+ }
114
+ }
115
+ return parameterObjects;
116
+ };
117
+
118
+ //#endregion
119
+ //#region src/create/specificationExtension.ts
120
+ const isISpecificationExtension = (key) => key.startsWith("x-");
121
+
122
+ //#endregion
123
+ //#region src/create/callbacks.ts
124
+ const createCallbacks = (callbacks, registry$1, path) => {
125
+ if (!callbacks) return void 0;
126
+ const callbacksObject = {};
127
+ for (const [name, value] of Object.entries(callbacks)) {
128
+ if (isISpecificationExtension(name)) {
129
+ callbacksObject[name] = value;
130
+ continue;
131
+ }
132
+ callbacksObject[name] = registry$1.addCallback(value, [...path, name]);
133
+ }
134
+ return callbacksObject;
135
+ };
136
+
137
+ //#endregion
138
+ //#region src/create/responses.ts
139
+ const createResponses = (responses, registry$1, path) => {
140
+ if (!responses) return void 0;
141
+ const responsesObject = {};
142
+ for (const [statusCode, response] of Object.entries(responses)) {
143
+ if (!response) continue;
144
+ if (isISpecificationExtension(statusCode)) {
145
+ responsesObject[statusCode] = response;
146
+ continue;
147
+ }
148
+ if ("$ref" in response) {
149
+ responsesObject[statusCode] = response;
150
+ continue;
151
+ }
152
+ const responseObject = registry$1.addResponse(response, [...path, statusCode]);
153
+ responsesObject[statusCode] = responseObject;
154
+ }
155
+ return responsesObject;
156
+ };
157
+
158
+ //#endregion
159
+ //#region src/create/paths.ts
160
+ const createOperation = (operation, registry$1, path) => {
161
+ const { parameters, requestParams, requestBody, responses, callbacks,...rest } = operation;
162
+ const operationObject = rest;
163
+ const maybeManualParameters = createManualParameters(parameters, registry$1, [...path, "parameters"]);
164
+ const maybeRequestParams = createParameters(requestParams, registry$1, [...path, "requestParams"]);
165
+ if (maybeRequestParams || maybeManualParameters) operationObject.parameters = [...maybeRequestParams ?? [], ...maybeManualParameters ?? []];
166
+ const maybeRequestBody = requestBody && registry$1.addRequestBody(requestBody, path);
167
+ if (maybeRequestBody) operationObject.requestBody = maybeRequestBody;
168
+ const maybeResponses = createResponses(responses, registry$1, [...path, "responses"]);
169
+ if (maybeResponses) operationObject.responses = maybeResponses;
170
+ const maybeCallbacks = createCallbacks(callbacks, registry$1, [...path, "callbacks"]);
171
+ if (maybeCallbacks) operationObject.callbacks = maybeCallbacks;
172
+ return operationObject;
173
+ };
174
+ const createPaths = (paths, registry$1, path) => {
175
+ if (!paths) return void 0;
176
+ const pathsObject = {};
177
+ for (const [singlePath, pathItemObject] of Object.entries(paths)) {
178
+ if (isISpecificationExtension(singlePath)) {
179
+ pathsObject[singlePath] = pathItemObject;
180
+ continue;
181
+ }
182
+ pathsObject[singlePath] = registry$1.addPathItem(pathItemObject, [...path, singlePath]);
183
+ }
184
+ return pathsObject;
185
+ };
186
+
187
+ //#endregion
188
+ //#region src/create/schema/override.ts
189
+ const override = (ctx) => {
190
+ const def = ctx.zodSchema._zod.def;
191
+ switch (def.type) {
192
+ case "bigint": {
193
+ ctx.jsonSchema.type = "integer";
194
+ ctx.jsonSchema.format = "int64";
195
+ break;
196
+ }
197
+ case "union": {
198
+ if ("discriminator" in def && typeof def.discriminator === "string") {
199
+ ctx.jsonSchema.oneOf = ctx.jsonSchema.anyOf;
200
+ delete ctx.jsonSchema.anyOf;
201
+ ctx.jsonSchema.type = "object";
202
+ ctx.jsonSchema.discriminator = { propertyName: def.discriminator };
203
+ const mapping = {};
204
+ for (const [index, obj] of Object.entries(ctx.jsonSchema.oneOf)) {
205
+ const ref = obj.$ref;
206
+ if (!ref) return;
207
+ const discriminatorValues = def.options[Number(index)]._zod.propValues?.[def.discriminator];
208
+ if (!discriminatorValues?.size) return;
209
+ for (const value of [...discriminatorValues ?? []]) {
210
+ if (typeof value !== "string") return;
211
+ mapping[value] = ref;
212
+ }
213
+ }
214
+ ctx.jsonSchema.discriminator.mapping = mapping;
215
+ }
216
+ const meta = ctx.zodSchema.meta();
217
+ if (typeof meta?.unionOneOf === "boolean") {
218
+ if (meta.unionOneOf) {
219
+ ctx.jsonSchema.oneOf = ctx.jsonSchema.anyOf;
220
+ delete ctx.jsonSchema.anyOf;
221
+ }
222
+ delete ctx.jsonSchema.unionOneOf;
223
+ }
224
+ break;
225
+ }
226
+ case "date": {
227
+ ctx.jsonSchema.type = "string";
228
+ break;
229
+ }
230
+ case "literal": {
231
+ if (def.values.includes(void 0)) break;
232
+ break;
233
+ }
234
+ }
235
+ };
236
+ const validate = (ctx, opts) => {
237
+ if (Object.keys(ctx.jsonSchema).length) return;
238
+ const def = ctx.zodSchema._zod.def;
239
+ const allowEmptySchema = opts.allowEmptySchema?.[def.type];
240
+ if (allowEmptySchema === true || allowEmptySchema?.[ctx.io]) return;
241
+ switch (def.type) {
242
+ case "any": return;
243
+ case "unknown": return;
244
+ case "pipe": {
245
+ if (ctx.io === "output") throw new Error("Zod transform schemas are not supported in output schemas. Please use `.overwrite()` or wrap the schema in a `.pipe()`");
246
+ return;
247
+ }
248
+ case "transform": {
249
+ if (ctx.io === "output") return;
250
+ break;
251
+ }
252
+ case "literal": {
253
+ if (def.values.includes(void 0)) throw new Error("Zod literal schemas cannot include `undefined` as a value. Please use `z.undefined()` or `.optional()` instead.");
254
+ return;
255
+ }
256
+ }
257
+ throw new Error(`Zod schema of type \`${def.type}\` cannot be represented in OpenAPI. Please assign it metadata with \`.meta()\``);
258
+ };
259
+
260
+ //#endregion
261
+ //#region src/create/schema/rename.ts
262
+ const renameComponents = (components, outputIds, ctx) => {
263
+ const componentsToRename = /* @__PURE__ */ new Map();
264
+ if (ctx.io === "input") return componentsToRename;
265
+ const componentDependencies = /* @__PURE__ */ new Map();
266
+ const stringifiedComponents = /* @__PURE__ */ new Map();
267
+ for (const [key, value] of Object.entries(components)) {
268
+ const stringified = JSON.stringify(value);
269
+ const matches = stringified.matchAll(/"#\/components\/schemas\/([^"]+)"/g);
270
+ const dependencies = /* @__PURE__ */ new Set();
271
+ for (const match of matches) {
272
+ const dep = match[1];
273
+ if (dep !== key) dependencies.add(dep);
274
+ }
275
+ stringifiedComponents.set(key, stringified);
276
+ componentDependencies.set(key, { dependencies });
277
+ }
278
+ for (const [key] of stringifiedComponents) {
279
+ const registeredComponent = ctx.registry.components.schemas.ids.get(key);
280
+ if (!registeredComponent) continue;
281
+ if (isDependencyPure(componentDependencies, stringifiedComponents, ctx.registry, key)) continue;
282
+ const newName = outputIds.get(key) ?? `${key}Output`;
283
+ componentsToRename.set(key, newName);
284
+ components[newName] = components[key];
285
+ delete components[key];
286
+ continue;
287
+ }
288
+ return componentsToRename;
289
+ };
290
+ const isDependencyPure = (componentDependencies, stringifiedComponents, registry$1, key, visited = /* @__PURE__ */ new Set()) => {
291
+ if (visited.has(key)) return true;
292
+ const dependencies = componentDependencies.get(key);
293
+ if (dependencies.pure !== void 0) return dependencies.pure;
294
+ const stringified = stringifiedComponents.get(key);
295
+ const component = registry$1.components.schemas.ids.get(key);
296
+ if (component && stringified !== JSON.stringify(component)) {
297
+ dependencies.pure = false;
298
+ return false;
299
+ }
300
+ visited.add(key);
301
+ const result = [...dependencies.dependencies].every((dep) => isDependencyPure(componentDependencies, stringifiedComponents, registry$1, dep, new Set(visited)));
302
+ dependencies.pure = result;
303
+ return result;
304
+ };
305
+
306
+ //#endregion
307
+ //#region src/create/schema/schema.ts
308
+ const deleteZodOpenApiMeta = (jsonSchema) => {
309
+ delete jsonSchema.param;
310
+ delete jsonSchema.header;
311
+ delete jsonSchema.unusedIO;
312
+ delete jsonSchema.override;
313
+ delete jsonSchema.outputId;
314
+ };
315
+ const createSchema = (schema, ctx = {
316
+ registry: createRegistry(),
317
+ io: "output",
318
+ opts: {}
319
+ }) => {
320
+ ctx.registry ??= createRegistry();
321
+ ctx.opts ??= {};
322
+ ctx.io ??= "output";
323
+ const registrySchemas = Object.fromEntries(ctx.registry.components.schemas[ctx.io]);
324
+ const schemas = { zodOpenApiCreateSchema: { zodType: schema } };
325
+ Object.assign(schemas, registrySchemas);
326
+ const jsonSchemas = createSchemas(schemas, {
327
+ registry: ctx.registry,
328
+ io: ctx.io,
329
+ opts: ctx.opts
330
+ });
331
+ return {
332
+ schema: jsonSchemas.schemas.zodOpenApiCreateSchema,
333
+ components: jsonSchemas.components
334
+ };
335
+ };
336
+ const createSchemas = (schemas, ctx) => {
337
+ const entries = {};
338
+ for (const [name, { zodType }] of Object.entries(schemas)) entries[name] = zodType;
339
+ const zodRegistry = (0, zod_v4.registry)();
340
+ zodRegistry.add((0, zod_v4.object)(entries), { id: "zodOpenApiCreateSchema" });
341
+ for (const [id, { zodType }] of ctx.registry.components.schemas.manual) zodRegistry.add(zodType, { id });
342
+ const outputIds = /* @__PURE__ */ new Map();
343
+ const jsonSchema = (0, zod_v4.toJSONSchema)(zodRegistry, {
344
+ override(context) {
345
+ const meta = context.zodSchema.meta();
346
+ if (meta?.outputId && meta?.id) outputIds.set(meta.id, meta.outputId);
347
+ if (context.jsonSchema.$ref) return;
348
+ const enrichedContext = {
349
+ ...context,
350
+ io: ctx.io
351
+ };
352
+ override(enrichedContext);
353
+ if (typeof ctx.opts.override === "function") ctx.opts.override(enrichedContext);
354
+ if (typeof meta?.override === "function") {
355
+ meta.override(enrichedContext);
356
+ delete context.jsonSchema.override;
357
+ }
358
+ if (typeof meta?.override === "object" && meta.override !== null) {
359
+ Object.assign(context.jsonSchema, meta.override);
360
+ delete context.jsonSchema.override;
361
+ }
362
+ delete context.jsonSchema.$schema;
363
+ delete context.jsonSchema.id;
364
+ deleteZodOpenApiMeta(context.jsonSchema);
365
+ validate(enrichedContext, ctx.opts);
366
+ },
367
+ io: ctx.io,
368
+ unrepresentable: "any",
369
+ reused: ctx.opts.reused,
370
+ cycles: ctx.opts.cycles,
371
+ uri: (id) => id === "__shared" ? `#ZOD_OPENAPI/${id}` : `#ZOD_OPENAPI/__shared#/$defs/${id}`
372
+ });
373
+ const components = jsonSchema.schemas.__shared?.$defs ?? {};
374
+ jsonSchema.schemas.__shared ??= { $defs: components };
375
+ const dynamicComponents = /* @__PURE__ */ new Map();
376
+ for (const [key, value] of Object.entries(components)) if (/^schema\d+$/.test(key)) {
377
+ const newName = `__schema${ctx.registry.components.schemas.dynamicSchemaCount++}`;
378
+ dynamicComponents.set(key, `"#/components/schemas/${newName}"`);
379
+ if (newName !== key) {
380
+ components[newName] = value;
381
+ delete components[key];
382
+ }
383
+ }
384
+ const manualUsed = {};
385
+ const parsedJsonSchema = JSON.parse(JSON.stringify(jsonSchema).replace(/"#ZOD_OPENAPI\/__shared#\/\$defs\/([^"]+)"/g, (_, match) => {
386
+ const dynamic = dynamicComponents.get(match);
387
+ if (dynamic) return dynamic;
388
+ const manualComponent = ctx.registry.components.schemas.manual.get(match);
389
+ if (manualComponent) manualUsed[match] = true;
390
+ return `"#/components/schemas/${match}"`;
391
+ }));
392
+ const parsedComponents = parsedJsonSchema.schemas.__shared?.$defs ?? {};
393
+ parsedJsonSchema.schemas.__shared ??= { $defs: parsedComponents };
394
+ for (const [key] of ctx.registry.components.schemas.manual) {
395
+ const manualComponent = parsedJsonSchema.schemas[key];
396
+ if (!manualComponent) continue;
397
+ delete manualComponent.$schema;
398
+ delete manualComponent.id;
399
+ if (manualUsed[key]) {
400
+ delete manualComponent.$schema;
401
+ delete manualComponent.id;
402
+ if (parsedComponents[key]) throw new Error(`Component "${key}" is already registered as a component in the registry`);
403
+ parsedComponents[key] = manualComponent;
404
+ }
405
+ }
406
+ const componentsToRename = renameComponents(parsedComponents, outputIds, ctx);
407
+ if (!componentsToRename.size) {
408
+ const parsedSchemas = parsedJsonSchema.schemas.zodOpenApiCreateSchema?.properties;
409
+ delete parsedJsonSchema.schemas.zodOpenApiCreateSchema;
410
+ delete parsedJsonSchema.schemas.__shared;
411
+ return {
412
+ schemas: parsedSchemas,
413
+ components: parsedComponents,
414
+ manual: parsedJsonSchema.schemas
415
+ };
416
+ }
417
+ const renamedJsonSchema = JSON.parse(JSON.stringify(parsedJsonSchema).replace(/"#\/components\/schemas\/([^"]+)"/g, (_, match) => {
418
+ const replacement = componentsToRename.get(match);
419
+ if (replacement) return `"#/components/schemas/${replacement}"`;
420
+ return `"#/components/schemas/${match}"`;
421
+ }));
422
+ const renamedSchemas = renamedJsonSchema.schemas.zodOpenApiCreateSchema?.properties;
423
+ const renamedComponents = renamedJsonSchema.schemas.__shared?.$defs ?? {};
424
+ delete renamedJsonSchema.schemas.zodOpenApiCreateSchema;
425
+ delete renamedJsonSchema.schemas.__shared;
426
+ return {
427
+ schemas: renamedSchemas,
428
+ components: renamedComponents,
429
+ manual: renamedJsonSchema.schemas
430
+ };
431
+ };
432
+
433
+ //#endregion
434
+ //#region src/create/components.ts
435
+ const createRegistry = (components) => {
436
+ const registry$1 = {
437
+ components: {
438
+ schemas: {
439
+ dynamicSchemaCount: 0,
440
+ input: /* @__PURE__ */ new Map(),
441
+ output: /* @__PURE__ */ new Map(),
442
+ ids: /* @__PURE__ */ new Map(),
443
+ manual: /* @__PURE__ */ new Map()
444
+ },
445
+ headers: {
446
+ ids: /* @__PURE__ */ new Map(),
447
+ seen: /* @__PURE__ */ new WeakMap()
448
+ },
449
+ requestBodies: {
450
+ ids: /* @__PURE__ */ new Map(),
451
+ seen: /* @__PURE__ */ new WeakMap()
452
+ },
453
+ responses: {
454
+ ids: /* @__PURE__ */ new Map(),
455
+ seen: /* @__PURE__ */ new WeakMap()
456
+ },
457
+ parameters: {
458
+ ids: /* @__PURE__ */ new Map(),
459
+ seen: /* @__PURE__ */ new WeakMap()
460
+ },
461
+ callbacks: {
462
+ ids: /* @__PURE__ */ new Map(),
463
+ seen: /* @__PURE__ */ new WeakMap()
464
+ },
465
+ pathItems: {
466
+ ids: /* @__PURE__ */ new Map(),
467
+ seen: /* @__PURE__ */ new WeakMap()
468
+ }
469
+ },
470
+ addSchema: (schema, path, opts) => {
471
+ const io = opts?.io ?? "output";
472
+ const schemaObject = {};
473
+ registry$1.components.schemas[io].set(path.join(" > "), {
474
+ schemaObject,
475
+ zodType: schema
476
+ });
477
+ return schemaObject;
478
+ },
479
+ addParameter: (parameter, path, opts) => {
480
+ const seenParameter = registry$1.components.parameters.seen.get(parameter);
481
+ if (seenParameter) return seenParameter;
482
+ const meta = zod_v4_core.globalRegistry.get(parameter);
483
+ const name = opts?.location?.name ?? meta?.param?.name;
484
+ const inLocation = opts?.location?.in ?? meta?.param?.in;
485
+ if (!name || !inLocation) throw new Error(`Parameter at ${path.join(" > ")} is missing \`.meta({ param: { name, in } })\` information`);
486
+ const schemaObject = registry$1.addSchema(parameter, [...path, "schema"], { io: "input" });
487
+ const { id: metaId,...rest } = meta?.param ?? {};
488
+ const parameterObject = {
489
+ ...rest,
490
+ name,
491
+ in: inLocation,
492
+ schema: schemaObject
493
+ };
494
+ if (isRequired(parameter, "input")) parameterObject.required = true;
495
+ if (!parameterObject.description && meta?.description) parameterObject.description = meta.description;
496
+ const id = metaId ?? opts?.manualId;
497
+ if (id) {
498
+ if (registry$1.components.parameters.ids.has(id)) throw new Error(`Schema "${id}" is already registered`);
499
+ const ref = { $ref: `#/components/parameters/${id}` };
500
+ registry$1.components.parameters.seen.set(parameter, ref);
501
+ registry$1.components.parameters.ids.set(id, parameterObject);
502
+ return ref;
503
+ }
504
+ registry$1.components.parameters.seen.set(parameter, parameterObject);
505
+ return parameterObject;
506
+ },
507
+ addHeader: (header, path, opts) => {
508
+ const seenHeader = registry$1.components.headers.seen.get(header);
509
+ if (seenHeader) return seenHeader;
510
+ const meta = zod_v4_core.globalRegistry.get(header);
511
+ const { id: metaId,...rest } = meta?.header ?? {};
512
+ const id = metaId ?? opts?.manualId;
513
+ const headerObject = rest;
514
+ if (isRequired(header, "output")) headerObject.required = true;
515
+ if (!headerObject.description && meta?.description) headerObject.description = meta.description;
516
+ headerObject.schema = registry$1.addSchema(header, [...path, "schema"], { io: "output" });
517
+ if (id) {
518
+ if (registry$1.components.schemas.ids.has(id)) throw new Error(`Schema "${id}" is already registered`);
519
+ const ref = { $ref: `#/components/headers/${id}` };
520
+ registry$1.components.headers.ids.set(id, headerObject);
521
+ registry$1.components.headers.seen.set(header, ref);
522
+ return ref;
523
+ }
524
+ registry$1.components.headers.seen.set(header, headerObject);
525
+ return headerObject;
526
+ },
527
+ addRequestBody: (requestBody, path, opts) => {
528
+ const seenRequestBody = registry$1.components.requestBodies.seen.get(requestBody);
529
+ if (seenRequestBody) return seenRequestBody;
530
+ const { content, id: metaId,...rest } = requestBody;
531
+ const requestBodyObject = {
532
+ ...rest,
533
+ content: createContent(content, {
534
+ registry: registry$1,
535
+ io: "input"
536
+ }, [...path, "content"])
537
+ };
538
+ const id = metaId ?? opts?.manualId;
539
+ if (id) {
540
+ if (registry$1.components.requestBodies.ids.has(id)) throw new Error(`RequestBody "${id}" is already registered`);
541
+ const ref = { $ref: `#/components/requestBodies/${id}` };
542
+ registry$1.components.requestBodies.ids.set(id, requestBodyObject);
543
+ registry$1.components.requestBodies.seen.set(requestBody, ref);
544
+ return ref;
545
+ }
546
+ registry$1.components.requestBodies.seen.set(requestBody, requestBodyObject);
547
+ return requestBodyObject;
548
+ },
549
+ addPathItem: (pathItem, path, opts) => {
550
+ const seenPathItem = registry$1.components.pathItems.seen.get(pathItem);
551
+ if (seenPathItem) return seenPathItem;
552
+ const pathItemObject = {};
553
+ const { id: metaId,...rest } = pathItem;
554
+ const id = metaId ?? opts?.manualId;
555
+ for (const [key, value] of Object.entries(rest)) {
556
+ if (isISpecificationExtension(key)) {
557
+ pathItemObject[key] = value;
558
+ continue;
559
+ }
560
+ if (key === "get" || key === "put" || key === "post" || key === "delete" || key === "options" || key === "head" || key === "patch" || key === "trace") {
561
+ pathItemObject[key] = createOperation(value, registry$1, [...path, key]);
562
+ continue;
563
+ }
564
+ if (key === "parameters") {
565
+ pathItemObject[key] = createManualParameters(value, registry$1, [...path, key]);
566
+ continue;
567
+ }
568
+ pathItemObject[key] = value;
569
+ }
570
+ if (id) {
571
+ if (registry$1.components.pathItems.ids.has(id)) throw new Error(`PathItem "${id}" is already registered`);
572
+ const ref = { $ref: `#/components/pathItems/${id}` };
573
+ registry$1.components.pathItems.ids.set(id, pathItemObject);
574
+ registry$1.components.pathItems.seen.set(pathItem, ref);
575
+ return ref;
576
+ }
577
+ registry$1.components.pathItems.seen.set(pathItem, pathItemObject);
578
+ return pathItemObject;
579
+ },
580
+ addResponse: (response, path, opts) => {
581
+ const seenResponse = registry$1.components.responses.seen.get(response);
582
+ if (seenResponse) return seenResponse;
583
+ const { content, headers, id: metaId,...rest } = response;
584
+ const responseObject = rest;
585
+ const maybeHeaders = createHeaders(headers, registry$1, [...path, "headers"]);
586
+ if (maybeHeaders) responseObject.headers = maybeHeaders;
587
+ if (content) responseObject.content = createContent(content, {
588
+ registry: registry$1,
589
+ io: "output"
590
+ }, [...path, "content"]);
591
+ const id = metaId ?? opts?.manualId;
592
+ if (id) {
593
+ if (registry$1.components.responses.ids.has(id)) throw new Error(`Response "${id}" is already registered`);
594
+ const ref = { $ref: `#/components/responses/${id}` };
595
+ registry$1.components.responses.ids.set(id, responseObject);
596
+ registry$1.components.responses.seen.set(response, ref);
597
+ return ref;
598
+ }
599
+ registry$1.components.responses.seen.set(response, responseObject);
600
+ return responseObject;
601
+ },
602
+ addCallback: (callback, path, opts) => {
603
+ const seenCallback = registry$1.components.callbacks.seen.get(callback);
604
+ if (seenCallback) return seenCallback;
605
+ const { id: metaId,...rest } = callback;
606
+ const callbackObject = {};
607
+ for (const [name, pathItem] of Object.entries(rest)) {
608
+ if (isISpecificationExtension(name)) {
609
+ callbackObject[name] = pathItem;
610
+ continue;
611
+ }
612
+ callbackObject[name] = registry$1.addPathItem(pathItem, [...path, name]);
613
+ }
614
+ const id = metaId ?? opts?.manualId;
615
+ if (id) {
616
+ if (registry$1.components.callbacks.ids.has(id)) throw new Error(`Callback "${id}" is already registered`);
617
+ const ref = { $ref: `#/components/callbacks/${id}` };
618
+ registry$1.components.callbacks.ids.set(id, callbackObject);
619
+ registry$1.components.callbacks.seen.set(callback, ref);
620
+ return ref;
621
+ }
622
+ registry$1.components.callbacks.seen.set(callback, callbackObject);
623
+ return callbackObject;
624
+ }
625
+ };
626
+ registerSchemas(components?.schemas, registry$1);
627
+ registerParameters(components?.parameters, registry$1);
628
+ registerHeaders(components?.headers, registry$1);
629
+ registerResponses(components?.responses, registry$1);
630
+ registerPathItems(components?.pathItems, registry$1);
631
+ registerRequestBodies(components?.requestBodies, registry$1);
632
+ registerCallbacks(components?.callbacks, registry$1);
633
+ return registry$1;
634
+ };
635
+ const registerSchemas = (schemas, registry$1) => {
636
+ if (!schemas) return;
637
+ for (const [key, schema] of Object.entries(schemas)) {
638
+ if (isAnyZodType(schema)) {
639
+ const id = zod_v4_core.globalRegistry.get(schema)?.id ?? key;
640
+ registry$1.components.schemas.manual.set(id, {
641
+ input: { schemaObject: {} },
642
+ output: { schemaObject: {} },
643
+ zodType: schema
644
+ });
645
+ continue;
646
+ }
647
+ registry$1.components.schemas.ids.set(key, schema);
648
+ }
649
+ };
650
+ const registerParameters = (parameters, registry$1) => {
651
+ if (!parameters) return;
652
+ for (const [key, schema] of Object.entries(parameters)) {
653
+ if (isAnyZodType(schema)) {
654
+ const path = [
655
+ "components",
656
+ "parameters",
657
+ key
658
+ ];
659
+ registry$1.addParameter(schema, path, { manualId: key });
660
+ continue;
661
+ }
662
+ registry$1.components.parameters.ids.set(key, schema);
663
+ }
664
+ };
665
+ const registerHeaders = (headers, registry$1) => {
666
+ if (!headers) return;
667
+ for (const [key, schema] of Object.entries(headers)) {
668
+ if (isAnyZodType(schema)) {
669
+ const path = [
670
+ "components",
671
+ "headers",
672
+ key
673
+ ];
674
+ registry$1.addHeader(schema, path, { manualId: key });
675
+ continue;
676
+ }
677
+ registry$1.components.headers.ids.set(key, schema);
678
+ }
679
+ };
680
+ const registerResponses = (responses, registry$1) => {
681
+ if (!responses) return;
682
+ for (const [key, schema] of Object.entries(responses)) {
683
+ const responseObject = registry$1.addResponse(schema, [
684
+ "components",
685
+ "responses",
686
+ key
687
+ ], { manualId: key });
688
+ registry$1.components.responses.ids.set(key, responseObject);
689
+ registry$1.components.responses.seen.set(schema, responseObject);
690
+ }
691
+ };
692
+ const registerRequestBodies = (requestBodies, registry$1) => {
693
+ if (!requestBodies) return;
694
+ for (const [key, schema] of Object.entries(requestBodies)) {
695
+ if (isAnyZodType(schema)) {
696
+ registry$1.addRequestBody(schema, [
697
+ "components",
698
+ "requestBodies",
699
+ key
700
+ ], { manualId: key });
701
+ continue;
702
+ }
703
+ registry$1.components.requestBodies.ids.set(key, schema);
704
+ }
705
+ };
706
+ const registerCallbacks = (callbacks, registry$1) => {
707
+ if (!callbacks) return;
708
+ for (const [key, schema] of Object.entries(callbacks)) registry$1.addCallback(schema, [
709
+ "components",
710
+ "callbacks",
711
+ key
712
+ ], { manualId: key });
713
+ };
714
+ const registerPathItems = (pathItems, registry$1) => {
715
+ if (!pathItems) return;
716
+ for (const [key, schema] of Object.entries(pathItems)) registry$1.addPathItem(schema, [
717
+ "components",
718
+ "pathItems",
719
+ key
720
+ ], { manualId: key });
721
+ };
722
+ const createIOSchemas = (ctx) => {
723
+ const { schemas, components, manual } = createSchemas(Object.fromEntries(ctx.registry.components.schemas[ctx.io]), ctx);
724
+ for (const [key, schema] of Object.entries(components)) {
725
+ if (ctx.registry.components.schemas.ids.has(key)) throw new Error(`Schema "${key}" is already registered`);
726
+ ctx.registry.components.schemas.ids.set(key, schema);
727
+ }
728
+ for (const [key, schema] of Object.entries(schemas)) {
729
+ const ioSchema = ctx.registry.components.schemas[ctx.io].get(key);
730
+ if (ioSchema) Object.assign(ioSchema.schemaObject, schema);
731
+ }
732
+ for (const [key, value] of Object.entries(manual)) {
733
+ const manualSchema = ctx.registry.components.schemas.manual.get(key);
734
+ if (!manualSchema) continue;
735
+ if (components[key]) manualSchema[ctx.io].used = true;
736
+ Object.assign(manualSchema[ctx.io].schemaObject, value);
737
+ }
738
+ };
739
+ const createManualSchemas = (registry$1) => {
740
+ for (const [key, value] of registry$1.components.schemas.manual) if (!value.input.used) {
741
+ const io = zod_v4_core.globalRegistry.get(value.zodType)?.unusedIO ?? "output";
742
+ const schema = value[io].schemaObject;
743
+ registry$1.components.schemas.ids.set(key, schema);
744
+ }
745
+ };
746
+ const createComponents = (registry$1, opts) => {
747
+ createIOSchemas({
748
+ registry: registry$1,
749
+ io: "input",
750
+ opts
751
+ });
752
+ createIOSchemas({
753
+ registry: registry$1,
754
+ io: "output",
755
+ opts
756
+ });
757
+ createManualSchemas(registry$1);
758
+ const components = {};
759
+ if (registry$1.components.schemas.ids.size > 0) components.schemas = Object.fromEntries(registry$1.components.schemas.ids);
760
+ if (registry$1.components.headers.ids.size > 0) components.headers = Object.fromEntries(registry$1.components.headers.ids);
761
+ if (registry$1.components.requestBodies.ids.size > 0) components.requestBodies = Object.fromEntries(registry$1.components.requestBodies.ids);
762
+ if (registry$1.components.responses.ids.size > 0) components.responses = Object.fromEntries(registry$1.components.responses.ids);
763
+ if (registry$1.components.parameters.ids.size > 0) components.parameters = Object.fromEntries(registry$1.components.parameters.ids);
764
+ if (registry$1.components.callbacks.ids.size > 0) components.callbacks = Object.fromEntries(registry$1.components.callbacks.ids);
765
+ if (registry$1.components.pathItems.ids.size > 0) components.pathItems = Object.fromEntries(registry$1.components.pathItems.ids);
766
+ return components;
767
+ };
768
+
769
+ //#endregion
770
+ Object.defineProperty(exports, 'createComponents', {
771
+ enumerable: true,
772
+ get: function () {
773
+ return createComponents;
774
+ }
775
+ });
776
+ Object.defineProperty(exports, 'createPaths', {
777
+ enumerable: true,
778
+ get: function () {
779
+ return createPaths;
780
+ }
781
+ });
782
+ Object.defineProperty(exports, 'createRegistry', {
783
+ enumerable: true,
784
+ get: function () {
785
+ return createRegistry;
786
+ }
787
+ });
788
+ Object.defineProperty(exports, 'createSchema', {
789
+ enumerable: true,
790
+ get: function () {
791
+ return createSchema;
792
+ }
793
+ });
794
+ Object.defineProperty(exports, 'isAnyZodType', {
795
+ enumerable: true,
796
+ get: function () {
797
+ return isAnyZodType;
798
+ }
799
+ });
800
+ Object.defineProperty(exports, 'unwrapZodObject', {
801
+ enumerable: true,
802
+ get: function () {
803
+ return unwrapZodObject;
804
+ }
805
+ });