@kubb/adapter-oas 5.18.7 → 5.19.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.cjs +313 -231
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +310 -214
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -2,11 +2,11 @@ import { defineExtension } from "@kubb/core";
|
|
|
2
2
|
import { Diagnostics, ast, childName, enumPrimitive, enumPropName, macroDedupeUnion, macroDiscriminatorEnum, macroEnumName, mergeAdjacentObjectsLazy } from "@kubb/kit";
|
|
3
3
|
import { dirname, join, resolve } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import {
|
|
5
|
+
import { readFile } from "node:fs";
|
|
6
|
+
import { access } from "node:fs/promises";
|
|
6
7
|
import { upgrade } from "@scalar/openapi-upgrader";
|
|
7
8
|
import { bundle } from "api-ref-bundler";
|
|
8
|
-
import { CORE_SCHEMA, load } from "js-yaml";
|
|
9
|
-
import * as yaml from "yaml";
|
|
9
|
+
import { COLLECTION_STYLE, CORE_SCHEMA, EVENT_ID, SCALAR_STYLE, constructFromEvents, load, parseEvents } from "js-yaml";
|
|
10
10
|
import { parse as parse$1 } from "yaml";
|
|
11
11
|
// kubb-extension.json
|
|
12
12
|
var compatibility = {
|
|
@@ -14,7 +14,7 @@ var compatibility = {
|
|
|
14
14
|
"node": ">=22.22"
|
|
15
15
|
};
|
|
16
16
|
// package.json
|
|
17
|
-
var version = "5.
|
|
17
|
+
var version = "5.19.0";
|
|
18
18
|
// src/constants.ts
|
|
19
19
|
/**
|
|
20
20
|
* Default parser options applied when no explicit options are provided.
|
|
@@ -222,6 +222,47 @@ function patchDiscriminatorNode(node, entry) {
|
|
|
222
222
|
members: newMembers
|
|
223
223
|
};
|
|
224
224
|
}
|
|
225
|
+
// ../../internals/utils/src/promise.ts
|
|
226
|
+
/**
|
|
227
|
+
* Returns the value cached under `key` in `store`, computing and storing it on the first call.
|
|
228
|
+
* Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
|
|
229
|
+
* keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* `const refs = memo(refCache, node, () => collectRefs(node))`
|
|
233
|
+
*/
|
|
234
|
+
function memo(store, key, compute) {
|
|
235
|
+
const cached = store.get(key);
|
|
236
|
+
if (cached !== void 0 || store.has(key)) return cached;
|
|
237
|
+
const value = compute();
|
|
238
|
+
store.set(key, value);
|
|
239
|
+
return value;
|
|
240
|
+
}
|
|
241
|
+
function createStore({ weak, max } = {}) {
|
|
242
|
+
if (weak) return /* @__PURE__ */ new WeakMap();
|
|
243
|
+
const entry = /* @__PURE__ */ new Map();
|
|
244
|
+
if (max === void 0) return entry;
|
|
245
|
+
const set = (key, value) => {
|
|
246
|
+
if (entry.size >= max) entry.clear();
|
|
247
|
+
entry.set(key, value);
|
|
248
|
+
};
|
|
249
|
+
if (max !== 1) return {
|
|
250
|
+
has: (key) => entry.has(key),
|
|
251
|
+
get: (key) => entry.get(key),
|
|
252
|
+
set
|
|
253
|
+
};
|
|
254
|
+
const touch = (key) => {
|
|
255
|
+
if (!entry.has(key)) return false;
|
|
256
|
+
set(key, entry.get(key));
|
|
257
|
+
return true;
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
has: touch,
|
|
261
|
+
get: (key) => touch(key) ? entry.get(key) : void 0,
|
|
262
|
+
set
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
memo.create = createStore;
|
|
225
266
|
// ../../internals/utils/src/casing.ts
|
|
226
267
|
/**
|
|
227
268
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -236,16 +277,10 @@ function convert(text, pascal) {
|
|
|
236
277
|
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
237
278
|
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
238
279
|
}
|
|
239
|
-
const cache =
|
|
240
|
-
const cacheLimit = 1e5;
|
|
280
|
+
const cache = memo.create({ max: 1e5 });
|
|
241
281
|
function toCamelOrPascal(text, pascal) {
|
|
242
282
|
const key = `${pascal ? "P" : "c"}${text}`;
|
|
243
|
-
|
|
244
|
-
if (cached !== void 0) return cached;
|
|
245
|
-
const result = convert(text, pascal);
|
|
246
|
-
if (cache.size >= cacheLimit) cache.clear();
|
|
247
|
-
cache.set(key, result);
|
|
248
|
-
return result;
|
|
283
|
+
return memo(cache, key, () => convert(text, pascal));
|
|
249
284
|
}
|
|
250
285
|
/**
|
|
251
286
|
* Converts `text` to PascalCase.
|
|
@@ -361,7 +396,7 @@ async function exists(path) {
|
|
|
361
396
|
*/
|
|
362
397
|
async function read(path) {
|
|
363
398
|
if (runtime.isBun) return Bun.file(path).text();
|
|
364
|
-
return readFile(path, { encoding: "utf8" });
|
|
399
|
+
return new Promise((resolvePromise, reject) => readFile(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
|
|
365
400
|
}
|
|
366
401
|
// src/oas.ts
|
|
367
402
|
/**
|
|
@@ -501,7 +536,7 @@ function isJsonMimeType(mimeType) {
|
|
|
501
536
|
*/
|
|
502
537
|
function createRefs(document) {
|
|
503
538
|
const pointerCache = /* @__PURE__ */ new Map();
|
|
504
|
-
const resolvedNodeCache =
|
|
539
|
+
const resolvedNodeCache = memo.create();
|
|
505
540
|
const resolvingRefs = /* @__PURE__ */ new Set();
|
|
506
541
|
function walkPointer($ref) {
|
|
507
542
|
const trimmed = $ref.trim();
|
|
@@ -551,7 +586,7 @@ function createRefs(document) {
|
|
|
551
586
|
*/
|
|
552
587
|
function resolveNode(refPath, parse) {
|
|
553
588
|
if (resolvingRefs.has(refPath)) return null;
|
|
554
|
-
|
|
589
|
+
return memo(resolvedNodeCache, refPath, () => {
|
|
555
590
|
let resolved = null;
|
|
556
591
|
try {
|
|
557
592
|
const referenced = resolve(refPath);
|
|
@@ -561,9 +596,8 @@ function createRefs(document) {
|
|
|
561
596
|
resolvingRefs.delete(refPath);
|
|
562
597
|
}
|
|
563
598
|
} catch {}
|
|
564
|
-
|
|
565
|
-
}
|
|
566
|
-
return resolvedNodeCache.get(refPath) ?? null;
|
|
599
|
+
return resolved;
|
|
600
|
+
});
|
|
567
601
|
}
|
|
568
602
|
/**
|
|
569
603
|
* Resolves a `$ref` value without mutating anything: when `value` holds a `$ref`, returns the
|
|
@@ -608,6 +642,11 @@ const NAMED_ENTRY_KEYS = /* @__PURE__ */ new Set([
|
|
|
608
642
|
"schemas",
|
|
609
643
|
"dependentSchemas"
|
|
610
644
|
]);
|
|
645
|
+
const TARGET_KEYS = [
|
|
646
|
+
"$id",
|
|
647
|
+
"$anchor",
|
|
648
|
+
"$dynamicAnchor"
|
|
649
|
+
];
|
|
611
650
|
function walk(node, pointer, visit, parentKey) {
|
|
612
651
|
if (!node || typeof node !== "object") return;
|
|
613
652
|
if (!Array.isArray(node)) visit(node, pointer);
|
|
@@ -617,6 +656,37 @@ function walk(node, pointer, visit, parentKey) {
|
|
|
617
656
|
walk(value, `${pointer}/${escapePointerToken(key)}`, visit, Array.isArray(node) ? parentKey : key);
|
|
618
657
|
}
|
|
619
658
|
}
|
|
659
|
+
/** Answers, in one walk and without building JSON pointers, what the loading steps after it need to know. */
|
|
660
|
+
function scanDocument(document) {
|
|
661
|
+
const scan = {
|
|
662
|
+
externalRef: false,
|
|
663
|
+
schemaIds: false,
|
|
664
|
+
pathRefs: false
|
|
665
|
+
};
|
|
666
|
+
const visit = ({ node, readsIds, parentKey }) => {
|
|
667
|
+
if (!node || typeof node !== "object") return;
|
|
668
|
+
const isArray = Array.isArray(node);
|
|
669
|
+
const record = node;
|
|
670
|
+
const ref = record["$ref"];
|
|
671
|
+
if (typeof ref === "string" && !ref.startsWith("#")) scan.externalRef = true;
|
|
672
|
+
if (typeof ref === "string" && ref.startsWith("#/paths/")) scan.pathRefs = true;
|
|
673
|
+
if (readsIds && !isArray && TARGET_KEYS.some((key) => typeof record[key] === "string")) scan.schemaIds = true;
|
|
674
|
+
const named = parentKey !== void 0 && NAMED_ENTRY_KEYS.has(parentKey);
|
|
675
|
+
for (const key in record) {
|
|
676
|
+
const childReadsIds = readsIds && (named || !(DATA_KEYS.has(key) || key.startsWith("x-")));
|
|
677
|
+
visit({
|
|
678
|
+
node: record[key],
|
|
679
|
+
readsIds: childReadsIds,
|
|
680
|
+
parentKey: isArray ? parentKey : key
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
};
|
|
684
|
+
visit({
|
|
685
|
+
node: document,
|
|
686
|
+
readsIds: true
|
|
687
|
+
});
|
|
688
|
+
return scan;
|
|
689
|
+
}
|
|
620
690
|
/**
|
|
621
691
|
* The declaration in the nearest enclosing schema, else the only one, else `undefined`.
|
|
622
692
|
*/
|
|
@@ -634,8 +704,8 @@ function pick(declarations, pointer) {
|
|
|
634
704
|
* enclosing anchor, so a relative `$id` base or a recursively extended schema is not followed, upgrade when a spec needs that.
|
|
635
705
|
*/
|
|
636
706
|
function resolveSchemaIds(document) {
|
|
637
|
-
const targets =
|
|
638
|
-
const declare = (key, pointer) => targets
|
|
707
|
+
const targets = memo.create();
|
|
708
|
+
const declare = (key, pointer) => memo(targets, key, () => []).push(pointer);
|
|
639
709
|
walk(document, "#", (node, pointer) => {
|
|
640
710
|
if (typeof node["$id"] === "string") declare(node["$id"], pointer);
|
|
641
711
|
for (const key of ["$anchor", "$dynamicAnchor"]) if (typeof node[key] === "string") declare(`#${node[key]}`, pointer);
|
|
@@ -651,6 +721,182 @@ function resolveSchemaIds(document) {
|
|
|
651
721
|
}
|
|
652
722
|
});
|
|
653
723
|
}
|
|
724
|
+
// src/sourceLocator.ts
|
|
725
|
+
const flowGap = /(?:[\s,]|#[^\n]*)*/y;
|
|
726
|
+
const indexes = memo.create({ max: 1 });
|
|
727
|
+
function toKeyText(text) {
|
|
728
|
+
if (!/^[-+.~\d]|^(?:null|true|false)$/i.test(text)) return text;
|
|
729
|
+
try {
|
|
730
|
+
const value = load(text, { schema: CORE_SCHEMA });
|
|
731
|
+
return value === null || typeof value === "number" || typeof value === "boolean" ? String(value) : text;
|
|
732
|
+
} catch {
|
|
733
|
+
return text;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
function indexEvents(events) {
|
|
737
|
+
const types = new Uint8Array(events.length);
|
|
738
|
+
const styles = new Uint8Array(events.length);
|
|
739
|
+
const starts = new Int32Array(events.length);
|
|
740
|
+
const ends = new Int32Array(events.length);
|
|
741
|
+
for (let at = 0, last = 0; at < events.length; last = Math.max(last, starts[at], ends[at]), at++) {
|
|
742
|
+
const event = events[at];
|
|
743
|
+
types[at] = event.type;
|
|
744
|
+
if (event.type === EVENT_ID.SCALAR) {
|
|
745
|
+
const quote = event.style === SCALAR_STYLE.SINGLE_QUOTED || event.style === SCALAR_STYLE.DOUBLE_QUOTED ? 1 : 0;
|
|
746
|
+
const empty = event.valueStart < 0;
|
|
747
|
+
styles[at] = event.style;
|
|
748
|
+
starts[at] = empty ? last : event.valueStart - quote;
|
|
749
|
+
ends[at] = empty ? last : event.valueEnd + quote;
|
|
750
|
+
}
|
|
751
|
+
if (event.type === EVENT_ID.ALIAS) {
|
|
752
|
+
starts[at] = event.anchorStart - 1;
|
|
753
|
+
ends[at] = event.anchorEnd;
|
|
754
|
+
}
|
|
755
|
+
if (event.type === EVENT_ID.MAPPING || event.type === EVENT_ID.SEQUENCE) {
|
|
756
|
+
styles[at] = event.style;
|
|
757
|
+
starts[at] = event.start;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return {
|
|
761
|
+
types,
|
|
762
|
+
styles,
|
|
763
|
+
starts,
|
|
764
|
+
ends,
|
|
765
|
+
collections: /* @__PURE__ */ new Map()
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
function createLocator({ index, source }) {
|
|
769
|
+
const { types, styles, starts, ends, collections } = index;
|
|
770
|
+
const isCollection = (at) => types[at] === EVENT_ID.MAPPING || types[at] === EVENT_ID.SEQUENCE;
|
|
771
|
+
const readText = (at) => {
|
|
772
|
+
const text = source.slice(starts[at], ends[at]);
|
|
773
|
+
if (styles[at] === SCALAR_STYLE.SINGLE_QUOTED) return text.slice(1, -1).replaceAll("''", "'");
|
|
774
|
+
if (styles[at] !== SCALAR_STYLE.DOUBLE_QUOTED) return text;
|
|
775
|
+
try {
|
|
776
|
+
return JSON.parse(text);
|
|
777
|
+
} catch {
|
|
778
|
+
return String(load(text, { schema: CORE_SCHEMA }));
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
const readNode = (at) => {
|
|
782
|
+
if (!isCollection(at)) return {
|
|
783
|
+
start: starts[at],
|
|
784
|
+
end: ends[at],
|
|
785
|
+
next: at + 1
|
|
786
|
+
};
|
|
787
|
+
let next = at + 1;
|
|
788
|
+
let end = starts[at];
|
|
789
|
+
while (types[next] !== EVENT_ID.POP) ({end, next} = readNode(next));
|
|
790
|
+
if (styles[at] === COLLECTION_STYLE.FLOW) {
|
|
791
|
+
flowGap.lastIndex = end;
|
|
792
|
+
flowGap.exec(source);
|
|
793
|
+
end = flowGap.lastIndex + 1;
|
|
794
|
+
}
|
|
795
|
+
return {
|
|
796
|
+
start: starts[at],
|
|
797
|
+
end,
|
|
798
|
+
next: next + 1
|
|
799
|
+
};
|
|
800
|
+
};
|
|
801
|
+
const readChildren = (collection) => {
|
|
802
|
+
const keys = /* @__PURE__ */ new Map();
|
|
803
|
+
const items = [];
|
|
804
|
+
const isMapping = types[collection] === EVENT_ID.MAPPING;
|
|
805
|
+
for (let at = collection + 1; types[at] !== EVENT_ID.POP;) {
|
|
806
|
+
const key = isMapping ? at : void 0;
|
|
807
|
+
if (key !== void 0) at = readNode(at).next;
|
|
808
|
+
const { start, end, next } = readNode(at);
|
|
809
|
+
if (key === void 0) items.push({
|
|
810
|
+
index: at,
|
|
811
|
+
range: {
|
|
812
|
+
start,
|
|
813
|
+
end
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
const raw = key !== void 0 && types[key] === EVENT_ID.SCALAR ? readText(key) : void 0;
|
|
817
|
+
const text = raw !== void 0 && styles[key] === SCALAR_STYLE.PLAIN ? toKeyText(raw) : raw;
|
|
818
|
+
if (text !== void 0 && !keys.has(text)) keys.set(text, {
|
|
819
|
+
index: at,
|
|
820
|
+
range: {
|
|
821
|
+
start: starts[key],
|
|
822
|
+
end: ends[key]
|
|
823
|
+
}
|
|
824
|
+
});
|
|
825
|
+
at = next;
|
|
826
|
+
}
|
|
827
|
+
return {
|
|
828
|
+
keys,
|
|
829
|
+
items
|
|
830
|
+
};
|
|
831
|
+
};
|
|
832
|
+
const walk = ({ at, tokens: [token, ...rest], range }) => {
|
|
833
|
+
if (token === void 0) return range;
|
|
834
|
+
if (!isCollection(at)) return void 0;
|
|
835
|
+
const { keys, items } = memo(collections, at, () => readChildren(at));
|
|
836
|
+
const child = keys.get(token) ?? items[Number(token)];
|
|
837
|
+
return child ? walk({
|
|
838
|
+
at: child.index,
|
|
839
|
+
tokens: rest,
|
|
840
|
+
range: child.range
|
|
841
|
+
}) : void 0;
|
|
842
|
+
};
|
|
843
|
+
return {
|
|
844
|
+
find(pointer) {
|
|
845
|
+
return walk({
|
|
846
|
+
at: 1,
|
|
847
|
+
tokens: pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken),
|
|
848
|
+
range: void 0
|
|
849
|
+
});
|
|
850
|
+
},
|
|
851
|
+
findRef(ref) {
|
|
852
|
+
if (!index.refs) {
|
|
853
|
+
index.refs = /* @__PURE__ */ new Map();
|
|
854
|
+
const frames = [];
|
|
855
|
+
for (let at = 1; at + 1 < types.length; at++) {
|
|
856
|
+
if (types[at] === EVENT_ID.POP) {
|
|
857
|
+
frames.pop();
|
|
858
|
+
continue;
|
|
859
|
+
}
|
|
860
|
+
const top = frames.length - 1;
|
|
861
|
+
const isKey = frames[top] === 0;
|
|
862
|
+
if (frames[top] >= 0) frames[top] = 1 - frames[top];
|
|
863
|
+
if (isCollection(at)) frames.push(types[at] === EVENT_ID.MAPPING ? 0 : -1);
|
|
864
|
+
if (!isKey || types[at] !== EVENT_ID.SCALAR || types[at + 1] !== EVENT_ID.SCALAR || readText(at) !== "$ref") continue;
|
|
865
|
+
const text = readText(at + 1);
|
|
866
|
+
if (!index.refs.has(text)) index.refs.set(text, {
|
|
867
|
+
start: starts[at + 1],
|
|
868
|
+
end: ends[at + 1]
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
return index.refs.get(ref);
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
function parse(source) {
|
|
877
|
+
try {
|
|
878
|
+
return indexEvents(parseEvents(source, { maxDepth: Number.POSITIVE_INFINITY }));
|
|
879
|
+
} catch {
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
function rememberEvents({ source, events }) {
|
|
884
|
+
indexes.set(source, indexEvents(events));
|
|
885
|
+
}
|
|
886
|
+
/**
|
|
887
|
+
* Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
|
|
888
|
+
*/
|
|
889
|
+
const locate = (source, location) => {
|
|
890
|
+
if (!("pointer" in location)) return void 0;
|
|
891
|
+
const index = memo(indexes, source, () => parse(source));
|
|
892
|
+
if (!index) return void 0;
|
|
893
|
+
const { find, findRef } = createLocator({
|
|
894
|
+
index,
|
|
895
|
+
source
|
|
896
|
+
});
|
|
897
|
+
const ref = "ref" in location ? location.ref : void 0;
|
|
898
|
+
return find(location.pointer) ?? (ref ? findRef(ref) : void 0);
|
|
899
|
+
};
|
|
654
900
|
// src/load/source.ts
|
|
655
901
|
const urlRegExp = /^https?:\/+/i;
|
|
656
902
|
/**
|
|
@@ -705,14 +951,15 @@ async function readSource(sourcePath) {
|
|
|
705
951
|
* Reads and parses one source file or URL referenced during bundling: YAML/JSON is parsed into an
|
|
706
952
|
* object, Markdown is returned as-is (bundled inline rather than dereferenced).
|
|
707
953
|
*/
|
|
708
|
-
async function resolveSource(sourcePath) {
|
|
954
|
+
async function resolveSource(sourcePath, { remember } = {}) {
|
|
709
955
|
const data = await readSource(sourcePath);
|
|
710
956
|
if (sourcePath.toLowerCase().endsWith(".md")) return data;
|
|
711
957
|
const pathname = urlRegExp.test(sourcePath) ? new URL(sourcePath).pathname : sourcePath;
|
|
712
958
|
return parseYaml(data, {
|
|
713
959
|
origin: sourcePath,
|
|
714
960
|
json: !/\.ya?ml$/i.test(pathname),
|
|
715
|
-
help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page."
|
|
961
|
+
help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page.",
|
|
962
|
+
remember
|
|
716
963
|
});
|
|
717
964
|
}
|
|
718
965
|
/**
|
|
@@ -728,13 +975,23 @@ async function resolveSource(sourcePath) {
|
|
|
728
975
|
* keeping the last one. Its core schema keeps timestamps as strings and `<<` as a plain key, as
|
|
729
976
|
* `yaml` does for YAML 1.2.
|
|
730
977
|
*/
|
|
731
|
-
function parseYaml(data, { origin, help, json = true }) {
|
|
978
|
+
function parseYaml(data, { origin, help, json = true, remember = false }) {
|
|
732
979
|
if (json && /^\s*[[{]/.test(data)) try {
|
|
733
980
|
return JSON.parse(data);
|
|
734
981
|
} catch {}
|
|
735
982
|
try {
|
|
736
|
-
const
|
|
737
|
-
|
|
983
|
+
const events = parseEvents(data, {});
|
|
984
|
+
const documents = constructFromEvents(events, {
|
|
985
|
+
source: data,
|
|
986
|
+
schema: CORE_SCHEMA
|
|
987
|
+
});
|
|
988
|
+
if (documents.length === 1 && documents[0] !== void 0) {
|
|
989
|
+
if (remember) rememberEvents({
|
|
990
|
+
source: data,
|
|
991
|
+
events
|
|
992
|
+
});
|
|
993
|
+
return documents[0];
|
|
994
|
+
}
|
|
738
995
|
} catch {}
|
|
739
996
|
try {
|
|
740
997
|
return parse$1(data);
|
|
@@ -766,18 +1023,15 @@ async function assertInputExists(input) {
|
|
|
766
1023
|
});
|
|
767
1024
|
}
|
|
768
1025
|
// src/load/normalize.ts
|
|
1026
|
+
const scans = /* @__PURE__ */ new WeakMap();
|
|
769
1027
|
/**
|
|
770
|
-
*
|
|
771
|
-
* absolute path, or URL). An internal `#/...` fragment does not count.
|
|
772
|
-
*
|
|
773
|
-
* `Object.values` reads array elements and object property values alike, so the same recursion
|
|
774
|
-
* walks both without a separate array branch.
|
|
1028
|
+
* Rewrites `$id` and `$anchor` refs in place, then returns the scan of the result.
|
|
775
1029
|
*/
|
|
776
|
-
function
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
return
|
|
1030
|
+
function resolveTargets(document) {
|
|
1031
|
+
const scan = scanDocument(document);
|
|
1032
|
+
if (!scan.schemaIds) return scan;
|
|
1033
|
+
resolveSchemaIds(document);
|
|
1034
|
+
return scanDocument(document);
|
|
781
1035
|
}
|
|
782
1036
|
/**
|
|
783
1037
|
* Replaces each `$ref` into `#/paths/...` with a copy of its target, in place.
|
|
@@ -859,23 +1113,24 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
859
1113
|
* `const document = await bundleDocument('https://example.com/openapi.yaml')`
|
|
860
1114
|
*/
|
|
861
1115
|
async function bundleDocument(pathOrUrl, { document, sources = [] } = {}) {
|
|
862
|
-
const cache =
|
|
1116
|
+
const cache = memo.create();
|
|
863
1117
|
const toKey = (sourcePath) => urlRegExp.test(sourcePath) ? new URL(sourcePath).href : sourcePath;
|
|
864
1118
|
let entry = pathOrUrl;
|
|
865
1119
|
let entryDocument = document;
|
|
866
1120
|
const resolver = (sourcePath) => {
|
|
867
1121
|
if (entryDocument !== void 0 && toKey(sourcePath) === toKey(entry)) return Promise.resolve(entryDocument);
|
|
868
1122
|
const key = toKey(sourcePath);
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
cache.set(key, result);
|
|
874
|
-
return result;
|
|
1123
|
+
return memo(cache, key, () => {
|
|
1124
|
+
sources.push(key);
|
|
1125
|
+
return resolveSource(sourcePath, { remember: key === toKey(pathOrUrl) });
|
|
1126
|
+
});
|
|
875
1127
|
};
|
|
876
1128
|
const root = await resolver(pathOrUrl);
|
|
877
|
-
|
|
878
|
-
if (typeof root === "object" && root !== null && !
|
|
1129
|
+
const scan = resolveTargets(root);
|
|
1130
|
+
if (typeof root === "object" && root !== null && !scan.externalRef) {
|
|
1131
|
+
scans.set(root, scan);
|
|
1132
|
+
return root;
|
|
1133
|
+
}
|
|
879
1134
|
entry = getBaseUri(pathOrUrl, root);
|
|
880
1135
|
entryDocument = root;
|
|
881
1136
|
return await bundle(entry, resolver, { hooks: { onError: (message, { path, state }) => {
|
|
@@ -918,11 +1173,13 @@ async function parseFromConfig(source, sources = []) {
|
|
|
918
1173
|
origin: "the inline `input` string",
|
|
919
1174
|
help: "Fix the syntax at the reported line. Kubb reads `input` as an inline spec, not a file path, because it starts with `{`, `[`, `openapi:`, or `swagger:`, or spans several lines."
|
|
920
1175
|
}) : structuredClone(source.data);
|
|
921
|
-
|
|
1176
|
+
const isObject = Boolean(data) && typeof data === "object";
|
|
1177
|
+
if (source.root && isObject) return bundleDocument(join(source.root, inlineInputName), {
|
|
922
1178
|
document: data,
|
|
923
1179
|
sources
|
|
924
1180
|
});
|
|
925
|
-
|
|
1181
|
+
const scan = resolveTargets(data);
|
|
1182
|
+
if (isObject) scans.set(data, scan);
|
|
926
1183
|
return data;
|
|
927
1184
|
}
|
|
928
1185
|
await assertInputExists(source.path);
|
|
@@ -935,7 +1192,7 @@ async function parseFromConfig(source, sources = []) {
|
|
|
935
1192
|
if (typeof version === "number") document[key] = Number.isInteger(version) ? version.toFixed(1) : String(version);
|
|
936
1193
|
}
|
|
937
1194
|
}
|
|
938
|
-
if (raw && typeof raw === "object" && "swagger" in raw) {
|
|
1195
|
+
if (raw && typeof raw === "object" && "swagger" in raw && (scans.get(raw)?.pathRefs ?? true)) {
|
|
939
1196
|
const refs = createRefs(raw);
|
|
940
1197
|
inlinePathRefs(raw, (ref) => refs.resolve(ref, { report: false }));
|
|
941
1198
|
}
|
|
@@ -1242,13 +1499,8 @@ function getSchemas(document, { contentType }, refs) {
|
|
|
1242
1499
|
pointer: `${ref}/content/${escapePointerToken(extracted.contentType)}/schema`
|
|
1243
1500
|
}];
|
|
1244
1501
|
}))];
|
|
1245
|
-
const normalizedNames =
|
|
1246
|
-
for (const item of candidates)
|
|
1247
|
-
const key = pascalCase(item.originalName);
|
|
1248
|
-
const bucket = normalizedNames.get(key) ?? [];
|
|
1249
|
-
bucket.push(item);
|
|
1250
|
-
normalizedNames.set(key, bucket);
|
|
1251
|
-
}
|
|
1502
|
+
const normalizedNames = memo.create();
|
|
1503
|
+
for (const item of candidates) memo(normalizedNames, pascalCase(item.originalName), () => []).push(item);
|
|
1252
1504
|
const schemas = {};
|
|
1253
1505
|
const renames = /* @__PURE__ */ new Map();
|
|
1254
1506
|
const pointers = /* @__PURE__ */ new Map();
|
|
@@ -1646,7 +1898,7 @@ let formatTally;
|
|
|
1646
1898
|
* thousands of warnings that say the same thing. `fn` must be synchronous.
|
|
1647
1899
|
*/
|
|
1648
1900
|
function groupUnsupportedFormats(fn) {
|
|
1649
|
-
formatTally =
|
|
1901
|
+
formatTally = memo.create();
|
|
1650
1902
|
try {
|
|
1651
1903
|
return fn();
|
|
1652
1904
|
} finally {
|
|
@@ -1657,7 +1909,7 @@ function groupUnsupportedFormats(fn) {
|
|
|
1657
1909
|
}
|
|
1658
1910
|
function tallyFormat(format, pointer) {
|
|
1659
1911
|
if (!formatTally) return reportUnsupportedFormat(format, [pointer]);
|
|
1660
|
-
formatTally
|
|
1912
|
+
memo(formatTally, format, () => /* @__PURE__ */ new Set()).add(pointer);
|
|
1661
1913
|
}
|
|
1662
1914
|
function reportUnsupportedFormat(format, pointers) {
|
|
1663
1915
|
const count = pointers.length;
|
|
@@ -1714,10 +1966,10 @@ function visit(node, pointer) {
|
|
|
1714
1966
|
*/
|
|
1715
1967
|
function reportDuplicateDiscriminators(node, propertyName, pointer) {
|
|
1716
1968
|
const keyword = node.strategy === "any" ? "anyOf" : "oneOf";
|
|
1717
|
-
const membersByValue =
|
|
1969
|
+
const membersByValue = memo.create();
|
|
1718
1970
|
for (const [index, member] of (node.members ?? []).entries()) {
|
|
1719
1971
|
const label = memberName(member) ?? `${keyword}/${index}`;
|
|
1720
|
-
for (const value of memberValues(member, propertyName)) membersByValue
|
|
1972
|
+
for (const value of memberValues(member, propertyName)) memo(membersByValue, value, () => []).push(label);
|
|
1721
1973
|
}
|
|
1722
1974
|
const duplicates = [...membersByValue].filter(([, members]) => members.length > 1);
|
|
1723
1975
|
if (!duplicates.length) return;
|
|
@@ -3602,162 +3854,6 @@ function refPromotedEnums(node, promoted) {
|
|
|
3602
3854
|
});
|
|
3603
3855
|
} });
|
|
3604
3856
|
}
|
|
3605
|
-
// src/sourceLocator.ts
|
|
3606
|
-
let cached;
|
|
3607
|
-
const toRange = ([start, end]) => ({
|
|
3608
|
-
start,
|
|
3609
|
-
end
|
|
3610
|
-
});
|
|
3611
|
-
/** Walks a JSON pointer through maps and sequences; the range is the last key or item it reached. */
|
|
3612
|
-
function findPointer(document, pointer) {
|
|
3613
|
-
return pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken).reduce((at, token) => {
|
|
3614
|
-
if (yaml.isMap(at?.node)) {
|
|
3615
|
-
const pair = at.node.items.find((item) => yaml.isScalar(item.key) && String(item.key.value) === token);
|
|
3616
|
-
return pair && yaml.isScalar(pair.key) && pair.key.range ? {
|
|
3617
|
-
node: pair.value,
|
|
3618
|
-
range: toRange(pair.key.range)
|
|
3619
|
-
} : void 0;
|
|
3620
|
-
}
|
|
3621
|
-
if (yaml.isSeq(at?.node)) {
|
|
3622
|
-
const item = at.node.items[Number(token)];
|
|
3623
|
-
return yaml.isNode(item) && item.range ? {
|
|
3624
|
-
node: item,
|
|
3625
|
-
range: toRange(item.range)
|
|
3626
|
-
} : void 0;
|
|
3627
|
-
}
|
|
3628
|
-
}, {
|
|
3629
|
-
node: document.contents,
|
|
3630
|
-
range: void 0
|
|
3631
|
-
})?.range;
|
|
3632
|
-
}
|
|
3633
|
-
/** The first `$ref` whose value is `ref`. */
|
|
3634
|
-
function findRefUsage(document, ref) {
|
|
3635
|
-
const found = [];
|
|
3636
|
-
yaml.visit(document, { Pair(_, { key, value }) {
|
|
3637
|
-
if (!yaml.isScalar(key) || key.value !== "$ref" || !yaml.isScalar(value) || value.value !== ref || !value.range) return;
|
|
3638
|
-
found.push(toRange(value.range));
|
|
3639
|
-
return yaml.visit.BREAK;
|
|
3640
|
-
} });
|
|
3641
|
-
return found[0];
|
|
3642
|
-
}
|
|
3643
|
-
/**
|
|
3644
|
-
* One pass over JSON text, so a located diagnostic in a large JSON spec skips `yaml.parseDocument`.
|
|
3645
|
-
* Holds the range of every key and array item by pointer, and the first usage of every `$ref`, as
|
|
3646
|
-
* `findPointer` and `findRefUsage` return them. Throws where the text breaks JSON syntax, such as a
|
|
3647
|
-
* comment, an unquoted key, or an unquoted `$ref` value. Other bare values, such as `object`, pass.
|
|
3648
|
-
*/
|
|
3649
|
-
function indexJson(source) {
|
|
3650
|
-
const pointers = /* @__PURE__ */ new Map();
|
|
3651
|
-
const refs = /* @__PURE__ */ new Map();
|
|
3652
|
-
let at = source.indexOf("{");
|
|
3653
|
-
const fail = () => {
|
|
3654
|
-
throw new SyntaxError(`Unexpected character at ${at}`);
|
|
3655
|
-
};
|
|
3656
|
-
const skipSpace = () => {
|
|
3657
|
-
for (let code = source.charCodeAt(at); code === 32 || code === 9 || code === 10 || code === 13; code = source.charCodeAt(++at));
|
|
3658
|
-
};
|
|
3659
|
-
const readString = () => {
|
|
3660
|
-
if (source[at] !== "\"") fail();
|
|
3661
|
-
const start = at;
|
|
3662
|
-
for (at++; source.charCodeAt(at) !== 34; at++) {
|
|
3663
|
-
if (at >= source.length) fail();
|
|
3664
|
-
if (source.charCodeAt(at) === 92) at++;
|
|
3665
|
-
}
|
|
3666
|
-
const raw = source.slice(start + 1, at++);
|
|
3667
|
-
return {
|
|
3668
|
-
text: raw.includes("\\") ? JSON.parse(source.slice(start, at)) : raw,
|
|
3669
|
-
start,
|
|
3670
|
-
end: at
|
|
3671
|
-
};
|
|
3672
|
-
};
|
|
3673
|
-
const expect = (char) => {
|
|
3674
|
-
skipSpace();
|
|
3675
|
-
if (source[at++] !== char) fail();
|
|
3676
|
-
skipSpace();
|
|
3677
|
-
};
|
|
3678
|
-
const walk = (pointer) => {
|
|
3679
|
-
const open = source[at];
|
|
3680
|
-
if (open === "\"") {
|
|
3681
|
-
readString();
|
|
3682
|
-
return;
|
|
3683
|
-
}
|
|
3684
|
-
if (open !== "{" && open !== "[") {
|
|
3685
|
-
const start = at;
|
|
3686
|
-
while (at < source.length && !/[\s,\]}]/.test(source[at])) at++;
|
|
3687
|
-
if (at === start) fail();
|
|
3688
|
-
return;
|
|
3689
|
-
}
|
|
3690
|
-
const close = open === "{" ? "}" : "]";
|
|
3691
|
-
at++;
|
|
3692
|
-
for (let index = 0;; index++) {
|
|
3693
|
-
skipSpace();
|
|
3694
|
-
if (source[at] === close) break;
|
|
3695
|
-
if (index > 0) expect(",");
|
|
3696
|
-
if (open === "[") {
|
|
3697
|
-
const start = at;
|
|
3698
|
-
const next = `${pointer}/${index}`;
|
|
3699
|
-
walk(next);
|
|
3700
|
-
if (!pointers.has(next)) pointers.set(next, {
|
|
3701
|
-
start,
|
|
3702
|
-
end: at
|
|
3703
|
-
});
|
|
3704
|
-
continue;
|
|
3705
|
-
}
|
|
3706
|
-
const key = readString();
|
|
3707
|
-
const next = `${pointer}/${escapePointerToken(key.text)}`;
|
|
3708
|
-
if (!pointers.has(next)) pointers.set(next, {
|
|
3709
|
-
start: key.start,
|
|
3710
|
-
end: key.end
|
|
3711
|
-
});
|
|
3712
|
-
expect(":");
|
|
3713
|
-
if (key.text !== "$ref" || source[at] === "{" || source[at] === "[") {
|
|
3714
|
-
walk(next);
|
|
3715
|
-
continue;
|
|
3716
|
-
}
|
|
3717
|
-
const value = readString();
|
|
3718
|
-
if (!refs.has(value.text)) refs.set(value.text, {
|
|
3719
|
-
start: value.start,
|
|
3720
|
-
end: value.end
|
|
3721
|
-
});
|
|
3722
|
-
}
|
|
3723
|
-
at++;
|
|
3724
|
-
};
|
|
3725
|
-
walk("#");
|
|
3726
|
-
return {
|
|
3727
|
-
pointers,
|
|
3728
|
-
refs
|
|
3729
|
-
};
|
|
3730
|
-
}
|
|
3731
|
-
/** Indexes plain JSON text; anything else goes to the YAML parser. */
|
|
3732
|
-
function parse(source) {
|
|
3733
|
-
if (/^[ \t\n\r]*\{/.test(source)) try {
|
|
3734
|
-
return {
|
|
3735
|
-
source,
|
|
3736
|
-
json: indexJson(source)
|
|
3737
|
-
};
|
|
3738
|
-
} catch {}
|
|
3739
|
-
return {
|
|
3740
|
-
source,
|
|
3741
|
-
document: yaml.parseDocument(source)
|
|
3742
|
-
};
|
|
3743
|
-
}
|
|
3744
|
-
/**
|
|
3745
|
-
* Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
|
|
3746
|
-
*/
|
|
3747
|
-
const locate = (source, location) => {
|
|
3748
|
-
if (!("pointer" in location)) return void 0;
|
|
3749
|
-
cached = cached?.source === source ? {
|
|
3750
|
-
...cached,
|
|
3751
|
-
source
|
|
3752
|
-
} : parse(source);
|
|
3753
|
-
const ref = "ref" in location ? location.ref : void 0;
|
|
3754
|
-
if ("json" in cached) {
|
|
3755
|
-
const { pointers, refs } = cached.json;
|
|
3756
|
-
return pointers.get(location.pointer) ?? (ref ? refs.get(ref) : void 0);
|
|
3757
|
-
}
|
|
3758
|
-
const { document } = cached;
|
|
3759
|
-
return findPointer(document, location.pointer) ?? (ref ? findRefUsage(document, ref) : void 0);
|
|
3760
|
-
};
|
|
3761
3857
|
// src/adapter.ts
|
|
3762
3858
|
/**
|
|
3763
3859
|
* The `name` of `@kubb/adapter-oas`, used to identify this adapter in a Kubb config.
|