@kubb/adapter-oas 5.25.1 → 5.27.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.
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ var compatibility = {
17
17
  "node": ">=22.22"
18
18
  };
19
19
  // package.json
20
- var version = "5.25.1";
20
+ var version = "5.27.0";
21
21
  // src/constants.ts
22
22
  /**
23
23
  * Default parser options applied when no explicit options are provided.
@@ -241,29 +241,44 @@ function memo(store, key, compute) {
241
241
  store.set(key, value);
242
242
  return value;
243
243
  }
244
+ var BoundedStore = class {
245
+ #entry = /* @__PURE__ */ new Map();
246
+ #max;
247
+ constructor(max) {
248
+ this.#max = max;
249
+ }
250
+ has(key) {
251
+ return this.#entry.has(key);
252
+ }
253
+ get(key) {
254
+ return this.#entry.get(key);
255
+ }
256
+ set(key, value) {
257
+ if (this.#entry.size >= this.#max) this.#entry.clear();
258
+ this.#entry.set(key, value);
259
+ }
260
+ };
261
+ var LastEntryStore = class extends BoundedStore {
262
+ constructor() {
263
+ super(1);
264
+ }
265
+ #touch(key) {
266
+ if (!super.has(key)) return false;
267
+ this.set(key, super.get(key));
268
+ return true;
269
+ }
270
+ has(key) {
271
+ return this.#touch(key);
272
+ }
273
+ get(key) {
274
+ return this.#touch(key) ? super.get(key) : void 0;
275
+ }
276
+ };
244
277
  function createStore({ weak, max } = {}) {
245
278
  if (weak) return /* @__PURE__ */ new WeakMap();
246
- const entry = /* @__PURE__ */ new Map();
247
- if (max === void 0) return entry;
248
- const set = (key, value) => {
249
- if (entry.size >= max) entry.clear();
250
- entry.set(key, value);
251
- };
252
- if (max !== 1) return {
253
- has: (key) => entry.has(key),
254
- get: (key) => entry.get(key),
255
- set
256
- };
257
- const touch = (key) => {
258
- if (!entry.has(key)) return false;
259
- set(key, entry.get(key));
260
- return true;
261
- };
262
- return {
263
- has: touch,
264
- get: (key) => touch(key) ? entry.get(key) : void 0,
265
- set
266
- };
279
+ if (max === void 0) return /* @__PURE__ */ new Map();
280
+ if (max !== 1) return new BoundedStore(max);
281
+ return new LastEntryStore();
267
282
  }
268
283
  memo.create = createStore;
269
284
  // ../../internals/utils/src/errors.ts
@@ -530,7 +545,8 @@ function isJsonMimeType(mimeType) {
530
545
  }
531
546
  // src/refs.ts
532
547
  /**
533
- * Creates the `$ref` resolution service for one document. One pointer walk backs every method.
548
+ * The `$ref` service bound to one document: pointer resolution and resolved-node parsing, each
549
+ * with its own instance-scoped memoization.
534
550
  *
535
551
  * @example
536
552
  * ```ts
@@ -542,28 +558,32 @@ function isJsonMimeType(mimeType) {
542
558
  * refs.derefKeepingRef(parameter)
543
559
  * ```
544
560
  */
545
- function createRefs(document) {
546
- const pointerCache = /* @__PURE__ */ new Map();
547
- const resolvedNodeCache = memo.create();
548
- const resolvingRefs = /* @__PURE__ */ new Set();
549
- function walkPointer($ref) {
561
+ var Refs = class {
562
+ #document;
563
+ #pointerCache = /* @__PURE__ */ new Map();
564
+ #resolvedNodeCache = memo.create();
565
+ #resolvingRefs = /* @__PURE__ */ new Set();
566
+ constructor(document) {
567
+ this.#document = document;
568
+ }
569
+ #walkPointer($ref) {
550
570
  const trimmed = $ref.trim();
551
571
  if (trimmed === "" || !trimmed.startsWith("#")) return {
552
572
  applicable: false,
553
573
  value: null
554
574
  };
555
575
  const pointer = trimmed.substring(1);
556
- if (pointerCache.has(pointer)) return {
576
+ if (this.#pointerCache.has(pointer)) return {
557
577
  applicable: true,
558
- value: pointerCache.get(pointer)
578
+ value: this.#pointerCache.get(pointer)
559
579
  };
560
580
  let current;
561
581
  try {
562
- current = pointer.split("/").filter(Boolean).map((token) => unescapePointerToken(globalThis.decodeURIComponent(token))).reduce((obj, key) => obj != null && Object.hasOwn(obj, key) ? obj[key] : void 0, document);
582
+ current = pointer.split("/").filter(Boolean).map((token) => unescapePointerToken(globalThis.decodeURIComponent(token))).reduce((obj, key) => obj != null && Object.hasOwn(obj, key) ? obj[key] : void 0, this.#document);
563
583
  } catch {
564
584
  current = void 0;
565
585
  }
566
- if (current) pointerCache.set(pointer, current);
586
+ if (current) this.#pointerCache.set(pointer, current);
567
587
  return {
568
588
  applicable: true,
569
589
  value: current ?? null
@@ -575,8 +595,8 @@ function createRefs(document) {
575
595
  * outside one) when the pointer cannot be resolved. `report: false` resolves to `null` silently,
576
596
  * for a speculative lookup where a missing ref is not an error.
577
597
  */
578
- function resolve(refPath, options) {
579
- const { applicable, value } = walkPointer(refPath);
598
+ resolve(refPath, options) {
599
+ const { applicable, value } = this.#walkPointer(refPath);
580
600
  if (!applicable || value || options?.report === false) return value;
581
601
  const diagnostic = {
582
602
  code: Diagnostics.code.refNotFound,
@@ -598,22 +618,22 @@ function createRefs(document) {
598
618
  * resolved (e.g. a minimal document in a unit test). An error from `parse` is thrown, so a bug
599
619
  * in a converter is not mistaken for a missing ref.
600
620
  */
601
- function resolveNode(refPath, parse) {
602
- if (resolvingRefs.has(refPath)) return null;
603
- return memo(resolvedNodeCache, refPath, () => {
621
+ resolveNode(refPath, parse) {
622
+ if (this.#resolvingRefs.has(refPath)) return null;
623
+ return memo(this.#resolvedNodeCache, refPath, () => {
604
624
  let referenced;
605
625
  try {
606
- referenced = resolve(refPath);
626
+ referenced = this.resolve(refPath);
607
627
  } catch (error) {
608
628
  if (Diagnostics.isError(error) && error.diagnostic.code === Diagnostics.code.refNotFound) return null;
609
629
  throw error;
610
630
  }
611
631
  if (!referenced) return null;
612
- resolvingRefs.add(refPath);
632
+ this.#resolvingRefs.add(refPath);
613
633
  try {
614
634
  return parse({ schema: referenced });
615
635
  } finally {
616
- resolvingRefs.delete(refPath);
636
+ this.#resolvingRefs.delete(refPath);
617
637
  }
618
638
  });
619
639
  }
@@ -623,26 +643,28 @@ function createRefs(document) {
623
643
  * `$ref` after resolving (e.g. a document with no component registry). A non-`$ref` value is
624
644
  * returned as-is.
625
645
  */
626
- function deref(value) {
646
+ deref(value) {
627
647
  if (!isReference(value)) return value ? value : null;
628
- const resolved = resolve(value.$ref);
648
+ const resolved = this.resolve(value.$ref);
629
649
  return resolved && !isReference(resolved) ? resolved : null;
630
650
  }
631
- /** Resolves a `$ref` object but keeps the `$ref` field on the result; other values pass through. */
632
- function derefKeepingRef(value) {
651
+ /**
652
+ * Resolves a `$ref` object but keeps the `$ref` field on the result; other values pass through.
653
+ */
654
+ derefKeepingRef(value) {
633
655
  if (isReference(value)) return {
634
656
  ...value,
635
- ...resolve(value.$ref),
657
+ ...this.resolve(value.$ref),
636
658
  $ref: value.$ref
637
659
  };
638
660
  return value;
639
661
  }
640
- return {
641
- resolve,
642
- resolveNode,
643
- deref,
644
- derefKeepingRef
645
- };
662
+ };
663
+ /**
664
+ * Creates the `$ref` resolution service for one document. One pointer walk backs every method.
665
+ */
666
+ function createRefs(document) {
667
+ return new Refs(document);
646
668
  }
647
669
  // src/load/schemaIds.ts
648
670
  const DATA_KEYS = /* @__PURE__ */ new Set([
@@ -783,11 +805,69 @@ function indexEvents(events) {
783
805
  collections: /* @__PURE__ */ new Map()
784
806
  };
785
807
  }
786
- function createLocator({ index, source }) {
787
- const { types, styles, starts, ends, collections } = index;
788
- const isCollection = (at) => types[at] === EVENT_ID.MAPPING || types[at] === EVENT_ID.SEQUENCE;
789
- const readText = (at) => {
790
- const text = source.slice(starts[at], ends[at]);
808
+ var SourceLocator = class {
809
+ #index;
810
+ #source;
811
+ #types;
812
+ #styles;
813
+ #starts;
814
+ #ends;
815
+ constructor({ index, source }) {
816
+ this.#index = index;
817
+ this.#source = source;
818
+ this.#types = index.types;
819
+ this.#styles = index.styles;
820
+ this.#starts = index.starts;
821
+ this.#ends = index.ends;
822
+ }
823
+ find(pointer) {
824
+ return this.#walk({
825
+ at: 1,
826
+ tokens: pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken),
827
+ range: void 0
828
+ });
829
+ }
830
+ findRef(ref) {
831
+ this.#index.refs ??= this.#indexRefs();
832
+ return this.#index.refs.get(ref);
833
+ }
834
+ #indexRefs() {
835
+ const types = this.#types;
836
+ const refs = /* @__PURE__ */ new Map();
837
+ const frames = [];
838
+ for (let at = 1; at + 1 < types.length; at++) {
839
+ if (!this.#stepFrames({
840
+ frames,
841
+ at
842
+ }) || !this.#isRefKey(at)) continue;
843
+ const text = this.#readText(at + 1);
844
+ if (!refs.has(text)) refs.set(text, {
845
+ start: this.#starts[at + 1],
846
+ end: this.#ends[at + 1]
847
+ });
848
+ }
849
+ return refs;
850
+ }
851
+ #stepFrames({ frames, at }) {
852
+ if (this.#types[at] === EVENT_ID.POP) {
853
+ frames.pop();
854
+ return false;
855
+ }
856
+ const top = frames.length - 1;
857
+ const isKey = frames[top] === 0;
858
+ if (frames[top] >= 0) frames[top] = 1 - frames[top];
859
+ if (this.#isCollection(at)) frames.push(this.#types[at] === EVENT_ID.MAPPING ? 0 : -1);
860
+ return isKey;
861
+ }
862
+ #isRefKey(at) {
863
+ return this.#types[at] === EVENT_ID.SCALAR && this.#types[at + 1] === EVENT_ID.SCALAR && this.#readText(at) === "$ref";
864
+ }
865
+ #isCollection(at) {
866
+ return this.#types[at] === EVENT_ID.MAPPING || this.#types[at] === EVENT_ID.SEQUENCE;
867
+ }
868
+ #readText(at) {
869
+ const styles = this.#styles;
870
+ const text = this.#source.slice(this.#starts[at], this.#ends[at]);
791
871
  if (styles[at] === SCALAR_STYLE.SINGLE_QUOTED) return text.slice(1, -1).replaceAll("''", "'");
792
872
  if (styles[at] !== SCALAR_STYLE.DOUBLE_QUOTED) return text;
793
873
  try {
@@ -795,19 +875,20 @@ function createLocator({ index, source }) {
795
875
  } catch {
796
876
  return String(load(text, { schema: CORE_SCHEMA }));
797
877
  }
798
- };
799
- const readNode = (at) => {
800
- if (!isCollection(at)) return {
878
+ }
879
+ #readNode(at) {
880
+ const starts = this.#starts;
881
+ if (!this.#isCollection(at)) return {
801
882
  start: starts[at],
802
- end: ends[at],
883
+ end: this.#ends[at],
803
884
  next: at + 1
804
885
  };
805
886
  let next = at + 1;
806
887
  let end = starts[at];
807
- while (types[next] !== EVENT_ID.POP) ({end, next} = readNode(next));
808
- if (styles[at] === COLLECTION_STYLE.FLOW) {
888
+ while (this.#types[next] !== EVENT_ID.POP) ({end, next} = this.#readNode(next));
889
+ if (this.#styles[at] === COLLECTION_STYLE.FLOW) {
809
890
  flowGap.lastIndex = end;
810
- flowGap.exec(source);
891
+ flowGap.exec(this.#source);
811
892
  end = flowGap.lastIndex + 1;
812
893
  }
813
894
  return {
@@ -815,15 +896,16 @@ function createLocator({ index, source }) {
815
896
  end,
816
897
  next: next + 1
817
898
  };
818
- };
819
- const readChildren = (collection) => {
899
+ }
900
+ #readChildren(collection) {
901
+ const types = this.#types;
820
902
  const keys = /* @__PURE__ */ new Map();
821
903
  const items = [];
822
904
  const isMapping = types[collection] === EVENT_ID.MAPPING;
823
905
  for (let at = collection + 1; types[at] !== EVENT_ID.POP;) {
824
906
  const key = isMapping ? at : void 0;
825
- if (key !== void 0) at = readNode(at).next;
826
- const { start, end, next } = readNode(at);
907
+ if (key !== void 0) at = this.#readNode(at).next;
908
+ const { start, end, next } = this.#readNode(at);
827
909
  if (key === void 0) items.push({
828
910
  index: at,
829
911
  range: {
@@ -831,13 +913,12 @@ function createLocator({ index, source }) {
831
913
  end
832
914
  }
833
915
  });
834
- const raw = key !== void 0 && types[key] === EVENT_ID.SCALAR ? readText(key) : void 0;
835
- const text = raw !== void 0 && styles[key] === SCALAR_STYLE.PLAIN ? toKeyText(raw) : raw;
916
+ const text = key !== void 0 ? this.#readKey(key) : void 0;
836
917
  if (text !== void 0 && !keys.has(text)) keys.set(text, {
837
918
  index: at,
838
919
  range: {
839
- start: starts[key],
840
- end: ends[key]
920
+ start: this.#starts[key],
921
+ end: this.#ends[key]
841
922
  }
842
923
  });
843
924
  at = next;
@@ -846,50 +927,26 @@ function createLocator({ index, source }) {
846
927
  keys,
847
928
  items
848
929
  };
849
- };
850
- const walk = ({ at, tokens: [token, ...rest], range }) => {
930
+ }
931
+ #readKey(key) {
932
+ if (this.#types[key] !== EVENT_ID.SCALAR) return void 0;
933
+ const raw = this.#readText(key);
934
+ return this.#styles[key] === SCALAR_STYLE.PLAIN ? toKeyText(raw) : raw;
935
+ }
936
+ #walk({ at, tokens: [token, ...rest], range }) {
851
937
  if (token === void 0) return range;
852
- if (!isCollection(at)) return void 0;
853
- const { keys, items } = memo(collections, at, () => readChildren(at));
938
+ if (!this.#isCollection(at)) return void 0;
939
+ const { keys, items } = memo(this.#index.collections, at, () => this.#readChildren(at));
854
940
  const child = keys.get(token) ?? items[Number(token)];
855
- return child ? walk({
941
+ return child ? this.#walk({
856
942
  at: child.index,
857
943
  tokens: rest,
858
944
  range: child.range
859
945
  }) : void 0;
860
- };
861
- return {
862
- find(pointer) {
863
- return walk({
864
- at: 1,
865
- tokens: pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken),
866
- range: void 0
867
- });
868
- },
869
- findRef(ref) {
870
- if (!index.refs) {
871
- index.refs = /* @__PURE__ */ new Map();
872
- const frames = [];
873
- for (let at = 1; at + 1 < types.length; at++) {
874
- if (types[at] === EVENT_ID.POP) {
875
- frames.pop();
876
- continue;
877
- }
878
- const top = frames.length - 1;
879
- const isKey = frames[top] === 0;
880
- if (frames[top] >= 0) frames[top] = 1 - frames[top];
881
- if (isCollection(at)) frames.push(types[at] === EVENT_ID.MAPPING ? 0 : -1);
882
- if (!isKey || types[at] !== EVENT_ID.SCALAR || types[at + 1] !== EVENT_ID.SCALAR || readText(at) !== "$ref") continue;
883
- const text = readText(at + 1);
884
- if (!index.refs.has(text)) index.refs.set(text, {
885
- start: starts[at + 1],
886
- end: ends[at + 1]
887
- });
888
- }
889
- }
890
- return index.refs.get(ref);
891
- }
892
- };
946
+ }
947
+ };
948
+ function createSourceLocator(options) {
949
+ return new SourceLocator(options);
893
950
  }
894
951
  function parse(source) {
895
952
  try {
@@ -908,12 +965,12 @@ const locate = (source, location) => {
908
965
  if (!("pointer" in location)) return void 0;
909
966
  const index = memo(indexes, source, () => parse(source));
910
967
  if (!index) return void 0;
911
- const { find, findRef } = createLocator({
968
+ const locator = createSourceLocator({
912
969
  index,
913
970
  source
914
971
  });
915
972
  const ref = "ref" in location ? location.ref : void 0;
916
- return find(location.pointer) ?? (ref ? findRef(ref) : void 0);
973
+ return locator.find(location.pointer) ?? (ref ? locator.findRef(ref) : void 0);
917
974
  };
918
975
  // src/load/source.ts
919
976
  const urlRegExp = /^https?:\/+/i;
@@ -1381,6 +1438,7 @@ function reportValidationErrors({ errors, additionalErrors }) {
1381
1438
  for (const { message } of errors) reportInvalidSpec(message.replace(/\n.*/s, ""));
1382
1439
  if (additionalErrors > 0) reportInvalidSpec(`${additionalErrors} more validation errors are not listed.`);
1383
1440
  }
1441
+ const VALIDATION_WORKER_LIMITS = { maxYoungGenerationSizeMb: 4 };
1384
1442
  /**
1385
1443
  * Validates like {@link validateDocument} without `throwOnError`, but in a worker thread, so the
1386
1444
  * build keeps generating while `@readme/openapi-parser` dereferences and checks the spec. Await the
@@ -1393,7 +1451,10 @@ async function validateDocumentInWorker(document) {
1393
1451
  try {
1394
1452
  const { Worker } = await import("node:worker_threads");
1395
1453
  const workerFile = import.meta.url.endsWith(".ts") ? "./validateWorker.ts" : "./validateWorker.js";
1396
- const worker = new Worker(new URL(workerFile, import.meta.url), { workerData: document });
1454
+ const worker = new Worker(new URL(workerFile, import.meta.url), {
1455
+ workerData: document,
1456
+ resourceLimits: VALIDATION_WORKER_LIMITS
1457
+ });
1397
1458
  message = await new Promise((resolve, reject) => {
1398
1459
  worker.once("message", resolve);
1399
1460
  worker.once("error", reject);
@@ -1999,25 +2060,31 @@ function flattenSchema(schema) {
1999
2060
  function scanSchema(node, pointer = node.pointer) {
2000
2061
  if (pointer) visit(node, pointer);
2001
2062
  }
2002
- let formatTally;
2063
+ var FormatTally = class {
2064
+ #pointers;
2065
+ group(fn) {
2066
+ this.#pointers = memo.create();
2067
+ try {
2068
+ return fn();
2069
+ } finally {
2070
+ const tally = this.#pointers;
2071
+ this.#pointers = void 0;
2072
+ for (const [format, pointers] of tally) reportUnsupportedFormat(format, [...pointers]);
2073
+ }
2074
+ }
2075
+ add(format, pointer) {
2076
+ if (!this.#pointers) return reportUnsupportedFormat(format, [pointer]);
2077
+ memo(this.#pointers, format, () => /* @__PURE__ */ new Set()).add(pointer);
2078
+ }
2079
+ };
2080
+ const formatTally = new FormatTally();
2003
2081
  /**
2004
2082
  * Runs `fn` and reports `KUBB_UNSUPPORTED_FORMAT` once per format with the number of schemas that use it,
2005
2083
  * instead of once per schema. `format` is an open value in OpenAPI, so a large spec would otherwise print
2006
2084
  * thousands of warnings that say the same thing. `fn` must be synchronous.
2007
2085
  */
2008
2086
  function groupUnsupportedFormats(fn) {
2009
- formatTally = memo.create();
2010
- try {
2011
- return fn();
2012
- } finally {
2013
- const tally = formatTally;
2014
- formatTally = void 0;
2015
- for (const [format, pointers] of tally) reportUnsupportedFormat(format, [...pointers]);
2016
- }
2017
- }
2018
- function tallyFormat(format, pointer) {
2019
- if (!formatTally) return reportUnsupportedFormat(format, [pointer]);
2020
- memo(formatTally, format, () => /* @__PURE__ */ new Set()).add(pointer);
2087
+ return formatTally.group(fn);
2021
2088
  }
2022
2089
  function reportUnsupportedFormat(format, pointers) {
2023
2090
  const count = pointers.length;
@@ -2043,7 +2110,7 @@ function visit(node, pointer) {
2043
2110
  pointer
2044
2111
  }
2045
2112
  });
2046
- if (typeof node.format === "string" && !isHandledFormat(node.format)) tallyFormat(node.format, pointer);
2113
+ if (typeof node.format === "string" && !isHandledFormat(node.format)) formatTally.add(node.format, pointer);
2047
2114
  if (node.type === "object") {
2048
2115
  for (const property of node.properties) visit(property.schema, `${pointer}/properties/${escapePointerToken(property.name)}`);
2049
2116
  if (node.additionalProperties && typeof node.additionalProperties === "object") visit(node.additionalProperties, `${pointer}/additionalProperties`);
@@ -2386,10 +2453,15 @@ function parseParameter({ param, pointer, parentName, options, parseSchema, refs
2386
2453
  refs
2387
2454
  });
2388
2455
  const rawSchema = param.schema ?? mediaType?.schema;
2389
- const schema = rawSchema ? parseSchema({
2456
+ const parsed = rawSchema ? parseSchema({
2390
2457
  schema: rawSchema,
2391
2458
  name: schemaName
2392
2459
  }) : ast.factory.createSchema({ type: options.unknownType });
2460
+ const schema = parentName && param.name ? ast.macro.apply(parsed, [macroEnumName({
2461
+ parentName,
2462
+ propName: param.name,
2463
+ enumSuffix: options.enumSuffix
2464
+ })], { depth: "shallow" }) : parsed;
2393
2465
  if (rawSchema) scanSchema(schema, contentType ? `${pointer}/content/${escapePointerToken(contentType)}/schema` : `${pointer}/schema`);
2394
2466
  return ast.factory.createParameter({
2395
2467
  pointer,
@@ -3500,6 +3572,17 @@ function convertFormat(context) {
3500
3572
  });
3501
3573
  }
3502
3574
  /**
3575
+ * Primitive of an enum's members. Without a `type`, the values decide: `{ enum: [0, 1] }` is a number enum.
3576
+ */
3577
+ function getEnumPrimitive(type, values) {
3578
+ if (type === "boolean") return "boolean";
3579
+ if (type === "number" || type === "integer") return "number";
3580
+ if (type !== void 0 || values.length === 0) return "string";
3581
+ if (values.every((value) => typeof value === "boolean")) return "boolean";
3582
+ if (values.every((value) => typeof value === "number")) return "number";
3583
+ return "string";
3584
+ }
3585
+ /**
3503
3586
  * Converts an `enum` schema into an `EnumSchemaNode`.
3504
3587
  */
3505
3588
  function convertEnum({ schema, name, nullable, type, parse }) {
@@ -3513,8 +3596,8 @@ function convertEnum({ schema, name, nullable, type, parse }) {
3513
3596
  if (nullInEnum && filteredValues.length === 0) return createNullNode(schema, name);
3514
3597
  const enumNullable = nullable || nullInEnum || void 0;
3515
3598
  const enumDefault = schema.default === null && enumNullable ? void 0 : schema.default;
3516
- const primitive = type === "boolean" ? "boolean" : type === "number" || type === "integer" ? "number" : "string";
3517
3599
  const uniqueValues = [...new Set(filteredValues)];
3600
+ const primitive = getEnumPrimitive(type, uniqueValues);
3518
3601
  const ctx = {
3519
3602
  schema,
3520
3603
  name,
@@ -3532,16 +3615,18 @@ function convertEnum({ schema, name, nullable, type, parse }) {
3532
3615
  const rawEnumNames = extensionKey ? memberSlots(schema[extensionKey]) : void 0;
3533
3616
  const rawEnumDescriptions = descriptionKey ? memberSlots(schema[descriptionKey]) : void 0;
3534
3617
  const seenNames = /* @__PURE__ */ new Set();
3618
+ const seenValues = /* @__PURE__ */ new Set();
3535
3619
  return createNode(ctx, {
3536
3620
  type: "enum",
3537
3621
  primitive,
3538
- namedEnumValues: uniqueValues.map((value, index) => ({
3622
+ namedEnumValues: filteredValues.map((value, index) => ({
3539
3623
  name: String(rawEnumNames?.[index] ?? value),
3540
3624
  value,
3541
3625
  primitive,
3542
3626
  description: rawEnumDescriptions?.[index]
3543
3627
  })).filter((entry) => {
3544
- if (seenNames.has(entry.name)) return false;
3628
+ if (seenValues.has(entry.value) || seenNames.has(entry.name)) return false;
3629
+ seenValues.add(entry.value);
3545
3630
  seenNames.add(entry.name);
3546
3631
  return true;
3547
3632
  })
@@ -3831,7 +3916,7 @@ const schemaRules = [
3831
3916
  },
3832
3917
  {
3833
3918
  match: (ctx) => ctx.type !== "array" && !!ctx.schema.enum?.some((value) => value !== null) && matchesFormat(ctx) && formatPrintsPlainType(ctx),
3834
- convert: convertFormattedEnum
3919
+ convert: (ctx) => enumExtensionKeys.some((key) => key in ctx.schema) ? convertEnum(ctx) : convertFormattedEnum(ctx)
3835
3920
  },
3836
3921
  {
3837
3922
  match: matchesFormat,
@@ -3893,7 +3978,7 @@ const schemaRules = [
3893
3978
  convert: ({ schema, name, nullable }) => createNullNode(schema, name, nullable)
3894
3979
  }
3895
3980
  ];
3896
- // src/parser.ts
3981
+ // src/schemaParser.ts
3897
3982
  /**
3898
3983
  * `unevaluated*` also sees what composition siblings evaluate, so with such a sibling it is dropped to keep the schema open.
3899
3984
  */
@@ -3908,20 +3993,35 @@ function hasUnevaluatedSiblings(schema) {
3908
3993
  schema.$ref
3909
3994
  ].some((value) => value !== void 0);
3910
3995
  }
3996
+ function isKeyList(value) {
3997
+ return Array.isArray(value) && value.length > 0 && value.every((name) => typeof name === "string");
3998
+ }
3911
3999
  /**
3912
- * Creates the schema converter bound to one OpenAPI document: `parseSchema` dispatches each
3913
- * schema through the ordered `schemaRules` table, with the parser options merged once.
4000
+ * The schema converter bound to one OpenAPI document: `parse` dispatches each schema through the
4001
+ * ordered `schemaRules` table, with the parser options merged once.
3914
4002
  *
3915
4003
  * @internal
3916
4004
  */
3917
- function createSchemaParser(ctx) {
3918
- const { document, refs, renames } = ctx;
3919
- const options = {
3920
- ...DEFAULT_PARSER_OPTIONS,
3921
- ...ctx.options
3922
- };
3923
- /** Converts one OpenAPI schema into a `SchemaNode`, through the first matching rule of `schemaRules`. */
3924
- function parseSchema({ schema, name }) {
4005
+ var SchemaParser = class {
4006
+ #document;
4007
+ #refs;
4008
+ #renames;
4009
+ #options;
4010
+ #presenceKeys = /* @__PURE__ */ new WeakMap();
4011
+ constructor(ctx) {
4012
+ this.#document = ctx.document;
4013
+ this.#refs = ctx.refs;
4014
+ this.#renames = ctx.renames;
4015
+ this.#options = {
4016
+ ...DEFAULT_PARSER_OPTIONS,
4017
+ ...ctx.options
4018
+ };
4019
+ }
4020
+ /**
4021
+ * Converts one OpenAPI schema into a `SchemaNode`, through the first matching rule of `schemaRules`.
4022
+ */
4023
+ parse = ({ schema, name }) => {
4024
+ const options = this.#options;
3925
4025
  if (typeof schema === "boolean") return ast.factory.createSchema({
3926
4026
  type: schema ? options.emptySchemaType : "never",
3927
4027
  name: name ?? void 0
@@ -3929,19 +4029,26 @@ function createSchemaParser(ctx) {
3929
4029
  const flattenedSchema = flattenSchema(schema);
3930
4030
  if (flattenedSchema !== schema) {
3931
4031
  const { if: _if, then: _then, else: _else, dependentRequired: _required, dependentSchemas: _schemas, ...shape } = flattenedSchema;
3932
- return withConditionals(parseSchema({
4032
+ return this.#withConditionals(this.parse({
3933
4033
  schema: shape,
3934
4034
  name
3935
4035
  }), [schema, ...schema.allOf]);
3936
4036
  }
3937
4037
  if (hasUnevaluatedSiblings(schema)) {
3938
4038
  const { unevaluatedProperties: _properties, unevaluatedItems: _items, ...open } = schema;
3939
- return parseSchema({
4039
+ return this.parse({
3940
4040
  schema: open,
3941
4041
  name
3942
4042
  });
3943
4043
  }
3944
- const nullable = isNullable(schema, document) || void 0;
4044
+ return this.#convert({
4045
+ schema,
4046
+ name
4047
+ });
4048
+ };
4049
+ #convert({ schema, name }) {
4050
+ const options = this.#options;
4051
+ const nullable = isNullable(schema, this.#document) || void 0;
3945
4052
  const context = {
3946
4053
  schema,
3947
4054
  name,
@@ -3949,16 +4056,17 @@ function createSchemaParser(ctx) {
3949
4056
  defaultValue: schema.default === null && nullable ? void 0 : schema.default,
3950
4057
  type: Array.isArray(schema.type) ? schema.type.find((t) => t !== "null") ?? schema.type[0] : schema.type,
3951
4058
  options,
3952
- parse: parseSchema,
3953
- document,
3954
- refs,
3955
- renames
4059
+ parse: this.parse,
4060
+ document: this.#document,
4061
+ refs: this.#refs,
4062
+ renames: this.#renames
3956
4063
  };
3957
4064
  const rule = schemaRules.find((entry) => entry.match(context));
3958
- return withConditionals(rule ? rule.convert(context) : createNode(context, { type: options.emptySchemaType }), [schema, ...Array.isArray(schema.allOf) ? schema.allOf.filter(isAnnotationMember) : []]);
4065
+ const node = rule ? rule.convert(context) : createNode(context, { type: options.emptySchemaType });
4066
+ const annotationMembers = Array.isArray(schema.allOf) ? schema.allOf.filter(isAnnotationMember) : [];
4067
+ return this.#withConditionals(node, [schema, ...annotationMembers]);
3959
4068
  }
3960
- const presenceKeys = /* @__PURE__ */ new WeakMap();
3961
- function requireKeys(keys) {
4069
+ #requireKeys(keys) {
3962
4070
  const unknown = ast.factory.createSchema({ type: ast.schema.types.unknown });
3963
4071
  const node = ast.factory.createSchema({
3964
4072
  type: ast.schema.types.object,
@@ -3969,17 +4077,14 @@ function createSchemaParser(ctx) {
3969
4077
  schema: unknown
3970
4078
  }))
3971
4079
  });
3972
- presenceKeys.set(node, keys.join("\0"));
4080
+ this.#presenceKeys.set(node, keys.join("\0"));
3973
4081
  return node;
3974
4082
  }
3975
- /**
3976
- * Adds `if`/`then`/`else`, `dependentRequired` and `dependentSchemas` to the node as `conditionals`.
3977
- */
3978
- function withConditionals(node, sources) {
3979
- const conditionals = sources.flatMap(toConditionals);
3980
- const existing = new Set((node.conditionals ?? []).map(presenceKey));
4083
+ #withConditionals(node, sources) {
4084
+ const conditionals = sources.flatMap((source) => this.#toConditionals(source));
4085
+ const existing = new Set((node.conditionals ?? []).map((entry) => this.#presenceKey(entry)));
3981
4086
  const added = conditionals.filter((entry) => {
3982
- const key = presenceKey(entry);
4087
+ const key = this.#presenceKey(entry);
3983
4088
  return key === void 0 || !existing.has(key);
3984
4089
  });
3985
4090
  return added.length ? {
@@ -3987,50 +4092,54 @@ function createSchemaParser(ctx) {
3987
4092
  conditionals: [...node.conditionals ?? [], ...added]
3988
4093
  } : node;
3989
4094
  }
3990
- /**
3991
- * The keys an entry checks for presence, or `undefined` when a branch checks more than presence and the entry is always kept.
3992
- */
3993
- function presenceKey({ if: condition, then: thenNode, else: elseNode }) {
4095
+ #presenceKey({ if: condition, then: thenNode, else: elseNode }) {
3994
4096
  const keys = [
3995
4097
  condition,
3996
4098
  thenNode,
3997
4099
  elseNode
3998
- ].map((branch) => branch ? presenceKeys.get(branch) : "");
4100
+ ].map((branch) => branch ? this.#presenceKeys.get(branch) : "");
3999
4101
  return keys.includes(void 0) ? void 0 : keys.join("\n");
4000
4102
  }
4001
- function toConditionals(schema) {
4002
- const branch = (value) => {
4003
- if (!value || typeof value !== "object") return void 0;
4004
- const { required, type, ...rest } = value;
4005
- if (Array.isArray(required) && required.length && (type === void 0 || type === "object") && !Object.keys(rest).length) return requireKeys(required);
4006
- return parseSchema({ schema: value });
4007
- };
4103
+ #toBranch(value) {
4104
+ if (!value || typeof value !== "object") return void 0;
4105
+ const { required, type, ...rest } = value;
4106
+ if (Array.isArray(required) && required.length && (type === void 0 || type === "object") && !Object.keys(rest).length) return this.#requireKeys(required);
4107
+ return this.parse({ schema: value });
4108
+ }
4109
+ #toConditionals(schema) {
4008
4110
  const entries = (value) => value && typeof value === "object" ? Object.entries(value) : [];
4009
4111
  const conditionals = [];
4010
- const condition = branch(schema.if);
4112
+ const condition = this.#toBranch(schema.if);
4011
4113
  if (condition) {
4012
- const thenNode = branch(schema.then);
4013
- const elseNode = branch(schema.else);
4114
+ const thenNode = this.#toBranch(schema.then);
4115
+ const elseNode = this.#toBranch(schema.else);
4014
4116
  conditionals.push({
4015
4117
  if: condition,
4016
4118
  ...thenNode && { then: thenNode },
4017
4119
  ...elseNode && { else: elseNode }
4018
4120
  });
4019
4121
  }
4020
- for (const [key, required] of entries(schema.dependentRequired)) if (Array.isArray(required) && required.length && required.every((name) => typeof name === "string")) conditionals.push({
4021
- if: requireKeys([key]),
4022
- then: requireKeys(required)
4122
+ for (const [key, required] of entries(schema.dependentRequired)) if (isKeyList(required)) conditionals.push({
4123
+ if: this.#requireKeys([key]),
4124
+ then: this.#requireKeys(required)
4023
4125
  });
4024
4126
  for (const [key, dependent] of entries(schema.dependentSchemas)) {
4025
- const thenNode = branch(dependent);
4127
+ const thenNode = this.#toBranch(dependent);
4026
4128
  if (thenNode) conditionals.push({
4027
- if: requireKeys([key]),
4129
+ if: this.#requireKeys([key]),
4028
4130
  then: thenNode
4029
4131
  });
4030
4132
  }
4031
4133
  return conditionals;
4032
4134
  }
4033
- return parseSchema;
4135
+ };
4136
+ /**
4137
+ * Creates the schema converter bound to one OpenAPI document.
4138
+ *
4139
+ * @internal
4140
+ */
4141
+ function createSchemaParser(ctx) {
4142
+ return new SchemaParser(ctx);
4034
4143
  }
4035
4144
  // src/promoteEnums.ts
4036
4145
  /**
@@ -4083,6 +4192,237 @@ function refPromotedEnums(node, promoted) {
4083
4192
  * The `name` of `@kubb/adapter-oas`, used to identify this adapter in a Kubb config.
4084
4193
  */
4085
4194
  const adapterOasName = "oas";
4195
+ var Adapter = class {
4196
+ locate = locate;
4197
+ #settings;
4198
+ #parserOptions;
4199
+ #document = null;
4200
+ #sources = [];
4201
+ #pendingValidations = [];
4202
+ #inputCache = /* @__PURE__ */ new WeakMap();
4203
+ constructor({ validate, contentType, server, discriminator, enums, pathParamsCasing, ...parserOptions }) {
4204
+ this.#settings = {
4205
+ validate,
4206
+ contentType,
4207
+ server,
4208
+ discriminator,
4209
+ enums,
4210
+ pathParamsCasing
4211
+ };
4212
+ this.#parserOptions = parserOptions;
4213
+ }
4214
+ get document() {
4215
+ return this.#document;
4216
+ }
4217
+ get sources() {
4218
+ return this.#sources;
4219
+ }
4220
+ validate = async (input, options) => {
4221
+ const document = await parseFromConfig({
4222
+ type: "path",
4223
+ path: input
4224
+ }, [], { fileRefs: options?.fileRefs });
4225
+ assertDocument(document);
4226
+ await validateDocument(document, options);
4227
+ };
4228
+ settle = async () => {
4229
+ await Promise.all(this.#pendingValidations.splice(0));
4230
+ };
4231
+ parse = async (source, { signal, fileRefs } = {}) => {
4232
+ signal?.throwIfAborted();
4233
+ const cached = this.#inputCache.get(source);
4234
+ if (cached) {
4235
+ this.#sources = cached.sources;
4236
+ this.#document = cached.document;
4237
+ return cached.promise;
4238
+ }
4239
+ const read = [];
4240
+ this.#sources = read;
4241
+ const entry = {
4242
+ sources: read,
4243
+ document: null
4244
+ };
4245
+ const promise = (async () => {
4246
+ const document = await parseFromConfig(source, read, {
4247
+ fileRefs,
4248
+ signal
4249
+ });
4250
+ signal?.throwIfAborted();
4251
+ assertDocument(document);
4252
+ if (this.#settings.validate) this.#pendingValidations.push(validateDocumentInWorker(document));
4253
+ entry.document = document;
4254
+ this.#document = document;
4255
+ const refs = createRefs(document);
4256
+ const { schemas, renames, pointers } = getSchemas(document, { contentType: this.#settings.contentType }, refs);
4257
+ const parseSchema = createSchemaParser({
4258
+ document,
4259
+ refs,
4260
+ renames,
4261
+ options: this.#parserOptions
4262
+ }).parse;
4263
+ return groupUnsupportedFormats(() => this.#parseInput({
4264
+ document,
4265
+ refs,
4266
+ schemas,
4267
+ pointers,
4268
+ parseSchema
4269
+ }));
4270
+ })();
4271
+ this.#inputCache.set(source, Object.assign(entry, { promise }));
4272
+ return promise.catch((error) => {
4273
+ if (signal?.aborted) this.#inputCache.delete(source);
4274
+ throw error;
4275
+ });
4276
+ };
4277
+ /**
4278
+ * Parses every schema and operation once, resolving ref aliases and discriminator children from that same pass.
4279
+ */
4280
+ #parseInput({ document, refs, schemas, pointers, parseSchema }) {
4281
+ const { contentType, server, enums, pathParamsCasing } = this.#settings;
4282
+ const parserOptions = this.#parserOptions;
4283
+ const parsed = this.#parseSchemas({
4284
+ schemas,
4285
+ pointers,
4286
+ parseSchema
4287
+ });
4288
+ const operationNodes = getOperations(document, refs).map((operation) => {
4289
+ const node = parseOperation({
4290
+ operation,
4291
+ document,
4292
+ refs,
4293
+ contentType,
4294
+ options: parserOptions,
4295
+ parseSchema
4296
+ });
4297
+ return pathParamsCasing === "camelcase" ? camelCasePathParams(node) : node;
4298
+ });
4299
+ const promotedEnums = enums === "root" ? collectInlineEnums([...parsed.parsedByName.values(), ...operationNodes], new Set(Object.keys(schemas))) : null;
4300
+ const schemaNodes = resolveSchemaNodes({
4301
+ names: Object.keys(schemas),
4302
+ parsed,
4303
+ promotedEnums
4304
+ });
4305
+ const operations = promotedEnums ? operationNodes.map((node) => refPromotedEnums(node, promotedEnums)) : operationNodes;
4306
+ const authSchemes = toAuthSchemes(document.components?.securitySchemes, refs);
4307
+ return ast.factory.createInput({
4308
+ schemas: schemaNodes,
4309
+ operations,
4310
+ meta: toInputMeta({
4311
+ document,
4312
+ server,
4313
+ authSchemes
4314
+ })
4315
+ });
4316
+ }
4317
+ #parseSchemas({ schemas, pointers, parseSchema }) {
4318
+ const { discriminator } = this.#settings;
4319
+ const parsedByName = /* @__PURE__ */ new Map();
4320
+ const refAliasMap = /* @__PURE__ */ new Map();
4321
+ const discriminatorParentNodes = [];
4322
+ for (const [name, schema] of Object.entries(schemas)) {
4323
+ const node = {
4324
+ ...parseSchema({
4325
+ schema,
4326
+ name
4327
+ }),
4328
+ pointer: pointers.get(name)
4329
+ };
4330
+ parsedByName.set(name, node);
4331
+ scanSchema(node);
4332
+ if (node.type === "ref" && node.name) refAliasMap.set(name, node);
4333
+ if (discriminator === "propagate" && isDiscriminatorParent(schema)) discriminatorParentNodes.push(node);
4334
+ }
4335
+ return {
4336
+ parsedByName,
4337
+ refAliasMap,
4338
+ discriminatorChildMap: discriminatorParentNodes.length > 0 ? buildDiscriminatorChildMap(discriminatorParentNodes) : null
4339
+ };
4340
+ }
4341
+ };
4342
+ function isDiscriminatorParent(schema) {
4343
+ return Boolean((schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName);
4344
+ }
4345
+ function resolveSchemaNodes({ names, parsed: { parsedByName, refAliasMap, discriminatorChildMap }, promotedEnums }) {
4346
+ const aliasTargetOf = (name) => {
4347
+ const node = refAliasMap.get(name);
4348
+ return node?.ref ? ast.ref.getName(node.ref) : node?.name;
4349
+ };
4350
+ function isAliasCycle(name) {
4351
+ const seen = /* @__PURE__ */ new Set([name]);
4352
+ for (let next = aliasTargetOf(name); next; next = aliasTargetOf(next)) {
4353
+ if (next === name) return true;
4354
+ if (seen.has(next)) return false;
4355
+ seen.add(next);
4356
+ }
4357
+ return false;
4358
+ }
4359
+ const schemaNodes = promotedEnums ? [...promotedEnums.values()] : [];
4360
+ for (const name of names) {
4361
+ const aliasTarget = refAliasMap.get(name)?.name;
4362
+ const aliased = aliasTarget ? parsedByName.get(aliasTarget) : void 0;
4363
+ const parsed = parsedByName.get(name);
4364
+ const child = discriminatorChildMap?.get(name);
4365
+ const base = child ? patchDiscriminatorNode(parsed, child) : parsed;
4366
+ const resolved = aliased ? {
4367
+ ...aliased,
4368
+ name,
4369
+ pointer: parsed.pointer
4370
+ } : base;
4371
+ const node = isAliasCycle(name) ? createCycleNode(name, parsed.pointer) : resolved;
4372
+ schemaNodes.push(promotedEnums ? refPromotedEnums(node, promotedEnums) : node);
4373
+ }
4374
+ return schemaNodes;
4375
+ }
4376
+ function createCycleNode(name, pointer) {
4377
+ Diagnostics.report({
4378
+ code: Diagnostics.code.aliasCycle,
4379
+ severity: "warning",
4380
+ message: `${name} only refers to other schemas that lead back to it, so it is resolved to unknown.`,
4381
+ help: "Give one of the schemas in the loop a real definition, or remove the `$ref` that closes it.",
4382
+ location: {
4383
+ kind: "schema",
4384
+ pointer: pointer ?? `#/components/schemas/${name}`
4385
+ }
4386
+ });
4387
+ return ast.factory.createSchema({
4388
+ type: "unknown",
4389
+ name,
4390
+ pointer
4391
+ });
4392
+ }
4393
+ function toInfoMeta(info) {
4394
+ return {
4395
+ title: info?.title,
4396
+ description: info?.description,
4397
+ version: info?.version,
4398
+ summary: info?.summary,
4399
+ termsOfService: info?.termsOfService,
4400
+ contact: pickStrings(info?.contact, [
4401
+ "name",
4402
+ "url",
4403
+ "email"
4404
+ ]),
4405
+ license: typeof info?.license?.name === "string" ? {
4406
+ ...pickStrings(info.license, ["identifier", "url"]),
4407
+ name: info.license.name
4408
+ } : void 0
4409
+ };
4410
+ }
4411
+ function toInputMeta({ document, server, authSchemes }) {
4412
+ return {
4413
+ ...toInfoMeta(document.info),
4414
+ externalDocs: getExternalDocs(document.externalDocs),
4415
+ baseURL: resolveBaseUrl({
4416
+ document,
4417
+ server
4418
+ }),
4419
+ ...server?.index !== void 0 && { serverIndex: server.index },
4420
+ servers: toInputServers(document.servers ?? []),
4421
+ ...document.tags?.length && { tags: toInputTags(document.tags) },
4422
+ ...authSchemes.length && { auth: authSchemes },
4423
+ extensions: pickExtensions(document)
4424
+ };
4425
+ }
4086
4426
  /**
4087
4427
  * Default Kubb adapter for OpenAPI 2.0, 3.0, 3.1, and 3.2 specifications. Reads the
4088
4428
  * spec from `input` (a file path, URL, inline content, or parsed object), validates
@@ -4131,189 +4471,7 @@ const adapterOas = defineExtension({
4131
4471
  emptySchemaType,
4132
4472
  enumSuffix
4133
4473
  }),
4134
- build({ validate, contentType, server, discriminator, enums, pathParamsCasing, ...parserOptions }) {
4135
- let parsedDocument = null;
4136
- let sources = [];
4137
- const pendingValidations = [];
4138
- const inputCache = /* @__PURE__ */ new WeakMap();
4139
- /**
4140
- * Parses every schema and operation once, resolving ref aliases and discriminator children from that same pass.
4141
- */
4142
- function parseInput({ document, refs, schemas, pointers, parseSchema }) {
4143
- const parsedByName = /* @__PURE__ */ new Map();
4144
- const refAliasMap = /* @__PURE__ */ new Map();
4145
- const discriminatorParentNodes = [];
4146
- for (const [name, schema] of Object.entries(schemas)) {
4147
- const node = {
4148
- ...parseSchema({
4149
- schema,
4150
- name
4151
- }),
4152
- pointer: pointers.get(name)
4153
- };
4154
- parsedByName.set(name, node);
4155
- scanSchema(node);
4156
- if (node.type === "ref" && node.name) refAliasMap.set(name, node);
4157
- if (discriminator === "propagate" && (schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName) discriminatorParentNodes.push(node);
4158
- }
4159
- const discriminatorChildMap = discriminatorParentNodes.length > 0 ? buildDiscriminatorChildMap(discriminatorParentNodes) : null;
4160
- const operationNodes = getOperations(document, refs).map((operation) => {
4161
- const node = parseOperation({
4162
- operation,
4163
- document,
4164
- refs,
4165
- contentType,
4166
- options: parserOptions,
4167
- parseSchema
4168
- });
4169
- return pathParamsCasing === "camelcase" ? camelCasePathParams(node) : node;
4170
- });
4171
- const promotedEnums = enums === "root" ? collectInlineEnums([...parsedByName.values(), ...operationNodes], new Set(Object.keys(schemas))) : null;
4172
- const aliasTargetOf = (name) => {
4173
- const node = refAliasMap.get(name);
4174
- return node?.ref ? ast.ref.getName(node.ref) : node?.name;
4175
- };
4176
- function isAliasCycle(name) {
4177
- const seen = /* @__PURE__ */ new Set([name]);
4178
- for (let next = aliasTargetOf(name); next; next = aliasTargetOf(next)) {
4179
- if (next === name) return true;
4180
- if (seen.has(next)) return false;
4181
- seen.add(next);
4182
- }
4183
- return false;
4184
- }
4185
- function createCycleNode(name, pointer) {
4186
- Diagnostics.report({
4187
- code: Diagnostics.code.aliasCycle,
4188
- severity: "warning",
4189
- message: `${name} only refers to other schemas that lead back to it, so it is resolved to unknown.`,
4190
- help: "Give one of the schemas in the loop a real definition, or remove the `$ref` that closes it.",
4191
- location: {
4192
- kind: "schema",
4193
- pointer: pointer ?? `#/components/schemas/${name}`
4194
- }
4195
- });
4196
- return ast.factory.createSchema({
4197
- type: "unknown",
4198
- name,
4199
- pointer
4200
- });
4201
- }
4202
- const schemaNodes = promotedEnums ? [...promotedEnums.values()] : [];
4203
- for (const name of Object.keys(schemas)) {
4204
- const aliasTarget = refAliasMap.get(name)?.name;
4205
- const aliased = aliasTarget ? parsedByName.get(aliasTarget) : void 0;
4206
- const parsed = parsedByName.get(name);
4207
- const child = discriminatorChildMap?.get(name);
4208
- const base = child ? patchDiscriminatorNode(parsed, child) : parsed;
4209
- const resolved = aliased ? {
4210
- ...aliased,
4211
- name,
4212
- pointer: parsed.pointer
4213
- } : base;
4214
- const node = isAliasCycle(name) ? createCycleNode(name, parsed.pointer) : resolved;
4215
- schemaNodes.push(promotedEnums ? refPromotedEnums(node, promotedEnums) : node);
4216
- }
4217
- const operations = promotedEnums ? operationNodes.map((node) => refPromotedEnums(node, promotedEnums)) : operationNodes;
4218
- const authSchemes = toAuthSchemes(document.components?.securitySchemes, refs);
4219
- return ast.factory.createInput({
4220
- schemas: schemaNodes,
4221
- operations,
4222
- meta: {
4223
- title: document.info?.title,
4224
- description: document.info?.description,
4225
- version: document.info?.version,
4226
- summary: document.info?.summary,
4227
- termsOfService: document.info?.termsOfService,
4228
- contact: pickStrings(document.info?.contact, [
4229
- "name",
4230
- "url",
4231
- "email"
4232
- ]),
4233
- license: typeof document.info?.license?.name === "string" ? {
4234
- ...pickStrings(document.info.license, ["identifier", "url"]),
4235
- name: document.info.license.name
4236
- } : void 0,
4237
- externalDocs: getExternalDocs(document.externalDocs),
4238
- baseURL: resolveBaseUrl({
4239
- document,
4240
- server
4241
- }),
4242
- ...server?.index !== void 0 && { serverIndex: server.index },
4243
- servers: toInputServers(document.servers ?? []),
4244
- ...document.tags?.length && { tags: toInputTags(document.tags) },
4245
- ...authSchemes.length && { auth: authSchemes },
4246
- extensions: pickExtensions(document)
4247
- }
4248
- });
4249
- }
4250
- return {
4251
- get document() {
4252
- return parsedDocument;
4253
- },
4254
- get sources() {
4255
- return sources;
4256
- },
4257
- locate,
4258
- async validate(input, options) {
4259
- const document = await parseFromConfig({
4260
- type: "path",
4261
- path: input
4262
- }, [], { fileRefs: options?.fileRefs });
4263
- assertDocument(document);
4264
- await validateDocument(document, options);
4265
- },
4266
- async settle() {
4267
- await Promise.all(pendingValidations.splice(0));
4268
- },
4269
- async parse(source, { signal, fileRefs } = {}) {
4270
- signal?.throwIfAborted();
4271
- const cached = inputCache.get(source);
4272
- if (cached) {
4273
- sources = cached.sources;
4274
- parsedDocument = cached.document;
4275
- return cached.promise;
4276
- }
4277
- const read = [];
4278
- sources = read;
4279
- const entry = {
4280
- sources: read,
4281
- document: null
4282
- };
4283
- const promise = (async () => {
4284
- const document = await parseFromConfig(source, read, {
4285
- fileRefs,
4286
- signal
4287
- });
4288
- signal?.throwIfAborted();
4289
- assertDocument(document);
4290
- if (validate) pendingValidations.push(validateDocumentInWorker(document));
4291
- entry.document = document;
4292
- parsedDocument = document;
4293
- const refs = createRefs(document);
4294
- const { schemas, renames, pointers } = getSchemas(document, { contentType }, refs);
4295
- const parseSchema = createSchemaParser({
4296
- document,
4297
- refs,
4298
- renames,
4299
- options: parserOptions
4300
- });
4301
- return groupUnsupportedFormats(() => parseInput({
4302
- document,
4303
- refs,
4304
- schemas,
4305
- pointers,
4306
- parseSchema
4307
- }));
4308
- })();
4309
- inputCache.set(source, Object.assign(entry, { promise }));
4310
- return promise.catch((error) => {
4311
- if (signal?.aborted) inputCache.delete(source);
4312
- throw error;
4313
- });
4314
- }
4315
- };
4316
- }
4474
+ build: (options) => new Adapter(options)
4317
4475
  });
4318
4476
  export { adapterOas, adapterOasName };
4319
4477