schema-components 1.13.0 → 1.15.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.
Files changed (71) hide show
  1. package/README.md +4 -0
  2. package/dist/core/adapter.d.mts +8 -3
  3. package/dist/core/adapter.mjs +25 -10
  4. package/dist/core/constraints.d.mts +3 -2
  5. package/dist/core/constraints.mjs +14 -2
  6. package/dist/core/diagnostics.d.mts +2 -0
  7. package/dist/core/diagnostics.mjs +33 -0
  8. package/dist/core/errors.d.mts +1 -1
  9. package/dist/core/formats.d.mts +33 -0
  10. package/dist/core/formats.mjs +67 -0
  11. package/dist/core/merge.d.mts +17 -1
  12. package/dist/core/merge.mjs +30 -1
  13. package/dist/core/normalise.d.mts +3 -2
  14. package/dist/core/normalise.mjs +1 -170
  15. package/dist/core/openapi30.d.mts +4 -1
  16. package/dist/core/openapi30.mjs +1 -222
  17. package/dist/core/ref.d.mts +2 -25
  18. package/dist/core/ref.mjs +102 -23
  19. package/dist/core/renderer.d.mts +1 -1
  20. package/dist/core/swagger2.d.mts +2 -1
  21. package/dist/core/swagger2.mjs +1 -293
  22. package/dist/core/typeInference.d.mts +2 -2
  23. package/dist/core/types.d.mts +2 -2
  24. package/dist/core/types.mjs +4 -1
  25. package/dist/core/version.d.mts +2 -2
  26. package/dist/core/version.mjs +84 -12
  27. package/dist/core/walkBuilders.d.mts +15 -1
  28. package/dist/core/walkBuilders.mjs +1 -1
  29. package/dist/core/walker.d.mts +1 -1
  30. package/dist/core/walker.mjs +122 -22
  31. package/dist/diagnostics-DzbZmcLI.d.mts +64 -0
  32. package/dist/html/a11y.d.mts +2 -2
  33. package/dist/html/renderToHtml.d.mts +2 -2
  34. package/dist/html/renderToHtmlStream.d.mts +2 -2
  35. package/dist/html/renderers.d.mts +2 -2
  36. package/dist/html/streamRenderers.d.mts +2 -2
  37. package/dist/normalise-tL9FckAk.mjs +748 -0
  38. package/dist/openapi/ApiCallbacks.d.mts +16 -0
  39. package/dist/openapi/ApiCallbacks.mjs +34 -0
  40. package/dist/openapi/ApiLinks.d.mts +16 -0
  41. package/dist/openapi/ApiLinks.mjs +42 -0
  42. package/dist/openapi/ApiResponseHeaders.d.mts +16 -0
  43. package/dist/openapi/ApiResponseHeaders.mjs +35 -0
  44. package/dist/openapi/ApiSecurity.d.mts +19 -0
  45. package/dist/openapi/ApiSecurity.mjs +33 -0
  46. package/dist/openapi/bundle.d.mts +47 -0
  47. package/dist/openapi/bundle.mjs +95 -0
  48. package/dist/openapi/components.d.mts +7 -2
  49. package/dist/openapi/components.mjs +30 -6
  50. package/dist/openapi/parser.d.mts +1 -1
  51. package/dist/react/SchemaComponent.d.mts +12 -5
  52. package/dist/react/SchemaComponent.mjs +13 -7
  53. package/dist/react/SchemaView.d.mts +10 -3
  54. package/dist/react/SchemaView.mjs +13 -7
  55. package/dist/react/fieldPath.d.mts +1 -1
  56. package/dist/react/headless.d.mts +1 -1
  57. package/dist/react/headlessRenderers.d.mts +1 -1
  58. package/dist/react/headlessRenderers.mjs +1 -1
  59. package/dist/ref-DvWoULcy.d.mts +44 -0
  60. package/dist/{renderer-DseHeliw.d.mts → renderer-BdSqllx5.d.mts} +1 -1
  61. package/dist/themes/mantine.d.mts +1 -1
  62. package/dist/themes/mui.d.mts +1 -1
  63. package/dist/themes/mui.mjs +1 -1
  64. package/dist/themes/radix.d.mts +1 -1
  65. package/dist/themes/shadcn.d.mts +1 -1
  66. package/dist/{typeInference-CRPqVwKu.d.mts → typeInference-k7FXfTVO.d.mts} +44 -8
  67. package/dist/{types-ag2jYLqQ.d.mts → types-D_5ST7SS.d.mts} +11 -3
  68. package/dist/version-B5NV-35j.d.mts +69 -0
  69. package/package.json +1 -1
  70. package/dist/version-CLchheaH.d.mts +0 -40
  71. /package/dist/{errors-DIKI2C78.d.mts → errors-C5zRC2PU.d.mts} +0 -0
@@ -0,0 +1,748 @@
1
+ import { isObject } from "./core/guards.mjs";
2
+ import { emitDiagnostic } from "./core/diagnostics.mjs";
3
+ import { isOpenApi30, isSwagger2 } from "./core/version.mjs";
4
+ //#region src/core/openapi30.ts
5
+ /**
6
+ * OpenAPI 3.0.x schema normalisation.
7
+ *
8
+ * Transforms `nullable`, `discriminator`, `example` keywords, and walks
9
+ * all schema locations (components, paths, parameters, request bodies,
10
+ * responses) to apply normalisation.
11
+ */
12
+ /**
13
+ * Normalise OpenAPI 3.0.x `nullable` keyword to `anyOf [T, null]`.
14
+ *
15
+ * OpenAPI 3.0 uses `nullable: true` instead of the JSON Schema standard
16
+ * `anyOf: [T, { type: "null" }]`. The walker understands the latter form
17
+ * natively, so this normaliser converts `nullable` to `anyOf`.
18
+ *
19
+ * Only applied when `nullable` is explicitly `true`. `nullable: false` or
20
+ * absent is the default and requires no transformation.
21
+ */
22
+ function normaliseOpenApi30Node(node) {
23
+ if ("example" in node && !("examples" in node)) {
24
+ node.examples = [node.example];
25
+ delete node.example;
26
+ } else if ("example" in node) delete node.example;
27
+ if (node.nullable !== true) {
28
+ if ("nullable" in node) delete node.nullable;
29
+ return node;
30
+ }
31
+ const nullOption = { type: "null" };
32
+ if (Array.isArray(node.anyOf)) {
33
+ node.anyOf = [...node.anyOf, nullOption];
34
+ delete node.nullable;
35
+ return node;
36
+ }
37
+ if (Array.isArray(node.oneOf)) {
38
+ node.anyOf = [...node.oneOf, nullOption];
39
+ delete node.oneOf;
40
+ delete node.nullable;
41
+ return node;
42
+ }
43
+ if (Array.isArray(node.allOf)) {
44
+ node.anyOf = [{ allOf: node.allOf }, nullOption];
45
+ delete node.allOf;
46
+ delete node.nullable;
47
+ return node;
48
+ }
49
+ const wrapper = {};
50
+ for (const [key, value] of Object.entries(node)) if (key !== "nullable") wrapper[key] = value;
51
+ return { anyOf: [wrapper, nullOption] };
52
+ }
53
+ /**
54
+ * Normalise OpenAPI 3.0.x `discriminator` keyword by injecting `const`
55
+ * values into each `oneOf`/`anyOf` option's discriminator property.
56
+ *
57
+ * In OpenAPI 3.0, `discriminator` is a sibling of `oneOf`/`anyOf`:
58
+ * discriminator: { propertyName: "type" }
59
+ * The walker detects discriminated unions from `oneOf` + `const` on a
60
+ * property, so this normaliser injects the `const` values from the
61
+ * `mapping` or infers them from `$ref` fragment names.
62
+ */
63
+ function normaliseOpenApi30Discriminator(node) {
64
+ const discriminator = node.discriminator;
65
+ if (!isObject(discriminator)) return node;
66
+ const propertyName = discriminator.propertyName;
67
+ if (typeof propertyName !== "string") return node;
68
+ const mapping = isObject(discriminator.mapping) ? discriminator.mapping : void 0;
69
+ const composite = node.oneOf ?? node.anyOf;
70
+ if (!Array.isArray(composite)) return node;
71
+ const refToValue = /* @__PURE__ */ new Map();
72
+ if (mapping !== void 0) {
73
+ for (const [value, ref] of Object.entries(mapping)) if (typeof ref === "string") refToValue.set(ref, value);
74
+ }
75
+ const normalisedComposite = [];
76
+ for (const option of composite) {
77
+ if (!isObject(option)) {
78
+ normalisedComposite.push(option);
79
+ continue;
80
+ }
81
+ const props = isObject(option.properties) ? { ...option.properties } : void 0;
82
+ const discProp = props?.[propertyName];
83
+ if (isObject(discProp) && "const" in discProp) {
84
+ normalisedComposite.push(option);
85
+ continue;
86
+ }
87
+ let constValue;
88
+ if (isObject(discProp) && typeof discProp.$ref === "string") constValue = refToValue.get(discProp.$ref);
89
+ if (constValue === void 0 && typeof option.$ref === "string") {
90
+ constValue = refToValue.get(option.$ref);
91
+ if (constValue === void 0) {
92
+ const fragment = option.$ref.split("/").pop();
93
+ if (fragment !== void 0) constValue = fragment;
94
+ }
95
+ }
96
+ if (constValue === void 0 && mapping !== void 0) {
97
+ const optionIndex = composite.indexOf(option);
98
+ const mappingEntries = Object.entries(mapping);
99
+ const entry = optionIndex >= 0 && optionIndex < mappingEntries.length ? mappingEntries[optionIndex] : void 0;
100
+ if (entry !== void 0) constValue = entry[0];
101
+ }
102
+ if (constValue !== void 0) {
103
+ const normalisedProps = props ?? {};
104
+ normalisedProps[propertyName] = {
105
+ ...isObject(discProp) ? discProp : {},
106
+ const: constValue
107
+ };
108
+ normalisedComposite.push({
109
+ ...option,
110
+ properties: normalisedProps
111
+ });
112
+ } else normalisedComposite.push(option);
113
+ }
114
+ if ("oneOf" in node) node.oneOf = normalisedComposite;
115
+ else if ("anyOf" in node) node.anyOf = normalisedComposite;
116
+ delete node.discriminator;
117
+ return node;
118
+ }
119
+ /**
120
+ * Combined OpenAPI 3.0.x node transform: Draft 04 + nullable + discriminator.
121
+ * Applied to every schema node in an OpenAPI 3.0 document.
122
+ *
123
+ * Draft 04 normalisation is included because OpenAPI 3.0 inherits
124
+ * Draft 04/05 schema semantics including `exclusiveMinimum: boolean`.
125
+ */
126
+ function normaliseOpenApi30Combined(node) {
127
+ return normaliseOpenApi30Discriminator(normaliseOpenApi30Node(normaliseDraft04Node(node)));
128
+ }
129
+ /**
130
+ * Deep-normalise all schemas in an OpenAPI 3.0.x document.
131
+ * Walks components/schemas, path operations, parameters, request bodies,
132
+ * and responses — applying `nullable` normalisation to each schema.
133
+ */
134
+ function deepNormaliseOpenApi30Doc(doc, deepNormalise) {
135
+ const result = { ...doc };
136
+ const components = doc.components;
137
+ if (isObject(components)) {
138
+ const schemas = components.schemas;
139
+ if (isObject(schemas)) {
140
+ const normalisedSchemas = {};
141
+ for (const [name, schema] of Object.entries(schemas)) normalisedSchemas[name] = isObject(schema) ? deepNormalise(schema, normaliseOpenApi30Combined) : schema;
142
+ result.components = {
143
+ ...components,
144
+ schemas: normalisedSchemas
145
+ };
146
+ }
147
+ }
148
+ const paths = doc.paths;
149
+ if (isObject(paths)) {
150
+ const normalisedPaths = {};
151
+ for (const [path, pathItem] of Object.entries(paths)) normalisedPaths[path] = isObject(pathItem) ? normalisePathItem(pathItem, deepNormalise) : pathItem;
152
+ result.paths = normalisedPaths;
153
+ }
154
+ return result;
155
+ }
156
+ function normalisePathItem(pathItem, deepNormalise) {
157
+ const result = { ...pathItem };
158
+ for (const method of [
159
+ "get",
160
+ "post",
161
+ "put",
162
+ "patch",
163
+ "delete"
164
+ ]) {
165
+ const operation = pathItem[method];
166
+ if (!isObject(operation)) continue;
167
+ result[method] = normaliseOperation(operation, deepNormalise);
168
+ }
169
+ const parameters = pathItem.parameters;
170
+ if (Array.isArray(parameters)) result.parameters = parameters.map((param) => isObject(param) ? normaliseParameter(param, deepNormalise) : param);
171
+ return result;
172
+ }
173
+ function normaliseOperation(operation, deepNormalise) {
174
+ const result = { ...operation };
175
+ const parameters = operation.parameters;
176
+ if (Array.isArray(parameters)) result.parameters = parameters.map((param) => isObject(param) ? normaliseParameter(param, deepNormalise) : param);
177
+ const requestBody = operation.requestBody;
178
+ if (isObject(requestBody)) result.requestBody = normaliseRequestBody(requestBody, deepNormalise);
179
+ const responses = operation.responses;
180
+ if (isObject(responses)) {
181
+ const normalisedResponses = {};
182
+ for (const [code, response] of Object.entries(responses)) normalisedResponses[code] = isObject(response) ? normaliseResponse(response, deepNormalise) : response;
183
+ result.responses = normalisedResponses;
184
+ }
185
+ return result;
186
+ }
187
+ function normaliseParameter(param, deepNormalise) {
188
+ const result = { ...param };
189
+ const schema = param.schema;
190
+ if (isObject(schema)) result.schema = deepNormalise(schema, normaliseOpenApi30Combined);
191
+ if ("example" in result && !("examples" in result)) {
192
+ result.examples = [result.example];
193
+ delete result.example;
194
+ } else if ("example" in result) delete result.example;
195
+ return result;
196
+ }
197
+ function normaliseRequestBody(requestBody, deepNormalise) {
198
+ const result = { ...requestBody };
199
+ const content = requestBody.content;
200
+ if (isObject(content)) result.content = normaliseContentMap(content, deepNormalise);
201
+ return result;
202
+ }
203
+ function normaliseResponse(response, deepNormalise) {
204
+ const result = { ...response };
205
+ const content = response.content;
206
+ if (isObject(content)) result.content = normaliseContentMap(content, deepNormalise);
207
+ return result;
208
+ }
209
+ function normaliseContentMap(content, deepNormalise) {
210
+ const result = {};
211
+ for (const [mediaType, mediaObj] of Object.entries(content)) {
212
+ if (!isObject(mediaObj)) {
213
+ result[mediaType] = mediaObj;
214
+ continue;
215
+ }
216
+ const normalised = { ...mediaObj };
217
+ const schema = mediaObj.schema;
218
+ if (isObject(schema)) normalised.schema = deepNormalise(schema, normaliseOpenApi30Combined);
219
+ if ("example" in normalised && !("examples" in normalised)) {
220
+ normalised.examples = { value: normalised.example };
221
+ delete normalised.example;
222
+ } else if ("example" in normalised) delete normalised.example;
223
+ result[mediaType] = normalised;
224
+ }
225
+ return result;
226
+ }
227
+ //#endregion
228
+ //#region src/core/swagger2.ts
229
+ /**
230
+ * Swagger 2.0 → OpenAPI 3.1 document normalisation.
231
+ *
232
+ * Transforms a Swagger 2.0 document into an OpenAPI 3.1-compatible
233
+ * structure: host/basePath/schemes → servers, definitions → components,
234
+ * body/formData params → requestBody, response schemas → content.
235
+ *
236
+ * Individual schemas within the document are also normalised for
237
+ * Draft 04 semantics (exclusiveMinimum/exclusiveMaximum booleans).
238
+ */
239
+ /**
240
+ * Transform a Swagger 2.0 document into an OpenAPI 3.1-compatible
241
+ * structure.
242
+ */
243
+ function normaliseSwagger2Document(doc, deepNormalise, normaliseDraft04Node, diagnostics) {
244
+ const result = {
245
+ openapi: "3.1.0",
246
+ info: isObject(doc.info) ? { ...doc.info } : {
247
+ title: "API",
248
+ version: "0.0.0"
249
+ }
250
+ };
251
+ if (typeof doc.host === "string" || typeof doc.basePath === "string" || Array.isArray(doc.schemes)) {
252
+ const host = typeof doc.host === "string" ? doc.host : "localhost";
253
+ const basePath = typeof doc.basePath === "string" ? doc.basePath : "/";
254
+ const schemes = Array.isArray(doc.schemes) ? doc.schemes : ["https"];
255
+ result.servers = [{ url: `${typeof schemes[0] === "string" ? schemes[0] : "https"}://${host}${basePath}` }];
256
+ }
257
+ const paths = doc.paths;
258
+ if (isObject(paths)) result.paths = normaliseSwaggerPaths(paths, doc);
259
+ const components = {};
260
+ const definitions = doc.definitions;
261
+ if (isObject(definitions)) {
262
+ const schemas = {};
263
+ for (const [name, schema] of Object.entries(definitions)) schemas[name] = isObject(schema) ? deepNormalise(schema, (node) => normaliseOpenApi30Combined(normaliseDraft04Node(node))) : schema;
264
+ components.schemas = schemas;
265
+ }
266
+ const parameters = doc.parameters;
267
+ if (isObject(parameters)) components.parameters = { ...parameters };
268
+ const responses = doc.responses;
269
+ if (isObject(responses)) components.responses = { ...responses };
270
+ const securityDefinitions = doc.securityDefinitions;
271
+ if (isObject(securityDefinitions)) components.securitySchemes = { ...securityDefinitions };
272
+ if (Object.keys(components).length > 0) result.components = components;
273
+ if (Array.isArray(doc.tags)) result.tags = doc.tags;
274
+ if (isObject(doc.externalDocs)) result.externalDocs = doc.externalDocs;
275
+ rewriteSwaggerRefs(result);
276
+ if (isObject(doc.xml) || isObject(doc.definitions) && hasXmlInSchemas(doc.definitions)) emitDiagnostic(diagnostics, {
277
+ code: "dropped-swagger-feature",
278
+ message: "Swagger 2.0 xml markup is not supported and will be dropped",
279
+ pointer: "",
280
+ detail: { feature: "xml" }
281
+ });
282
+ return result;
283
+ }
284
+ function normaliseSwaggerPaths(paths, doc) {
285
+ const result = {};
286
+ const METHODS = [
287
+ "get",
288
+ "post",
289
+ "put",
290
+ "patch",
291
+ "delete",
292
+ "head",
293
+ "options"
294
+ ];
295
+ for (const [path, pathItem] of Object.entries(paths)) {
296
+ if (!isObject(pathItem)) {
297
+ result[path] = pathItem;
298
+ continue;
299
+ }
300
+ const normalisedPath = {};
301
+ for (const method of METHODS) {
302
+ const operation = pathItem[method];
303
+ if (!isObject(operation)) continue;
304
+ normalisedPath[method] = normaliseSwaggerOperation(operation, doc);
305
+ }
306
+ const pathParams = pathItem.parameters;
307
+ if (Array.isArray(pathParams)) normalisedPath.parameters = pathParams.map((p) => isObject(p) ? normaliseSwaggerParameter(p, doc) : p);
308
+ result[path] = normalisedPath;
309
+ }
310
+ return result;
311
+ }
312
+ function normaliseSwaggerOperation(operation, doc) {
313
+ const result = {};
314
+ const globalProduces = Array.isArray(doc.produces) ? doc.produces : ["application/json"];
315
+ const globalConsumes = Array.isArray(doc.consumes) ? doc.consumes : ["application/json"];
316
+ const produces = Array.isArray(operation.produces) ? operation.produces : globalProduces;
317
+ const consumes = Array.isArray(operation.consumes) ? operation.consumes : globalConsumes;
318
+ for (const [key, value] of Object.entries(operation)) if (key !== "parameters" && key !== "responses" && key !== "produces" && key !== "consumes") result[key] = value;
319
+ const params = operation.parameters;
320
+ if (Array.isArray(params)) {
321
+ const nonBodyParams = [];
322
+ let bodyParam;
323
+ let usesFormData = false;
324
+ for (const param of params) {
325
+ if (!isObject(param)) {
326
+ nonBodyParams.push(param);
327
+ continue;
328
+ }
329
+ const resolvedParam = resolveSwaggerParameter(param, doc);
330
+ const location = resolvedParam.in;
331
+ if (location === "body") bodyParam = resolvedParam;
332
+ else if (location === "formData") {
333
+ bodyParam = buildFormDataBody(resolvedParam, params);
334
+ usesFormData = true;
335
+ } else nonBodyParams.push(normaliseSwaggerParameter(resolvedParam, doc));
336
+ }
337
+ if (nonBodyParams.length > 0) result.parameters = nonBodyParams;
338
+ if (bodyParam !== void 0) result.requestBody = buildRequestBody(bodyParam, usesFormData ? ["multipart/form-data"] : consumes);
339
+ }
340
+ const responses = operation.responses;
341
+ if (isObject(responses)) result.responses = normaliseSwaggerResponses(responses, doc, produces);
342
+ return result;
343
+ }
344
+ /**
345
+ * Resolve a Swagger parameter that may be a `$ref`.
346
+ */
347
+ function resolveSwaggerParameter(param, doc, visited = /* @__PURE__ */ new Set()) {
348
+ const ref = param.$ref;
349
+ if (typeof ref !== "string" || !ref.startsWith("#/parameters/")) return param;
350
+ if (visited.has(ref)) return param;
351
+ const nextVisited = new Set(visited);
352
+ nextVisited.add(ref);
353
+ const name = ref.slice(13);
354
+ const globalParams = doc.parameters;
355
+ if (isObject(globalParams)) {
356
+ const resolved = globalParams[name];
357
+ if (isObject(resolved)) {
358
+ if (typeof resolved.$ref === "string") return resolveSwaggerParameter(resolved, doc, nextVisited);
359
+ return resolved;
360
+ }
361
+ }
362
+ return param;
363
+ }
364
+ /**
365
+ * Normalise a single Swagger parameter to OpenAPI 3.x form.
366
+ */
367
+ function normaliseSwaggerParameter(param, doc) {
368
+ if (typeof param.$ref === "string") {
369
+ const resolved = resolveSwaggerParameter(param, doc);
370
+ if (resolved !== param) return normaliseSwaggerParameter(resolved, doc);
371
+ }
372
+ const result = {};
373
+ for (const [key, value] of Object.entries(param)) {
374
+ if (key === "type" || key === "format" || key === "collectionFormat") continue;
375
+ result[key] = value;
376
+ }
377
+ if (typeof param.type === "string") {
378
+ const schema = { type: param.type };
379
+ if (typeof param.format === "string") schema.format = param.format;
380
+ if (param.enum !== void 0) schema.enum = param.enum;
381
+ if (param.default !== void 0) schema.default = param.default;
382
+ if (param.minimum !== void 0) schema.minimum = param.minimum;
383
+ if (param.maximum !== void 0) schema.maximum = param.maximum;
384
+ result.schema = schema;
385
+ }
386
+ const cf = param.collectionFormat;
387
+ if (typeof cf === "string") switch (cf) {
388
+ case "csv":
389
+ result.style = "form";
390
+ result.explode = false;
391
+ break;
392
+ case "ssv":
393
+ result.style = "spaceDelimited";
394
+ result.explode = false;
395
+ break;
396
+ case "tsv":
397
+ result.style = "tabDelimited";
398
+ result.explode = false;
399
+ break;
400
+ case "pipes":
401
+ result.style = "pipeDelimited";
402
+ result.explode = false;
403
+ break;
404
+ case "multi":
405
+ result.style = "form";
406
+ result.explode = true;
407
+ break;
408
+ }
409
+ return result;
410
+ }
411
+ /**
412
+ * Build a request body from a `formData` parameter.
413
+ */
414
+ function buildFormDataBody(param, allParams) {
415
+ const properties = {};
416
+ const required = [];
417
+ for (const p of allParams) {
418
+ if (!isObject(p) || p.in !== "formData") continue;
419
+ const name = p.name;
420
+ if (typeof name !== "string") continue;
421
+ const schema = {};
422
+ if (p.type === "file") {
423
+ schema.type = "string";
424
+ schema.format = "binary";
425
+ } else {
426
+ if (typeof p.type === "string") schema.type = p.type;
427
+ if (typeof p.format === "string") schema.format = p.format;
428
+ if (p.enum !== void 0) schema.enum = p.enum;
429
+ }
430
+ properties[name] = schema;
431
+ if (p.required === true) required.push(name);
432
+ }
433
+ return {
434
+ name: param.name,
435
+ in: "body",
436
+ schema: {
437
+ type: "object",
438
+ properties,
439
+ ...required.length > 0 ? { required } : {}
440
+ }
441
+ };
442
+ }
443
+ /**
444
+ * Build an OpenAPI 3.x request body from a Swagger 2.0 body parameter.
445
+ */
446
+ function buildRequestBody(bodyParam, consumes) {
447
+ const schema = bodyParam.schema;
448
+ const content = {};
449
+ const contentTypes = consumes.length > 0 ? consumes : ["application/json"];
450
+ for (const ct of contentTypes) if (typeof ct === "string") content[ct] = isObject(schema) ? { schema } : {};
451
+ const result = { content };
452
+ if (bodyParam.required === true) result.required = true;
453
+ if (typeof bodyParam.description === "string") result.description = bodyParam.description;
454
+ return result;
455
+ }
456
+ /**
457
+ * Resolve a Swagger 2.0 response `$ref` (e.g. `#/responses/NotFound`).
458
+ */
459
+ function resolveSwaggerResponse(response, doc, visited = /* @__PURE__ */ new Set()) {
460
+ const ref = response.$ref;
461
+ if (typeof ref !== "string" || !ref.startsWith("#/responses/")) return response;
462
+ if (visited.has(ref)) return response;
463
+ const nextVisited = new Set(visited);
464
+ nextVisited.add(ref);
465
+ const name = ref.slice(12);
466
+ const globalResponses = doc.responses;
467
+ if (isObject(globalResponses)) {
468
+ const resolved = globalResponses[name];
469
+ if (isObject(resolved)) {
470
+ if (typeof resolved.$ref === "string") return resolveSwaggerResponse(resolved, doc, nextVisited);
471
+ return resolved;
472
+ }
473
+ }
474
+ return response;
475
+ }
476
+ function normaliseSwaggerResponses(responses, doc, produces) {
477
+ const result = {};
478
+ for (const [code, response] of Object.entries(responses)) {
479
+ if (!isObject(response)) {
480
+ result[code] = response;
481
+ continue;
482
+ }
483
+ const resolved = resolveSwaggerResponse(response, doc);
484
+ const normalised = {};
485
+ for (const [key, value] of Object.entries(resolved)) if (key !== "schema") normalised[key] = value;
486
+ const schema = resolved.schema;
487
+ if (isObject(schema)) {
488
+ const content = {};
489
+ const contentTypes = produces.length > 0 ? produces : ["application/json"];
490
+ for (const ct of contentTypes) if (typeof ct === "string") content[ct] = { schema };
491
+ normalised.content = content;
492
+ }
493
+ result[code] = normalised;
494
+ }
495
+ return result;
496
+ }
497
+ /**
498
+ * Mapping of Swagger 2.0 $ref prefixes to OpenAPI 3.x equivalents.
499
+ * Applied after document restructuring so all $ref strings point
500
+ * to the correct locations in the normalised document.
501
+ */
502
+ const REF_REWRITES = [
503
+ ["#/definitions/", "#/components/schemas/"],
504
+ ["#/parameters/", "#/components/parameters/"],
505
+ ["#/responses/", "#/components/responses/"]
506
+ ];
507
+ /**
508
+ * Deep-rewrite $ref strings in a normalised Swagger 2.0 document
509
+ * from Swagger 2.0 locations to OpenAPI 3.x locations.
510
+ * Mutates the object in place \u2014 called only on the fresh clone
511
+ * produced by normaliseSwagger2Document.
512
+ */
513
+ function rewriteSwaggerRefs(node) {
514
+ if (!isObject(node)) return;
515
+ if (typeof node.$ref === "string") {
516
+ for (const [from, to] of REF_REWRITES) if (node.$ref.startsWith(from)) {
517
+ node.$ref = to + node.$ref.slice(from.length);
518
+ break;
519
+ }
520
+ }
521
+ for (const value of Object.values(node)) if (isObject(value)) rewriteSwaggerRefs(value);
522
+ else if (Array.isArray(value)) for (const item of value) rewriteSwaggerRefs(item);
523
+ }
524
+ /**
525
+ * Check if any schema in a definitions block contains an `xml` property.
526
+ */
527
+ function hasXmlInSchemas(definitions) {
528
+ for (const schema of Object.values(definitions)) if (isObject(schema) && "xml" in schema) return true;
529
+ return false;
530
+ }
531
+ //#endregion
532
+ //#region src/core/normalise.ts
533
+ /**
534
+ * Keys whose values are `Record<string, SubSchema>` — objects where each
535
+ * property is a sub-schema.
536
+ */
537
+ const OBJECT_SUBSCHEMA_KEYS = new Set([
538
+ "properties",
539
+ "patternProperties",
540
+ "$defs",
541
+ "definitions",
542
+ "dependentSchemas"
543
+ ]);
544
+ /**
545
+ * Keys whose values are `SubSchema[]` — arrays of sub-schemas.
546
+ */
547
+ const ARRAY_SUBSCHEMA_KEYS = new Set([
548
+ "allOf",
549
+ "anyOf",
550
+ "oneOf",
551
+ "prefixItems"
552
+ ]);
553
+ /**
554
+ * Keys whose values are a single sub-schema object.
555
+ */
556
+ const SINGLE_SUBSCHEMA_KEYS = new Set([
557
+ "additionalProperties",
558
+ "not",
559
+ "contains",
560
+ "propertyNames",
561
+ "if",
562
+ "then",
563
+ "else",
564
+ "unevaluatedProperties",
565
+ "unevaluatedItems"
566
+ ]);
567
+ /**
568
+ * Normalise each element of an unknown array by applying deepNormalise
569
+ * to object elements and passing others through unchanged.
570
+ */
571
+ function normaliseArray(items, transform) {
572
+ const result = [];
573
+ for (const item of items) result.push(isObject(item) ? deepNormalise(item, transform) : item);
574
+ return result;
575
+ }
576
+ /**
577
+ * Normalise each value of a sub-schema map (e.g. properties, $defs).
578
+ */
579
+ function normaliseSubSchemaMap(map, transform) {
580
+ const result = {};
581
+ for (const [k, v] of Object.entries(map)) result[k] = isObject(v) ? deepNormalise(v, transform) : v;
582
+ return result;
583
+ }
584
+ /**
585
+ * Deep-normalise a JSON Schema object by applying a per-node transform
586
+ * and recursing into every sub-schema location.
587
+ */
588
+ function deepNormalise(schema, transform) {
589
+ const node = transform({ ...schema });
590
+ const result = {};
591
+ for (const [key, value] of Object.entries(node)) if (isObject(value) && OBJECT_SUBSCHEMA_KEYS.has(key)) result[key] = normaliseSubSchemaMap(value, transform);
592
+ else if (Array.isArray(value) && ARRAY_SUBSCHEMA_KEYS.has(key)) result[key] = normaliseArray(value, transform);
593
+ else if (isObject(value) && SINGLE_SUBSCHEMA_KEYS.has(key)) result[key] = deepNormalise(value, transform);
594
+ else if (key === "items") if (Array.isArray(value)) result[key] = normaliseArray(value, transform);
595
+ else if (isObject(value)) result[key] = deepNormalise(value, transform);
596
+ else result[key] = value;
597
+ else if (key === "dependencies" && isObject(value)) {
598
+ const normalised = {};
599
+ for (const [dk, dv] of Object.entries(value)) if (isObject(dv)) normalised[dk] = deepNormalise(dv, transform);
600
+ else normalised[dk] = dv;
601
+ result[key] = normalised;
602
+ } else result[key] = value;
603
+ return result;
604
+ }
605
+ /**
606
+ * Split the legacy `dependencies` keyword into `dependentRequired` and
607
+ * `dependentSchemas` per the Draft 2019-09+ replacement.
608
+ *
609
+ * Each key in `dependencies` maps to either:
610
+ * - `string[]` → `dependentRequired`
611
+ * - A schema object → `dependentSchemas`
612
+ *
613
+ * Both forms can coexist within the same `dependencies` object.
614
+ * After splitting, `dependencies` is removed from the node.
615
+ */
616
+ function splitDependencies(node) {
617
+ const deps = node.dependencies;
618
+ if (!isObject(deps)) return;
619
+ const requiredEntries = {};
620
+ const schemaEntries = {};
621
+ for (const [key, value] of Object.entries(deps)) if (Array.isArray(value)) {
622
+ const strings = value.filter((v) => typeof v === "string");
623
+ if (strings.length === value.length) requiredEntries[key] = strings;
624
+ } else if (isObject(value)) schemaEntries[key] = value;
625
+ if (Object.keys(requiredEntries).length > 0) {
626
+ const existing = node.dependentRequired;
627
+ if (isObject(existing)) for (const [k, v] of Object.entries(requiredEntries)) existing[k] = v;
628
+ else node.dependentRequired = requiredEntries;
629
+ }
630
+ if (Object.keys(schemaEntries).length > 0) {
631
+ const existing = node.dependentSchemas;
632
+ if (isObject(existing)) for (const [k, v] of Object.entries(schemaEntries)) existing[k] = v;
633
+ else node.dependentSchemas = schemaEntries;
634
+ }
635
+ delete node.dependencies;
636
+ }
637
+ /**
638
+ * Normalise Draft 04 `exclusiveMinimum`/`exclusiveMaximum` from boolean
639
+ * to number form.
640
+ *
641
+ * In Draft 04:
642
+ * - `exclusiveMinimum: true` + `minimum: 5` → value must be > 5
643
+ * - `exclusiveMinimum: false` (or absent) + `minimum: 5` → value must be >= 5
644
+ *
645
+ * In Draft 06+:
646
+ * - `exclusiveMinimum: 5` → value must be > 5 (no separate `minimum`)
647
+ * - `minimum: 5` → value must be >= 5
648
+ *
649
+ * The transform converts boolean form to number form so the walker can
650
+ * treat `exclusiveMinimum`/`exclusiveMaximum` uniformly as numbers.
651
+ */
652
+ function normaliseDraft04Node(node) {
653
+ if (node.exclusiveMinimum === true && typeof node.minimum === "number") {
654
+ node.exclusiveMinimum = node.minimum;
655
+ delete node.minimum;
656
+ } else if (node.exclusiveMinimum === false) delete node.exclusiveMinimum;
657
+ if (node.exclusiveMaximum === true && typeof node.maximum === "number") {
658
+ node.exclusiveMaximum = node.maximum;
659
+ delete node.maximum;
660
+ } else if (node.exclusiveMaximum === false) delete node.exclusiveMaximum;
661
+ if (typeof node.id === "string" && !("$id" in node)) {
662
+ node.$id = node.id;
663
+ delete node.id;
664
+ }
665
+ if (Array.isArray(node.items) && !("prefixItems" in node)) {
666
+ node.prefixItems = node.items;
667
+ delete node.items;
668
+ if ("additionalItems" in node) {
669
+ node.items = node.additionalItems;
670
+ delete node.additionalItems;
671
+ }
672
+ }
673
+ splitDependencies(node);
674
+ return node;
675
+ }
676
+ /**
677
+ * Normalise Draft 06/07 nodes.
678
+ *
679
+ * These drafts introduced `exclusiveMinimum`/`exclusiveMaximum` as numbers
680
+ * (already in final form) and `const`/`examples`, but still use the
681
+ * legacy `dependencies` keyword. Split it into `dependentRequired` /
682
+ * `dependentSchemas` so the walker can process them uniformly.
683
+ */
684
+ function normaliseDraft06Or07Node(node) {
685
+ splitDependencies(node);
686
+ return node;
687
+ }
688
+ /**
689
+ * Normalise Draft 2019-09 `$recursiveRef` to `$ref`.
690
+ *
691
+ * `$recursiveRef` resolves to the nearest `$recursiveAnchor` in the
692
+ * dynamic scope. For rendering, the common pattern is a root
693
+ * `$recursiveAnchor: true` — the normaliser converts
694
+ * `$recursiveRef: "#"` to `$ref: "#"` pointing to the root.
695
+ *
696
+ * If a `$recursiveAnchor` name is given (non-empty string), the ref
697
+ * is converted to `$ref: "#<anchor>"` so the existing $anchor
698
+ * resolution in ref.ts can find it.
699
+ */
700
+ function normaliseDraft201909Node(node) {
701
+ if (typeof node.$recursiveRef === "string") {
702
+ node.$ref = "#";
703
+ delete node.$recursiveRef;
704
+ }
705
+ if (node.$recursiveAnchor === true) {
706
+ if (typeof node.$anchor !== "string") node.$anchor = "__recursive__";
707
+ delete node.$recursiveAnchor;
708
+ }
709
+ return node;
710
+ }
711
+ function normaliseDynamicRefNode(node) {
712
+ if (typeof node.$dynamicRef === "string") {
713
+ node.$ref = node.$dynamicRef;
714
+ delete node.$dynamicRef;
715
+ }
716
+ if (typeof node.$dynamicAnchor === "string") {
717
+ if (typeof node.$anchor !== "string") node.$anchor = node.$dynamicAnchor;
718
+ delete node.$dynamicAnchor;
719
+ }
720
+ return node;
721
+ }
722
+ /**
723
+ * Normalise a JSON Schema to canonical Draft 2020-12 form.
724
+ * Deep-clones the input — the original is never mutated.
725
+ */
726
+ function normaliseJsonSchema(schema, draft) {
727
+ switch (draft) {
728
+ case "draft-04": return deepNormalise(schema, normaliseDraft04Node);
729
+ case "draft-2019-09": return deepNormalise(schema, normaliseDraft201909Node);
730
+ case "draft-2020-12": return deepNormalise(schema, normaliseDynamicRefNode);
731
+ case "draft-06":
732
+ case "draft-07": return deepNormalise(schema, normaliseDraft06Or07Node);
733
+ }
734
+ }
735
+ /**
736
+ * Normalise an OpenAPI document's schemas for walker consumption.
737
+ * Handles version-specific keyword transformations.
738
+ *
739
+ * Returns the same object reference if no normalisation is needed
740
+ * (OpenAPI 3.1.x), or a deep-cloned normalised copy otherwise.
741
+ */
742
+ function normaliseOpenApiSchemas(doc, version, diagnostics) {
743
+ if (isSwagger2(version)) return normaliseSwagger2Document(doc, deepNormalise, normaliseDraft04Node, diagnostics);
744
+ if (isOpenApi30(version)) return deepNormaliseOpenApi30Doc(doc, deepNormalise);
745
+ return doc;
746
+ }
747
+ //#endregion
748
+ export { normaliseSwagger2Document as a, normaliseOpenApi30Discriminator as c, normaliseOpenApiSchemas as i, normaliseOpenApi30Node as l, normaliseDraft04Node as n, deepNormaliseOpenApi30Doc as o, normaliseJsonSchema as r, normaliseOpenApi30Combined as s, deepNormalise as t };