oas 31.1.2 → 32.1.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 (70) hide show
  1. package/README.md +5 -0
  2. package/dist/analyzer/index.cjs +6 -7
  3. package/dist/analyzer/index.cjs.map +1 -1
  4. package/dist/analyzer/index.js +5 -6
  5. package/dist/analyzer/index.js.map +1 -1
  6. package/dist/{chunk-BRYRBTD7.cjs → chunk-4OCSFLJO.cjs} +537 -385
  7. package/dist/chunk-4OCSFLJO.cjs.map +1 -0
  8. package/dist/{chunk-HGVFNEKW.js → chunk-5245ZLBC.js} +476 -324
  9. package/dist/chunk-5245ZLBC.js.map +1 -0
  10. package/dist/{chunk-B5WP4BJM.cjs → chunk-7RDMAMMI.cjs} +511 -240
  11. package/dist/chunk-7RDMAMMI.cjs.map +1 -0
  12. package/dist/{chunk-LSH3X5NA.js → chunk-DVD6RE2D.js} +85 -48
  13. package/dist/chunk-DVD6RE2D.js.map +1 -0
  14. package/dist/{chunk-5EP6HY2E.js → chunk-HYH37HS2.js} +494 -223
  15. package/dist/chunk-HYH37HS2.js.map +1 -0
  16. package/dist/{chunk-7MARUOFZ.js → chunk-MNOEMVCF.js} +2 -2
  17. package/dist/chunk-MNOEMVCF.js.map +1 -0
  18. package/dist/{chunk-SYZDNSG6.cjs → chunk-WXNEGGLJ.cjs} +104 -67
  19. package/dist/chunk-WXNEGGLJ.cjs.map +1 -0
  20. package/dist/chunk-YPR7YTHM.cjs +24 -0
  21. package/dist/chunk-YPR7YTHM.cjs.map +1 -0
  22. package/dist/extensions.d.cts +3 -1
  23. package/dist/extensions.d.ts +3 -1
  24. package/dist/get-parameters-as-json-schema-BH81ZOnw.d.ts +36 -0
  25. package/dist/get-parameters-as-json-schema-DM1vWIEM.d.cts +36 -0
  26. package/dist/index.cjs +5 -6
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +947 -7
  29. package/dist/index.d.ts +946 -8
  30. package/dist/index.js +4 -5
  31. package/dist/operation/index.cjs +4 -5
  32. package/dist/operation/index.cjs.map +1 -1
  33. package/dist/operation/index.d.cts +3 -1
  34. package/dist/operation/index.d.ts +3 -1
  35. package/dist/operation/index.js +3 -4
  36. package/dist/reducer/index.cjs +16 -18
  37. package/dist/reducer/index.cjs.map +1 -1
  38. package/dist/reducer/index.js +4 -6
  39. package/dist/reducer/index.js.map +1 -1
  40. package/dist/types.cjs +2 -2
  41. package/dist/types.d.cts +9 -1
  42. package/dist/types.d.ts +9 -1
  43. package/dist/types.js +1 -1
  44. package/dist/utils.cjs +4 -4
  45. package/dist/utils.cjs.map +1 -1
  46. package/dist/utils.d.cts +17 -3
  47. package/dist/utils.d.ts +17 -3
  48. package/dist/utils.js +5 -5
  49. package/package.json +3 -6
  50. package/dist/chunk-5EP6HY2E.js.map +0 -1
  51. package/dist/chunk-7MARUOFZ.js.map +0 -1
  52. package/dist/chunk-B5WP4BJM.cjs.map +0 -1
  53. package/dist/chunk-BRYRBTD7.cjs.map +0 -1
  54. package/dist/chunk-HGVFNEKW.js.map +0 -1
  55. package/dist/chunk-LSH3X5NA.js.map +0 -1
  56. package/dist/chunk-O3GIPZLC.cjs +0 -9
  57. package/dist/chunk-O3GIPZLC.cjs.map +0 -1
  58. package/dist/chunk-SYZDNSG6.cjs.map +0 -1
  59. package/dist/chunk-VA3NKXX7.js +0 -9
  60. package/dist/chunk-VA3NKXX7.js.map +0 -1
  61. package/dist/chunk-VQBEI5WI.cjs +0 -24
  62. package/dist/chunk-VQBEI5WI.cjs.map +0 -1
  63. package/dist/extensions-9XckV6aO.d.cts +0 -899
  64. package/dist/extensions-gq53-7Ux.d.ts +0 -899
  65. package/dist/operation/lib/get-parameters-as-json-schema.cjs +0 -11
  66. package/dist/operation/lib/get-parameters-as-json-schema.cjs.map +0 -1
  67. package/dist/operation/lib/get-parameters-as-json-schema.d.cts +0 -4
  68. package/dist/operation/lib/get-parameters-as-json-schema.d.ts +0 -4
  69. package/dist/operation/lib/get-parameters-as-json-schema.js +0 -11
  70. package/dist/operation/lib/get-parameters-as-json-schema.js.map +0 -1
@@ -6,15 +6,7 @@ import {
6
6
  isOpenAPI30,
7
7
  isRef,
8
8
  isSchema
9
- } from "./chunk-7MARUOFZ.js";
10
-
11
- // src/lib/clone-object.ts
12
- function cloneObject(obj) {
13
- if (typeof obj === "undefined") {
14
- return void 0;
15
- }
16
- return JSON.parse(JSON.stringify(obj));
17
- }
9
+ } from "./chunk-MNOEMVCF.js";
18
10
 
19
11
  // src/lib/matches-mimetype.ts
20
12
  function matchesMediaType(types2, mediaType) {
@@ -71,6 +63,9 @@ function getParameterContentType(contentKeys) {
71
63
  return contentKeys[0];
72
64
  }
73
65
 
66
+ // src/lib/refs.ts
67
+ import jsonpointer from "jsonpointer";
68
+
74
69
  // src/lib/helpers.ts
75
70
  function hasSchemaType(schema, discriminator) {
76
71
  if (Array.isArray(schema.type)) {
@@ -85,13 +80,18 @@ function isPrimitive(val) {
85
80
  return typeof val === "string" || typeof val === "number" || typeof val === "boolean";
86
81
  }
87
82
 
88
- // src/lib/openapi-to-json-schema.ts
89
- import mergeJSONSchemaAllOf from "json-schema-merge-allof";
90
- import jsonpointer2 from "jsonpointer";
91
- import removeUndefinedObjects from "remove-undefined-objects";
92
-
93
83
  // src/lib/refs.ts
94
- import jsonpointer from "jsonpointer";
84
+ function decorateComponentSchemasWithRefName(api) {
85
+ if (!api?.components?.schemas || typeof api.components.schemas !== "object") {
86
+ return;
87
+ }
88
+ Object.keys(api.components.schemas).forEach((schemaName) => {
89
+ if (isPrimitive(api.components?.schemas?.[schemaName]) || Array.isArray(api.components?.schemas?.[schemaName]) || api.components?.schemas?.[schemaName] === null) {
90
+ return;
91
+ }
92
+ (api.components?.schemas?.[schemaName])["x-readme-ref-name"] = schemaName;
93
+ });
94
+ }
95
95
  function encodePointer(str) {
96
96
  return str.replaceAll("~", "~0").replaceAll("/", "~1");
97
97
  }
@@ -114,7 +114,7 @@ function findRef($ref, definition) {
114
114
  }
115
115
  return current;
116
116
  }
117
- function dereferenceRef(value, definition, seenRefs) {
117
+ function dereferenceRef(value, definition, seenRefs = /* @__PURE__ */ new Set()) {
118
118
  if (value === void 0) {
119
119
  return void 0;
120
120
  }
@@ -123,23 +123,17 @@ function dereferenceRef(value, definition, seenRefs) {
123
123
  return value;
124
124
  }
125
125
  const ref = value.$ref;
126
- if (seenRefs?.has(ref)) {
126
+ if (seenRefs.has(ref)) {
127
127
  return value;
128
128
  }
129
- const localSeenRefs = seenRefs || /* @__PURE__ */ new Set();
130
- localSeenRefs.add(ref);
129
+ seenRefs.add(ref);
131
130
  try {
132
131
  const dereferenced = findRef(ref, definition);
133
132
  if (isRef(dereferenced)) {
134
- return dereferenceRef(dereferenced, definition, localSeenRefs);
133
+ return dereferenceRef(dereferenced, definition, seenRefs);
135
134
  }
136
- const refName = ref.split("/").pop();
137
135
  return {
138
- ...dereferenced,
139
- // Because dereferencing will eliminate any lineage back to the original `$ref`,
140
- // information that we might need at some point, we should preserve the original schema
141
- // name through a custom extension.
142
- "x-readme-ref-name": refName
136
+ ...dereferenced
143
137
  };
144
138
  } catch {
145
139
  return value;
@@ -164,14 +158,196 @@ function getDereferencingOptions(circularRefs) {
164
158
  }
165
159
  };
166
160
  }
161
+ function collectRefsInSchema(schema) {
162
+ const refs = /* @__PURE__ */ new Set();
163
+ if (!schema || typeof schema !== "object") return refs;
164
+ const obj = schema;
165
+ if (isRef(obj)) {
166
+ refs.add(obj.$ref);
167
+ }
168
+ for (const value of Object.values(obj)) {
169
+ if (Array.isArray(value)) {
170
+ for (const item of value) {
171
+ for (const r of collectRefsInSchema(item)) refs.add(r);
172
+ }
173
+ } else if (value && typeof value === "object") {
174
+ for (const r of collectRefsInSchema(value)) refs.add(r);
175
+ }
176
+ }
177
+ return refs;
178
+ }
179
+ function filterRequiredRefsToReferenced(requiredRefs, usedSchemas) {
180
+ const referenced = new Set(requiredRefs);
181
+ let prevSize = 0;
182
+ while (referenced.size > prevSize) {
183
+ prevSize = referenced.size;
184
+ for (const ref of referenced) {
185
+ const schema = usedSchemas.get(ref);
186
+ if (schema) {
187
+ for (const r of collectRefsInSchema(schema)) {
188
+ referenced.add(r);
189
+ }
190
+ }
191
+ }
192
+ }
193
+ const filtered = /* @__PURE__ */ new Map();
194
+ for (const ref of referenced) {
195
+ const s = usedSchemas.get(ref);
196
+ if (s !== void 0) {
197
+ filtered.set(ref, s);
198
+ }
199
+ }
200
+ return filtered;
201
+ }
202
+ function getRefPathSegments(ref) {
203
+ if (!ref.startsWith("#/")) return null;
204
+ const path = ref.slice(2).split("/").map((seg) => seg);
205
+ if (path.length < 2) {
206
+ return null;
207
+ }
208
+ return path;
209
+ }
210
+ function mergeReferencedSchemasIntoRoot(root, refToSchema) {
211
+ for (const [ref, schema] of refToSchema) {
212
+ const segments = getRefPathSegments(ref);
213
+ if (!segments || segments.length === 0) {
214
+ continue;
215
+ }
216
+ let current = root;
217
+ for (let i = 0; i < segments.length - 1; i++) {
218
+ const seg = segments[i];
219
+ let next = current[seg];
220
+ if (!next || typeof next !== "object" || Array.isArray(next)) {
221
+ next = {};
222
+ current[seg] = next;
223
+ }
224
+ current = next;
225
+ }
226
+ current[segments[segments.length - 1]] = schema;
227
+ }
228
+ }
229
+
230
+ // src/lib/clone-object.ts
231
+ function cloneObject(obj) {
232
+ if (typeof obj === "undefined") {
233
+ return void 0;
234
+ }
235
+ return JSON.parse(JSON.stringify(obj));
236
+ }
237
+
238
+ // src/lib/build-discriminator-one-of.ts
239
+ function hasDiscriminatorWithoutPolymorphism(schema) {
240
+ if (!schema || typeof schema !== "object") return false;
241
+ if (!("discriminator" in schema)) return false;
242
+ if ("oneOf" in schema || "anyOf" in schema) return false;
243
+ return true;
244
+ }
245
+ function allOfReferencesSchema(schema, targetSchemaName) {
246
+ if (!schema || typeof schema !== "object") return false;
247
+ if (!("allOf" in schema) || !Array.isArray(schema.allOf)) return false;
248
+ return schema.allOf.some((item) => {
249
+ if (isRef(item)) {
250
+ const refParts = item.$ref.split("/");
251
+ const refSchemaName = refParts[refParts.length - 1];
252
+ return refSchemaName === targetSchemaName;
253
+ }
254
+ return false;
255
+ });
256
+ }
257
+ function findDiscriminatorChildren(definition) {
258
+ const childrenMap = /* @__PURE__ */ new Map();
259
+ const childrenRefMap = /* @__PURE__ */ new Map();
260
+ if (!definition?.components?.schemas || typeof definition.components.schemas !== "object") {
261
+ return { children: childrenMap, refs: childrenRefMap };
262
+ }
263
+ const schemas = definition.components.schemas;
264
+ const schemaNames = Object.keys(schemas);
265
+ const discriminatorSchemas = schemaNames.filter((name) => {
266
+ return hasDiscriminatorWithoutPolymorphism(schemas[name]);
267
+ });
268
+ for (const baseName of discriminatorSchemas) {
269
+ const baseSchema = schemas[baseName];
270
+ const discriminator = baseSchema.discriminator;
271
+ let childSchemaNames = [];
272
+ if (discriminator.mapping && typeof discriminator.mapping === "object") {
273
+ const mappingRefs = Object.values(discriminator.mapping);
274
+ if (mappingRefs.length > 0) {
275
+ childSchemaNames = mappingRefs.map((ref) => ref.split("/").pop()).filter((ref) => ref !== void 0);
276
+ }
277
+ }
278
+ if (!childSchemaNames.length) {
279
+ childSchemaNames = schemaNames.filter((name) => {
280
+ if (name === baseName) return false;
281
+ return allOfReferencesSchema(schemas[name], baseName);
282
+ });
283
+ }
284
+ if (childSchemaNames.length) {
285
+ for (const childName of childSchemaNames) {
286
+ childrenRefMap.set(childName, `#/components/schemas/${childName}`);
287
+ }
288
+ childrenMap.set(baseName, childSchemaNames);
289
+ childrenRefMap.set(baseName, `#/components/schemas/${baseName}`);
290
+ }
291
+ }
292
+ return { children: childrenMap, refs: childrenRefMap };
293
+ }
294
+ function applyDiscriminatorOneOfToUsedSchemas(definition, usedSchemas, getOrAddSchema) {
295
+ const { children: childrenMap, refs: childrenRefMap } = findDiscriminatorChildren(definition);
296
+ if (!childrenMap.size) return;
297
+ for (const [baseName, childNames] of childrenMap) {
298
+ const baseRef = childrenRefMap.get(baseName);
299
+ if (!baseRef) continue;
300
+ const baseSchema = usedSchemas.get(baseRef);
301
+ if (!baseSchema || typeof baseSchema !== "object") continue;
302
+ const oneOf = [];
303
+ for (const childName of childNames) {
304
+ const childRef = childrenRefMap.get(childName);
305
+ if (!childRef) continue;
306
+ const childSchema = getOrAddSchema(childRef);
307
+ if (childSchema) {
308
+ oneOf.push({
309
+ $ref: childRef
310
+ });
311
+ }
312
+ }
313
+ if (oneOf.length > 0) {
314
+ baseSchema.oneOf = oneOf;
315
+ }
316
+ }
317
+ for (const [parentSchemaName, childNames] of childrenMap) {
318
+ for (const childName of childNames) {
319
+ const childRef = childrenRefMap.get(childName);
320
+ if (!childRef) continue;
321
+ const childSchema = usedSchemas.get(childRef);
322
+ if (!childSchema || !("allOf" in childSchema) || !Array.isArray(childSchema.allOf)) {
323
+ continue;
324
+ }
325
+ for (let i = 0; i < childSchema.allOf.length; i++) {
326
+ const item = childSchema.allOf[i];
327
+ if (item && typeof item === "object" && "x-readme-ref-name" in item && item["x-readme-ref-name"] === parentSchemaName && "oneOf" in item) {
328
+ const clonedItem = cloneObject(item);
329
+ delete clonedItem.oneOf;
330
+ childSchema.allOf[i] = clonedItem;
331
+ }
332
+ }
333
+ }
334
+ }
335
+ }
167
336
 
168
337
  // src/lib/openapi-to-json-schema.ts
338
+ import mergeJSONSchemaAllOf from "json-schema-merge-allof";
339
+ import jsonpointer2 from "jsonpointer";
340
+ import removeUndefinedObjects from "remove-undefined-objects";
169
341
  var UNSUPPORTED_SCHEMA_PROPS = [
170
342
  "example",
171
343
  // OpenAPI supports `example` but we're mapping it to `examples` in this library.
172
344
  "externalDocs",
173
345
  "xml"
174
346
  ];
347
+ var PENDING_SCHEMA = { __pending: true };
348
+ function isPendingSchema(s) {
349
+ return isObject(s) && "__pending" in s && s.__pending === true;
350
+ }
175
351
  function getSchemaVersionString(schema, api) {
176
352
  if (isOpenAPI30(api)) {
177
353
  return "http://json-schema.org/draft-04/schema#";
@@ -187,6 +363,101 @@ function getSchemaVersionString(schema, api) {
187
363
  function isPolymorphicSchema(schema) {
188
364
  return "allOf" in schema || "anyOf" in schema || "oneOf" in schema;
189
365
  }
366
+ function inlinePropertyRefsForMerge(schema, usedSchemas) {
367
+ const out = structuredClone(schema);
368
+ if (!("properties" in out) || typeof out.properties !== "object" || out.properties === null) {
369
+ return out;
370
+ }
371
+ for (const key of Object.keys(out.properties)) {
372
+ const val = out.properties[key];
373
+ if (isRef(val)) {
374
+ const resolved = usedSchemas.get(val.$ref);
375
+ if (resolved !== void 0 && !isPendingSchema(resolved)) {
376
+ out.properties[key] = {
377
+ ...structuredClone(resolved)
378
+ };
379
+ }
380
+ }
381
+ }
382
+ return out;
383
+ }
384
+ function resolveAndCacheRefSchema({
385
+ schema,
386
+ definition,
387
+ usedSchemas,
388
+ seenRefs,
389
+ conversionOptions,
390
+ returnMode,
391
+ refLogger
392
+ }) {
393
+ const ref = schema.$ref;
394
+ const existing = usedSchemas.get(ref);
395
+ if (existing !== void 0 && !isPendingSchema(existing)) {
396
+ return returnMode === "converted" ? existing : { $ref: ref };
397
+ }
398
+ if (existing !== void 0 && isPendingSchema(existing)) {
399
+ return { $ref: ref };
400
+ }
401
+ usedSchemas.set(ref, PENDING_SCHEMA);
402
+ if (returnMode === "ref") {
403
+ let resolved;
404
+ try {
405
+ const dereferenced = dereferenceRef(schema, definition, seenRefs);
406
+ if (isRef(dereferenced)) {
407
+ refLogger(dereferenced.$ref, "ref");
408
+ let converted2;
409
+ try {
410
+ const pointer = ref.startsWith("#") ? decodeURIComponent(ref.substring(1)) : ref;
411
+ const rawSchema = jsonpointer2.get(definition, pointer);
412
+ if (rawSchema && typeof rawSchema === "object") {
413
+ converted2 = toJSONSchema(structuredClone(rawSchema), { ...conversionOptions, seenRefs });
414
+ } else {
415
+ converted2 = { $ref: ref };
416
+ }
417
+ } catch {
418
+ converted2 = { $ref: ref };
419
+ }
420
+ usedSchemas.set(ref, converted2);
421
+ refLogger(ref, "ref");
422
+ return { $ref: ref };
423
+ }
424
+ resolved = dereferenced;
425
+ } catch {
426
+ refLogger(ref, "ref");
427
+ usedSchemas.set(ref, { $ref: ref });
428
+ return { $ref: ref };
429
+ }
430
+ const converted = toJSONSchema(structuredClone(resolved), { ...conversionOptions, seenRefs });
431
+ usedSchemas.set(ref, converted);
432
+ refLogger(ref, "ref");
433
+ return { $ref: ref };
434
+ }
435
+ try {
436
+ const dereferenced = dereferenceRef(schema, definition, seenRefs);
437
+ if (isRef(dereferenced)) {
438
+ let converted2;
439
+ try {
440
+ const pointer = ref.startsWith("#") ? decodeURIComponent(ref.substring(1)) : ref;
441
+ const rawSchema = jsonpointer2.get(definition, pointer);
442
+ if (rawSchema && typeof rawSchema === "object") {
443
+ converted2 = toJSONSchema(structuredClone(rawSchema), { ...conversionOptions, seenRefs });
444
+ } else {
445
+ converted2 = { $ref: ref };
446
+ }
447
+ } catch {
448
+ converted2 = { $ref: ref };
449
+ }
450
+ usedSchemas.set(ref, converted2);
451
+ return converted2;
452
+ }
453
+ const converted = toJSONSchema(structuredClone(dereferenced), { ...conversionOptions, seenRefs });
454
+ usedSchemas.set(ref, converted);
455
+ return converted;
456
+ } catch {
457
+ usedSchemas.set(ref, { $ref: ref });
458
+ return { $ref: ref };
459
+ }
460
+ }
190
461
  function isRequestBodySchema(schema) {
191
462
  return "content" in schema;
192
463
  }
@@ -232,12 +503,13 @@ function searchForValueByPropAndPointer(property, pointer, schemas = []) {
232
503
  }
233
504
  return foundValue;
234
505
  }
235
- function toJSONSchema(data, opts = {}) {
506
+ function toJSONSchema(data, opts) {
236
507
  let schema = data === true ? {} : { ...data };
237
508
  const schemaAdditionalProperties = isSchema(schema) ? schema.additionalProperties : null;
238
509
  const {
239
510
  addEnumsToDescriptions,
240
511
  currentLocation,
512
+ definition,
241
513
  globalDefaults,
242
514
  hideReadOnlyProperties,
243
515
  hideWriteOnlyProperties,
@@ -245,7 +517,8 @@ function toJSONSchema(data, opts = {}) {
245
517
  prevDefaultSchemas = [],
246
518
  prevExampleSchemas = [],
247
519
  refLogger,
248
- transformer
520
+ seenRefs,
521
+ usedSchemas
249
522
  } = {
250
523
  addEnumsToDescriptions: false,
251
524
  currentLocation: "",
@@ -256,15 +529,64 @@ function toJSONSchema(data, opts = {}) {
256
529
  prevDefaultSchemas: [],
257
530
  prevExampleSchemas: [],
258
531
  refLogger: () => true,
259
- transformer: (s) => s,
532
+ seenRefs: /* @__PURE__ */ new Set(),
533
+ usedSchemas: /* @__PURE__ */ new Map(),
260
534
  ...opts
261
535
  };
536
+ const polyOptions = {
537
+ addEnumsToDescriptions,
538
+ currentLocation,
539
+ definition,
540
+ globalDefaults,
541
+ hideReadOnlyProperties,
542
+ hideWriteOnlyProperties,
543
+ isPolymorphicAllOfChild: false,
544
+ prevDefaultSchemas,
545
+ prevExampleSchemas,
546
+ refLogger,
547
+ seenRefs,
548
+ usedSchemas
549
+ };
262
550
  if (isRef(schema)) {
551
+ if (definition && usedSchemas) {
552
+ return resolveAndCacheRefSchema({
553
+ schema,
554
+ definition,
555
+ usedSchemas,
556
+ seenRefs,
557
+ conversionOptions: polyOptions,
558
+ returnMode: "ref",
559
+ refLogger
560
+ });
561
+ }
263
562
  refLogger(schema.$ref, "ref");
264
- return transformer(schema);
563
+ return schema;
265
564
  }
266
565
  if (isSchema(schema, isPolymorphicAllOfChild)) {
267
566
  if ("allOf" in schema && Array.isArray(schema.allOf)) {
567
+ let allOfSchemas = schema.allOf;
568
+ if (definition && usedSchemas) {
569
+ const allOfOptions = schema.allOf.length > 1 ? { ...polyOptions, refLogger: () => {
570
+ } } : polyOptions;
571
+ allOfSchemas = schema.allOf.map((item) => {
572
+ if (isRef(item)) {
573
+ return resolveAndCacheRefSchema({
574
+ schema: item,
575
+ definition,
576
+ usedSchemas,
577
+ seenRefs,
578
+ conversionOptions: allOfOptions,
579
+ returnMode: "converted",
580
+ refLogger
581
+ });
582
+ }
583
+ return toJSONSchema(item, allOfOptions);
584
+ });
585
+ schema = {
586
+ ...schema,
587
+ allOf: allOfSchemas.map((s) => inlinePropertyRefsForMerge(s, usedSchemas))
588
+ };
589
+ }
268
590
  try {
269
591
  schema = mergeJSONSchemaAllOf(schema, {
270
592
  ignoreAdditionalProperties: true,
@@ -301,9 +623,12 @@ function toJSONSchema(data, opts = {}) {
301
623
  schema = schemaWithoutAllOf;
302
624
  delete schema.allOf;
303
625
  }
626
+ collectRefsInSchema(schema).forEach((ref) => {
627
+ refLogger(ref, "ref");
628
+ });
304
629
  if (isRef(schema)) {
305
630
  refLogger(schema.$ref, "ref");
306
- return transformer(schema);
631
+ return schema;
307
632
  }
308
633
  }
309
634
  ["anyOf", "oneOf"].forEach((polyType) => {
@@ -313,56 +638,72 @@ function toJSONSchema(data, opts = {}) {
313
638
  if (!schema[polyType]?.[idx]) {
314
639
  return;
315
640
  }
316
- const polyOptions = {
317
- addEnumsToDescriptions,
318
- currentLocation: `${currentLocation}/${idx}`,
319
- globalDefaults,
320
- hideReadOnlyProperties,
321
- hideWriteOnlyProperties,
322
- isPolymorphicAllOfChild: false,
323
- prevDefaultSchemas,
324
- prevExampleSchemas,
325
- refLogger,
326
- transformer
641
+ const itemOptions = {
642
+ ...polyOptions,
643
+ currentLocation: `${currentLocation}/${idx}`
327
644
  };
328
645
  if ("properties" in schema) {
329
646
  schema[polyType][idx] = toJSONSchema(
330
- { required: schema.required, allOf: [item, { properties: schema.properties }] },
331
- polyOptions
647
+ {
648
+ required: schema.required,
649
+ allOf: [item, { properties: schema.properties }]
650
+ },
651
+ itemOptions
332
652
  );
333
653
  } else if ("items" in schema) {
334
654
  schema[polyType][idx] = toJSONSchema(
335
- { allOf: [item, { items: schema.items }] },
336
- polyOptions
655
+ {
656
+ allOf: [item, { items: schema.items }]
657
+ },
658
+ itemOptions
337
659
  );
338
660
  } else {
339
- schema[polyType][idx] = toJSONSchema(item, polyOptions);
661
+ schema[polyType][idx] = toJSONSchema(item, itemOptions);
340
662
  }
341
- if (isObject(schema[polyType][idx]) && "required" in schema[polyType][idx] && typeof schema[polyType][idx].required === "boolean") {
342
- delete schema[polyType][idx].required;
663
+ if ("required" in schema[polyType][idx]) {
664
+ if (Array.isArray(schema[polyType][idx].required) && schema[polyType][idx].required.length === 0) {
665
+ delete schema[polyType][idx].required;
666
+ } else if (isObject(schema[polyType][idx]) && typeof schema[polyType][idx].required === "boolean") {
667
+ delete schema[polyType][idx].required;
668
+ }
343
669
  }
344
- if (discriminatorPropertyName && isObject(schema[polyType][idx])) {
670
+ if (discriminatorPropertyName) {
345
671
  const childSchema = schema[polyType][idx];
346
- if ("discriminator" in childSchema) {
347
- delete childSchema.discriminator;
348
- }
349
- if ("oneOf" in childSchema) {
350
- delete childSchema.oneOf;
672
+ if (isObject(childSchema)) {
673
+ if ("discriminator" in childSchema) {
674
+ delete childSchema.discriminator;
675
+ }
676
+ if ("oneOf" in childSchema) {
677
+ delete childSchema.oneOf;
678
+ }
679
+ if ("anyOf" in childSchema) {
680
+ delete childSchema.anyOf;
681
+ }
351
682
  }
352
- if ("anyOf" in childSchema) {
353
- delete childSchema.anyOf;
683
+ if (definition && usedSchemas && isRef(childSchema)) {
684
+ const resolved = usedSchemas.get(childSchema.$ref);
685
+ if (resolved && typeof resolved === "object" && !isPendingSchema(resolved)) {
686
+ if ("discriminator" in resolved) {
687
+ delete resolved.discriminator;
688
+ }
689
+ if ("oneOf" in resolved) {
690
+ delete resolved.oneOf;
691
+ }
692
+ if ("anyOf" in resolved) {
693
+ delete resolved.anyOf;
694
+ }
695
+ usedSchemas.set(childSchema.$ref, resolved);
696
+ }
354
697
  }
355
698
  }
356
699
  });
357
700
  }
358
701
  });
359
- if ("discriminator" in schema) {
360
- if ("mapping" in schema.discriminator && typeof schema.discriminator.mapping === "object") {
361
- const mapping = schema.discriminator.mapping;
362
- Object.keys(mapping).forEach((k) => {
363
- refLogger(mapping[k], "discriminator");
364
- });
365
- }
702
+ if (schema?.discriminator?.mapping && typeof schema.discriminator.mapping === "object") {
703
+ const mapping = schema.discriminator.mapping;
704
+ Object.keys(mapping).forEach((k) => {
705
+ refLogger(mapping[k], "discriminator");
706
+ });
366
707
  }
367
708
  }
368
709
  if (!("type" in schema) && !isPolymorphicSchema(schema) && !isRequestBodySchema(schema)) {
@@ -519,21 +860,21 @@ function toJSONSchema(data, opts = {}) {
519
860
  }
520
861
  if (hasSchemaType(schema, "array")) {
521
862
  if ("items" in schema && schema.items !== void 0) {
522
- if (!Array.isArray(schema.items) && Object.keys(schema.items).length === 1 && isRef(schema.items)) {
863
+ if (!(definition && usedSchemas) && !Array.isArray(schema.items) && Object.keys(schema.items).length === 1 && isRef(schema.items)) {
523
864
  refLogger(schema.items.$ref, "ref");
524
865
  } else if (schema.items !== true) {
525
866
  schema.items = toJSONSchema(schema.items, {
526
- addEnumsToDescriptions,
867
+ ...polyOptions,
527
868
  currentLocation: `${currentLocation}/0`,
528
- globalDefaults,
529
- hideReadOnlyProperties,
530
- hideWriteOnlyProperties,
531
- prevExampleSchemas,
532
- refLogger,
533
- transformer
869
+ prevDefaultSchemas: [],
870
+ prevExampleSchemas
534
871
  });
535
- if (isObject(schema.items) && "required" in schema.items && !Array.isArray(schema.items.required)) {
536
- delete schema.items.required;
872
+ if ("required" in schema.items) {
873
+ if (Array.isArray(schema.items.required) && schema.items.required.length === 0) {
874
+ delete schema.items.required;
875
+ } else if (isObject(schema.items) && !Array.isArray(schema.items.required)) {
876
+ delete schema.items.required;
877
+ }
537
878
  }
538
879
  }
539
880
  } else if ("properties" in schema || "additionalProperties" in schema) {
@@ -546,15 +887,10 @@ function toJSONSchema(data, opts = {}) {
546
887
  Object.keys(schema.properties).forEach((prop) => {
547
888
  if (Array.isArray(schema.properties?.[prop]) || typeof schema.properties?.[prop] === "object" && schema.properties?.[prop] !== null) {
548
889
  const newPropSchema = toJSONSchema(schema.properties[prop], {
549
- addEnumsToDescriptions,
890
+ ...polyOptions,
550
891
  currentLocation: `${currentLocation}/${encodePointer(prop)}`,
551
- globalDefaults,
552
- hideReadOnlyProperties,
553
- hideWriteOnlyProperties,
554
892
  prevDefaultSchemas,
555
- prevExampleSchemas,
556
- refLogger,
557
- transformer
893
+ prevExampleSchemas
558
894
  });
559
895
  let propShouldBeUpdated = true;
560
896
  if ((hideReadOnlyProperties || hideWriteOnlyProperties) && !Object.keys(newPropSchema).length) {
@@ -578,7 +914,7 @@ function toJSONSchema(data, opts = {}) {
578
914
  });
579
915
  if (hideReadOnlyProperties || hideWriteOnlyProperties) {
580
916
  if (!Object.keys(schema.properties).length) {
581
- return transformer({});
917
+ return {};
582
918
  }
583
919
  }
584
920
  }
@@ -588,15 +924,10 @@ function toJSONSchema(data, opts = {}) {
588
924
  schema.additionalProperties = true;
589
925
  } else {
590
926
  schema.additionalProperties = toJSONSchema(schemaAdditionalProperties, {
591
- addEnumsToDescriptions,
927
+ ...polyOptions,
592
928
  currentLocation,
593
- globalDefaults,
594
- hideReadOnlyProperties,
595
- hideWriteOnlyProperties,
596
929
  prevDefaultSchemas,
597
- prevExampleSchemas,
598
- refLogger,
599
- transformer
930
+ prevExampleSchemas
600
931
  });
601
932
  }
602
933
  }
@@ -658,10 +989,10 @@ ${enums}`;
658
989
  } else if (hideWriteOnlyProperties && "writeOnly" in schema && schema.writeOnly === true) {
659
990
  return {};
660
991
  }
661
- return transformer(schema);
992
+ return schema;
662
993
  }
663
994
 
664
- // src/operation/lib/get-parameters-as-json-schema.ts
995
+ // src/operation/transformers/get-parameters-as-json-schema.ts
665
996
  var types = {
666
997
  path: "Path Params",
667
998
  query: "Query Params",
@@ -673,54 +1004,25 @@ var types = {
673
1004
  // This a special type reserved for https://npm.im/api
674
1005
  };
675
1006
  function getParametersAsJSONSchema(operation, api, opts) {
676
- let hasCircularRefs = false;
677
- let hasDiscriminatorMappingRefs = false;
678
- function refLogger(ref, type) {
679
- if (type === "ref") {
680
- hasCircularRefs = true;
681
- } else {
682
- hasDiscriminatorMappingRefs = true;
683
- }
684
- }
685
- function getDeprecated(schema, type) {
686
- if (opts?.retainDeprecatedProperties) {
687
- return null;
1007
+ const usedSchemas = /* @__PURE__ */ new Map();
1008
+ const seenRefs = /* @__PURE__ */ new Set();
1009
+ const refsByGroup = /* @__PURE__ */ new Map();
1010
+ function refLoggerForSchemaGroup(group) {
1011
+ let set = refsByGroup.get(group);
1012
+ if (!set) {
1013
+ set = /* @__PURE__ */ new Set();
1014
+ refsByGroup.set(group, set);
688
1015
  }
689
- if (!schema || !schema.properties) return null;
690
- const deprecatedBody = cloneObject(schema);
691
- const requiredParams = schema.required || [];
692
- const allDeprecatedProps = {};
693
- Object.keys(deprecatedBody.properties || {}).forEach((key) => {
694
- const deprecatedProp = deprecatedBody.properties?.[key];
695
- if (deprecatedProp.deprecated && !requiredParams.includes(key) && !deprecatedProp.readOnly) {
696
- allDeprecatedProps[key] = deprecatedProp;
697
- }
698
- });
699
- deprecatedBody.properties = allDeprecatedProps;
700
- const deprecatedSchema = toJSONSchema(deprecatedBody, {
701
- globalDefaults: opts?.globalDefaults,
702
- hideReadOnlyProperties: opts?.hideReadOnlyProperties,
703
- hideWriteOnlyProperties: opts?.hideWriteOnlyProperties,
704
- prevExampleSchemas: [],
705
- refLogger,
706
- transformer: opts?.transformer
707
- });
708
- if (Object.keys(deprecatedSchema).length === 0 || Object.keys(deprecatedSchema.properties || {}).length === 0) {
709
- return null;
710
- }
711
- Object.keys(schema.properties).forEach((key) => {
712
- if ((schema.properties?.[key]).deprecated && !requiredParams.includes(key)) {
713
- delete schema.properties?.[key];
714
- }
715
- });
716
- return {
717
- type,
718
- schema: isPrimitive(deprecatedSchema) ? deprecatedSchema : {
719
- ...deprecatedSchema,
720
- $schema: getSchemaVersionString(deprecatedSchema, api)
721
- }
722
- };
1016
+ return set;
723
1017
  }
1018
+ const baseSchemaOptions = {
1019
+ definition: api,
1020
+ globalDefaults: opts?.globalDefaults,
1021
+ hideReadOnlyProperties: opts?.hideReadOnlyProperties,
1022
+ hideWriteOnlyProperties: opts?.hideWriteOnlyProperties,
1023
+ seenRefs,
1024
+ usedSchemas
1025
+ };
724
1026
  function transformRequestBody() {
725
1027
  const requestBody = operation.getRequestBody();
726
1028
  if (!requestBody || !Array.isArray(requestBody)) return null;
@@ -734,9 +1036,12 @@ function getParametersAsJSONSchema(operation, api, opts) {
734
1036
  prevExampleSchemas.push({ example: mediaTypeObject.example });
735
1037
  } else if ("examples" in mediaTypeObject) {
736
1038
  prevExampleSchemas.push({
737
- examples: Object.values(mediaTypeObject.examples || {}).map((example) => {
1039
+ examples: Object.values(mediaTypeObject.examples || {}).map((ex) => {
1040
+ let example = ex;
1041
+ if (!example) return void 0;
738
1042
  if (isRef(example)) {
739
- return void 0;
1043
+ example = dereferenceRef(example, operation.api);
1044
+ if (!example || isRef(example)) return void 0;
740
1045
  }
741
1046
  return example.value;
742
1047
  }).filter((item) => item !== void 0)
@@ -744,12 +1049,9 @@ function getParametersAsJSONSchema(operation, api, opts) {
744
1049
  }
745
1050
  const requestSchema = cloneObject(mediaTypeObject.schema);
746
1051
  const cleanedSchema = toJSONSchema(requestSchema, {
747
- globalDefaults: opts?.globalDefaults,
748
- hideReadOnlyProperties: opts?.hideReadOnlyProperties,
749
- hideWriteOnlyProperties: opts?.hideWriteOnlyProperties,
1052
+ ...baseSchemaOptions,
750
1053
  prevExampleSchemas,
751
- refLogger,
752
- transformer: opts?.transformer
1054
+ refLogger: (ref) => refLoggerForSchemaGroup(type).add(ref)
753
1055
  });
754
1056
  if (!Object.keys(cleanedSchema).length) {
755
1057
  return null;
@@ -761,43 +1063,9 @@ function getParametersAsJSONSchema(operation, api, opts) {
761
1063
  ...cleanedSchema,
762
1064
  $schema: getSchemaVersionString(cleanedSchema, api)
763
1065
  },
764
- deprecatedProps: getDeprecated(cleanedSchema, type) ?? void 0,
765
1066
  ...description ? { description } : {}
766
1067
  };
767
1068
  }
768
- function transformComponents() {
769
- if (!("components" in api) || !api.components) {
770
- return false;
771
- }
772
- const components2 = {
773
- ...Object.keys(api.components).map((componentType) => ({ [componentType]: {} })).reduce((prev, next) => Object.assign(prev, next), {})
774
- };
775
- Object.keys(api.components).forEach((componentType) => {
776
- const cType = componentType;
777
- if (typeof api.components?.[cType] === "object" && !Array.isArray(api.components[cType])) {
778
- Object.keys(api.components?.[cType] || {}).forEach((schemaName) => {
779
- const componentSchema = cloneObject(api.components?.[cType]?.[schemaName]);
780
- if (!components2[cType]) {
781
- components2[cType] = {};
782
- }
783
- components2[cType][schemaName] = toJSONSchema(componentSchema, {
784
- globalDefaults: opts?.globalDefaults,
785
- hideReadOnlyProperties: opts?.hideReadOnlyProperties,
786
- hideWriteOnlyProperties: opts?.hideWriteOnlyProperties,
787
- refLogger,
788
- transformer: opts?.transformer
789
- });
790
- });
791
- }
792
- });
793
- Object.keys(components2).forEach((componentType) => {
794
- const cType = componentType;
795
- if (!Object.keys(components2?.[cType] || {}).length) {
796
- delete components2?.[cType];
797
- }
798
- });
799
- return components2;
800
- }
801
1069
  function transformParameters() {
802
1070
  const operationParams = operation.getParameters();
803
1071
  const transformed = Object.keys(types).map((type) => {
@@ -817,20 +1085,11 @@ function getParametersAsJSONSchema(operation, api, opts) {
817
1085
  }
818
1086
  if (current.deprecated) currentSchema.deprecated = current.deprecated;
819
1087
  const interimSchema = toJSONSchema(currentSchema, {
1088
+ ...baseSchemaOptions,
820
1089
  currentLocation: `/${current.name}`,
821
- globalDefaults: opts?.globalDefaults,
822
- hideReadOnlyProperties: opts?.hideReadOnlyProperties,
823
- hideWriteOnlyProperties: opts?.hideWriteOnlyProperties,
824
- refLogger,
825
- transformer: opts?.transformer
1090
+ refLogger: (ref) => refLoggerForSchemaGroup(type).add(ref)
826
1091
  });
827
- schema2 = isPrimitive(interimSchema) ? interimSchema : {
828
- ...interimSchema,
829
- // Note: this applies a `$schema` version to each field in the larger schema
830
- // object. It's not really **correct** but it's what we have to do because
831
- // there's a chance that the end user has indicated the schemas are different.
832
- $schema: getSchemaVersionString(currentSchema, api)
833
- };
1092
+ schema2 = isPrimitive(interimSchema) ? interimSchema : { ...interimSchema };
834
1093
  } else if ("content" in current && typeof current.content === "object") {
835
1094
  const contentKeys = Object.keys(current.content);
836
1095
  if (contentKeys.length) {
@@ -844,20 +1103,11 @@ function getParametersAsJSONSchema(operation, api, opts) {
844
1103
  }
845
1104
  if (current.deprecated) currentSchema.deprecated = current.deprecated;
846
1105
  const interimSchema = toJSONSchema(currentSchema, {
1106
+ ...baseSchemaOptions,
847
1107
  currentLocation: `/${current.name}`,
848
- globalDefaults: opts?.globalDefaults,
849
- hideReadOnlyProperties: opts?.hideReadOnlyProperties,
850
- hideWriteOnlyProperties: opts?.hideWriteOnlyProperties,
851
- refLogger,
852
- transformer: opts?.transformer
1108
+ refLogger: (ref) => refLoggerForSchemaGroup(type).add(ref)
853
1109
  });
854
- schema2 = isPrimitive(interimSchema) ? interimSchema : {
855
- ...interimSchema,
856
- // Note: this applies a `$schema` version to each field in the larger schema
857
- // object. It's not really **correct** but it's what we have to do because
858
- // there's a chance that the end user has indicated the schemas are different.
859
- $schema: getSchemaVersionString(currentSchema, api)
860
- };
1110
+ schema2 = isPrimitive(interimSchema) ? interimSchema : { ...interimSchema };
861
1111
  }
862
1112
  }
863
1113
  }
@@ -873,15 +1123,15 @@ function getParametersAsJSONSchema(operation, api, opts) {
873
1123
  return prev;
874
1124
  }, {});
875
1125
  const schema = {
1126
+ $schema: getSchemaVersionString({}, api),
876
1127
  type: "object",
877
1128
  properties,
878
- required
1129
+ ...required.length > 0 ? { required } : {}
879
1130
  };
880
1131
  return {
881
1132
  type,
882
1133
  label: types[type],
883
- schema,
884
- deprecatedProps: getDeprecated(schema, type) ?? void 0
1134
+ schema
885
1135
  };
886
1136
  }).filter((item) => item !== null);
887
1137
  if (!opts?.mergeIntoBodyAndMetadata) {
@@ -889,20 +1139,13 @@ function getParametersAsJSONSchema(operation, api, opts) {
889
1139
  } else if (!transformed.length) {
890
1140
  return [];
891
1141
  }
892
- const deprecatedProps = transformed.map((r) => r.deprecatedProps?.schema || null).filter(Boolean);
893
1142
  return [
894
1143
  {
895
1144
  type: "metadata",
896
1145
  label: types.metadata,
897
1146
  schema: {
898
1147
  allOf: transformed.map((r) => r.schema)
899
- },
900
- deprecatedProps: deprecatedProps.length ? {
901
- type: "metadata",
902
- schema: {
903
- allOf: deprecatedProps
904
- }
905
- } : void 0
1148
+ }
906
1149
  }
907
1150
  ];
908
1151
  }
@@ -913,31 +1156,59 @@ function getParametersAsJSONSchema(operation, api, opts) {
913
1156
  typeKeys[typeKeys.indexOf("form")] = "formData";
914
1157
  typeKeys.push("metadata");
915
1158
  const jsonSchema = [transformRequestBody()].concat(...transformParameters()).filter((item) => item !== null);
916
- const shouldIncludeComponents = hasCircularRefs || hasDiscriminatorMappingRefs && opts?.includeDiscriminatorMappingRefs;
917
- const components = shouldIncludeComponents ? transformComponents() : false;
1159
+ applyDiscriminatorOneOfToUsedSchemas(api, usedSchemas, (ref) => {
1160
+ if (usedSchemas.has(ref)) {
1161
+ return usedSchemas.get(ref);
1162
+ }
1163
+ try {
1164
+ const resolved = dereferenceRef({ $ref: ref }, api, seenRefs);
1165
+ if (isRef(resolved)) return void 0;
1166
+ const converted = toJSONSchema(structuredClone(resolved), {
1167
+ ...baseSchemaOptions,
1168
+ seenRefs
1169
+ });
1170
+ usedSchemas.set(ref, converted);
1171
+ return converted;
1172
+ } catch {
1173
+ return void 0;
1174
+ }
1175
+ });
918
1176
  return jsonSchema.map((group) => {
919
- if (components && shouldIncludeComponents) {
920
- group.schema.components = components;
1177
+ if (group.schema && typeof group.schema === "object") {
1178
+ const refsInGroup = refsByGroup.get(group.type) ?? /* @__PURE__ */ new Set();
1179
+ const referencedSchemas = filterRequiredRefsToReferenced(refsInGroup, usedSchemas);
1180
+ if (referencedSchemas.size > 0) {
1181
+ mergeReferencedSchemasIntoRoot(group.schema, referencedSchemas);
1182
+ }
921
1183
  }
922
- if (!group.deprecatedProps) delete group.deprecatedProps;
923
1184
  return group;
924
1185
  }).sort((a, b) => {
925
1186
  return typeKeys.indexOf(a.type) - typeKeys.indexOf(b.type);
926
1187
  });
927
1188
  }
928
1189
 
1190
+ // src/utils.ts
1191
+ var supportedMethods = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
1192
+ var SERVER_VARIABLE_REGEX = /{([-_a-zA-Z0-9:.[\]]+)}/g;
1193
+
929
1194
  export {
930
- cloneObject,
1195
+ isObject,
1196
+ isPrimitive,
1197
+ decorateComponentSchemasWithRefName,
931
1198
  decodePointer,
932
1199
  dereferenceRef,
933
1200
  getDereferencingOptions,
934
- isObject,
935
- isPrimitive,
1201
+ filterRequiredRefsToReferenced,
1202
+ mergeReferencedSchemasIntoRoot,
936
1203
  matches_mimetype_default,
937
1204
  getParameterContentType,
1205
+ cloneObject,
1206
+ applyDiscriminatorOneOfToUsedSchemas,
938
1207
  getSchemaVersionString,
939
1208
  toJSONSchema,
940
1209
  types,
941
- getParametersAsJSONSchema
1210
+ getParametersAsJSONSchema,
1211
+ supportedMethods,
1212
+ SERVER_VARIABLE_REGEX
942
1213
  };
943
- //# sourceMappingURL=chunk-5EP6HY2E.js.map
1214
+ //# sourceMappingURL=chunk-HYH37HS2.js.map