@kubb/adapter-oas 5.28.5 → 5.29.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/README.md +1 -1
- package/dist/index.cjs +1815 -1628
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1816 -1628
- package/dist/index.js.map +1 -1
- package/dist/{validateSpec-BTbUBxQB.js → validateSpec-BvvS99hP.js} +3 -2
- package/dist/validateSpec-BvvS99hP.js.map +1 -0
- package/dist/validateSpec-DCgkUD1L.js +1 -0
- package/dist/{validateSpec-Bxf2WZ1Y.cjs → validateSpec-DXIH7vw7.cjs} +2 -1
- package/dist/validateSpec-DXIH7vw7.cjs.map +1 -0
- package/dist/validateWorker.js +1 -1
- package/package.json +6 -7
- package/dist/validateSpec-BTbUBxQB.js.map +0 -1
- package/dist/validateSpec-Bxf2WZ1Y.cjs.map +0 -1
- package/dist/validateSpec-DL7ObLHI.js +0 -1
package/dist/index.js
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
import "./rolldown-runtime-BowF1xt-.js";
|
|
2
|
-
import { defineExtension,
|
|
2
|
+
import { defineExtension, pascalCase } from "@kubb/core";
|
|
3
3
|
import { Diagnostics, Url, ast, childName, enumPrimitive, enumPropName, macroDedupeUnion, macroDiscriminatorEnum, macroEnumName, mergeAdjacentObjectsLazy } from "@kubb/kit";
|
|
4
|
-
import {
|
|
5
|
-
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
6
5
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
import { readFile } from "node:fs";
|
|
8
6
|
import { upgrade } from "@scalar/openapi-upgrader";
|
|
9
|
-
import { bundle } from "api-ref-bundler";
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
7
|
+
import { bundle, parseRef } from "api-ref-bundler";
|
|
8
|
+
import { readFile } from "node:fs";
|
|
9
|
+
import { access } from "node:fs/promises";
|
|
12
10
|
import { COLLECTION_STYLE, CORE_SCHEMA, EVENT_ID, SCALAR_STYLE, constructFromEvents, load, parseEvents } from "js-yaml";
|
|
13
|
-
import { parse as parse$1 } from "yaml";
|
|
14
11
|
// kubb-extension.json
|
|
15
12
|
var compatibility = {
|
|
16
13
|
"kubb": ">=5.8.3",
|
|
17
14
|
"node": ">=22.22"
|
|
18
15
|
};
|
|
19
16
|
// package.json
|
|
20
|
-
var version = "5.
|
|
17
|
+
var version = "5.29.0";
|
|
21
18
|
// src/constants.ts
|
|
22
19
|
/**
|
|
23
20
|
* Default parser options applied when no explicit options are provided.
|
|
@@ -41,6 +38,29 @@ const DEFAULT_PARSER_OPTIONS = {
|
|
|
41
38
|
*/
|
|
42
39
|
const SCHEMA_REF_PREFIX = "#/components/schemas/";
|
|
43
40
|
/**
|
|
41
|
+
* Keys whose value maps names the user chose to entries: `properties`-like schema maps, schema definitions,
|
|
42
|
+
* and the OpenAPI component and path-level maps. An entry name here is never a keyword, so `x-rate-limit`
|
|
43
|
+
* under `headers` is a real header, not a vendor extension, and a property named `default` holds a schema.
|
|
44
|
+
*/
|
|
45
|
+
const namedEntryKeys = /* @__PURE__ */ new Set([
|
|
46
|
+
"properties",
|
|
47
|
+
"patternProperties",
|
|
48
|
+
"dependentSchemas",
|
|
49
|
+
"$defs",
|
|
50
|
+
"definitions",
|
|
51
|
+
"schemas",
|
|
52
|
+
"headers",
|
|
53
|
+
"responses",
|
|
54
|
+
"parameters",
|
|
55
|
+
"requestBodies",
|
|
56
|
+
"examples",
|
|
57
|
+
"links",
|
|
58
|
+
"callbacks",
|
|
59
|
+
"securitySchemes",
|
|
60
|
+
"securityDefinitions",
|
|
61
|
+
"pathItems"
|
|
62
|
+
]);
|
|
63
|
+
/**
|
|
44
64
|
* HTTP methods that count as operations on an OpenAPI path item. Other keys
|
|
45
65
|
* (`parameters`, `summary`, `$ref`, vendor extensions) are skipped when iterating operations.
|
|
46
66
|
* `query` is the OpenAPI 3.2 field for the HTTP QUERY method (RFC 10008).
|
|
@@ -135,6 +155,13 @@ const formatMap = {
|
|
|
135
155
|
double: "number"
|
|
136
156
|
};
|
|
137
157
|
/**
|
|
158
|
+
* The `formatMap` entry for `format`. Reads own keys only, so `constructor` or `toString` never match.
|
|
159
|
+
*/
|
|
160
|
+
function getFormatType(format) {
|
|
161
|
+
const formats = formatMap;
|
|
162
|
+
return Object.hasOwn(formats, format) ? formats[format] : void 0;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
138
165
|
* Vendor extension keys that attach human-readable labels to enum values, checked in priority order.
|
|
139
166
|
*/
|
|
140
167
|
const enumExtensionKeys = ["x-enumNames", "x-enum-varnames"];
|
|
@@ -143,8 +170,29 @@ const enumExtensionKeys = ["x-enumNames", "x-enum-varnames"];
|
|
|
143
170
|
*/
|
|
144
171
|
const enumDescriptionKeys = ["x-enumDescriptions", "x-enum-descriptions"];
|
|
145
172
|
// src/emit/discriminator/propagate.ts
|
|
173
|
+
function findUnion(schema) {
|
|
174
|
+
return ast.schema.narrow(schema, "union") ?? ast.schema.narrow(schema, "intersection")?.members?.map((member) => ast.schema.narrow(member, "union")).find((union) => union);
|
|
175
|
+
}
|
|
176
|
+
function pinnedValues({ objNode, propertyName }) {
|
|
177
|
+
const prop = objNode.properties.find((p) => p.name === propertyName);
|
|
178
|
+
return (prop ? ast.schema.narrow(prop.schema, "enum") : null)?.enumValues?.filter((v) => v !== null) ?? [];
|
|
179
|
+
}
|
|
180
|
+
function memberDiscriminant({ member, propertyName }) {
|
|
181
|
+
const members = ast.schema.narrow(member, "intersection")?.members ?? [];
|
|
182
|
+
const refNode = members.map((m) => ast.schema.narrow(m, "ref")).find(Boolean);
|
|
183
|
+
const objNode = members.map((m) => ast.schema.narrow(m, "object")).find(Boolean);
|
|
184
|
+
if (!refNode?.name || !objNode) return null;
|
|
185
|
+
const enumValues = pinnedValues({
|
|
186
|
+
objNode,
|
|
187
|
+
propertyName
|
|
188
|
+
});
|
|
189
|
+
return enumValues.length ? {
|
|
190
|
+
name: refNode.targetName ?? refNode.name,
|
|
191
|
+
enumValues
|
|
192
|
+
} : null;
|
|
193
|
+
}
|
|
146
194
|
/**
|
|
147
|
-
* Maps each child schema name to its discriminator patch data by scanning the given
|
|
195
|
+
* Maps each child schema's emitted name to its discriminator patch data by scanning the given
|
|
148
196
|
* top-level AST schema nodes for union schemas that carry a `discriminatorPropertyName`.
|
|
149
197
|
*
|
|
150
198
|
* Called on a small pre-parsed subset of schemas (only the discriminator parents)
|
|
@@ -153,33 +201,21 @@ const enumDescriptionKeys = ["x-enumDescriptions", "x-enum-descriptions"];
|
|
|
153
201
|
function buildDiscriminatorChildMap(schemas) {
|
|
154
202
|
const childMap = /* @__PURE__ */ new Map();
|
|
155
203
|
for (const schema of schemas) {
|
|
156
|
-
const unionNode =
|
|
204
|
+
const unionNode = findUnion(schema);
|
|
157
205
|
if (!unionNode?.discriminatorPropertyName || !unionNode.members) continue;
|
|
158
|
-
const
|
|
159
|
-
for (const member of members) {
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (!enumNode?.enumValues?.length) continue;
|
|
172
|
-
const enumValues = enumNode.enumValues.filter((v) => v !== null);
|
|
173
|
-
if (!enumValues.length) continue;
|
|
174
|
-
const existing = childMap.get(refNode.name);
|
|
175
|
-
if (!existing) {
|
|
176
|
-
childMap.set(refNode.name, {
|
|
177
|
-
propertyName: discriminatorPropertyName,
|
|
178
|
-
enumValues: [...new Set(enumValues)]
|
|
179
|
-
});
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
existing.enumValues = [.../* @__PURE__ */ new Set([...existing.enumValues, ...enumValues])];
|
|
206
|
+
const propertyName = unionNode.discriminatorPropertyName;
|
|
207
|
+
for (const member of unionNode.members) {
|
|
208
|
+
const child = memberDiscriminant({
|
|
209
|
+
member,
|
|
210
|
+
propertyName
|
|
211
|
+
});
|
|
212
|
+
if (!child) continue;
|
|
213
|
+
const existing = childMap.get(child.name);
|
|
214
|
+
const enumValues = [.../* @__PURE__ */ new Set([...existing?.enumValues ?? [], ...child.enumValues])];
|
|
215
|
+
childMap.set(child.name, {
|
|
216
|
+
propertyName: existing?.propertyName ?? propertyName,
|
|
217
|
+
enumValues
|
|
218
|
+
});
|
|
183
219
|
}
|
|
184
220
|
}
|
|
185
221
|
return childMap;
|
|
@@ -385,27 +421,6 @@ async function read(path) {
|
|
|
385
421
|
if (runtime.isBun) return Bun.file(path).text();
|
|
386
422
|
return new Promise((resolvePromise, reject) => readFile(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
|
|
387
423
|
}
|
|
388
|
-
/**
|
|
389
|
-
* Resolves to `true` when `path` is `parent` itself or nested inside it. Both sides are resolved
|
|
390
|
-
* to absolute paths first, so relative and `..`-containing inputs compare correctly.
|
|
391
|
-
*
|
|
392
|
-
* Guards a destructive or an out-of-tree operation: before wiping an output directory, check that
|
|
393
|
-
* it does not contain the project root, and before loading a path a caller supplied, check that it
|
|
394
|
-
* did not escape the directory it is allowed to read from.
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
* isPathInside('./src/gen', '.') // true — nested inside the root
|
|
398
|
-
* isPathInside('.', '.') // true — the same directory counts as inside
|
|
399
|
-
* isPathInside('.', './src/gen') // false — the root is not inside its own output
|
|
400
|
-
* isPathInside('../other', '.') // false — escapes the root
|
|
401
|
-
*/
|
|
402
|
-
function isPathInside(path, parent) {
|
|
403
|
-
const resolvedPath = resolve(path);
|
|
404
|
-
const resolvedParent = resolve(parent);
|
|
405
|
-
if (resolvedPath === resolvedParent) return true;
|
|
406
|
-
const rel = relative(resolvedParent, resolvedPath);
|
|
407
|
-
return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
408
|
-
}
|
|
409
424
|
// src/oas.ts
|
|
410
425
|
const legacyDocuments = /* @__PURE__ */ new WeakSet();
|
|
411
426
|
/**
|
|
@@ -570,11 +585,13 @@ var Refs = class {
|
|
|
570
585
|
const trimmed = $ref.trim();
|
|
571
586
|
if (trimmed === "" || !trimmed.startsWith("#")) return {
|
|
572
587
|
applicable: false,
|
|
588
|
+
found: false,
|
|
573
589
|
value: null
|
|
574
590
|
};
|
|
575
591
|
const pointer = trimmed.substring(1);
|
|
576
592
|
if (this.#pointerCache.has(pointer)) return {
|
|
577
593
|
applicable: true,
|
|
594
|
+
found: true,
|
|
578
595
|
value: this.#pointerCache.get(pointer)
|
|
579
596
|
};
|
|
580
597
|
let current;
|
|
@@ -583,21 +600,28 @@ var Refs = class {
|
|
|
583
600
|
} catch {
|
|
584
601
|
current = void 0;
|
|
585
602
|
}
|
|
586
|
-
if (current)
|
|
603
|
+
if (current === void 0) return {
|
|
604
|
+
applicable: true,
|
|
605
|
+
found: false,
|
|
606
|
+
value: null
|
|
607
|
+
};
|
|
608
|
+
this.#pointerCache.set(pointer, current);
|
|
587
609
|
return {
|
|
588
610
|
applicable: true,
|
|
589
|
-
|
|
611
|
+
found: true,
|
|
612
|
+
value: current
|
|
590
613
|
};
|
|
591
614
|
}
|
|
592
615
|
/**
|
|
593
|
-
* Resolves a local `#/...` JSON pointer. Returns `null` for an empty
|
|
616
|
+
* Resolves a local `#/...` JSON pointer. Returns `null` for an empty, non-local or missing ref.
|
|
617
|
+
* A target that exists is returned as is, even when it is falsy, such as a boolean `false` schema.
|
|
594
618
|
* `report: true` (default) reports a `refNotFound` diagnostic into the active build (or throws
|
|
595
619
|
* outside one) when the pointer cannot be resolved. `report: false` resolves to `null` silently,
|
|
596
620
|
* for a speculative lookup where a missing ref is not an error.
|
|
597
621
|
*/
|
|
598
622
|
resolve(refPath, options) {
|
|
599
|
-
const { applicable, value } = this.#walkPointer(refPath);
|
|
600
|
-
if (!applicable ||
|
|
623
|
+
const { applicable, found, value } = this.#walkPointer(refPath);
|
|
624
|
+
if (!applicable || found || options?.report === false) return value;
|
|
601
625
|
const diagnostic = {
|
|
602
626
|
code: Diagnostics.code.refNotFound,
|
|
603
627
|
severity: "error",
|
|
@@ -628,7 +652,7 @@ var Refs = class {
|
|
|
628
652
|
if (Diagnostics.isError(error) && error.diagnostic.code === Diagnostics.code.refNotFound) return null;
|
|
629
653
|
throw error;
|
|
630
654
|
}
|
|
631
|
-
if (
|
|
655
|
+
if (referenced === null) return null;
|
|
632
656
|
this.#resolvingRefs.add(refPath);
|
|
633
657
|
try {
|
|
634
658
|
return parse({ schema: referenced });
|
|
@@ -674,28 +698,43 @@ const DATA_KEYS = /* @__PURE__ */ new Set([
|
|
|
674
698
|
"enum",
|
|
675
699
|
"default"
|
|
676
700
|
]);
|
|
677
|
-
const NAMED_ENTRY_KEYS = /* @__PURE__ */ new Set([
|
|
678
|
-
"properties",
|
|
679
|
-
"patternProperties",
|
|
680
|
-
"$defs",
|
|
681
|
-
"definitions",
|
|
682
|
-
"schemas",
|
|
683
|
-
"dependentSchemas"
|
|
684
|
-
]);
|
|
685
701
|
const TARGET_KEYS = [
|
|
686
702
|
"$id",
|
|
687
703
|
"$anchor",
|
|
688
704
|
"$dynamicAnchor"
|
|
689
705
|
];
|
|
690
|
-
function
|
|
706
|
+
function isNamedMap({ node, keyword }) {
|
|
707
|
+
return keyword !== void 0 && !Array.isArray(node) && namedEntryKeys.has(keyword);
|
|
708
|
+
}
|
|
709
|
+
function childKeyword({ node, key, named }) {
|
|
710
|
+
return named || Array.isArray(node) ? void 0 : key;
|
|
711
|
+
}
|
|
712
|
+
function walk(node, pointer, visit, keyword) {
|
|
691
713
|
if (!node || typeof node !== "object") return;
|
|
692
714
|
if (!Array.isArray(node)) visit(node, pointer);
|
|
693
|
-
const named =
|
|
715
|
+
const named = isNamedMap({
|
|
716
|
+
node,
|
|
717
|
+
keyword
|
|
718
|
+
});
|
|
694
719
|
for (const [key, value] of Object.entries(node)) {
|
|
695
|
-
if (!named && (
|
|
696
|
-
walk(value, `${pointer}/${escapePointerToken(key)}`, visit,
|
|
720
|
+
if (!named && isDataOrExtension(key)) continue;
|
|
721
|
+
walk(value, `${pointer}/${escapePointerToken(key)}`, visit, childKeyword({
|
|
722
|
+
node,
|
|
723
|
+
key,
|
|
724
|
+
named
|
|
725
|
+
}));
|
|
697
726
|
}
|
|
698
727
|
}
|
|
728
|
+
function isDataOrExtension(key) {
|
|
729
|
+
return DATA_KEYS.has(key) || key.startsWith("x-");
|
|
730
|
+
}
|
|
731
|
+
function scanNode({ scan, node, readsIds }) {
|
|
732
|
+
const record = node;
|
|
733
|
+
const ref = record["$ref"];
|
|
734
|
+
if (typeof ref === "string" && !ref.startsWith("#")) scan.externalRef = true;
|
|
735
|
+
if (typeof ref === "string" && ref.startsWith("#/paths/")) scan.pathRefs = true;
|
|
736
|
+
if (readsIds && !Array.isArray(node) && TARGET_KEYS.some((key) => typeof record[key] === "string")) scan.schemaIds = true;
|
|
737
|
+
}
|
|
699
738
|
/** Answers, in one walk and without building JSON pointers, what the loading steps after it need to know. */
|
|
700
739
|
function scanDocument(document) {
|
|
701
740
|
const scan = {
|
|
@@ -703,23 +742,27 @@ function scanDocument(document) {
|
|
|
703
742
|
schemaIds: false,
|
|
704
743
|
pathRefs: false
|
|
705
744
|
};
|
|
706
|
-
const visit = ({ node, readsIds,
|
|
745
|
+
const visit = ({ node, readsIds, keyword }) => {
|
|
707
746
|
if (!node || typeof node !== "object") return;
|
|
708
|
-
|
|
747
|
+
scanNode({
|
|
748
|
+
scan,
|
|
749
|
+
node,
|
|
750
|
+
readsIds
|
|
751
|
+
});
|
|
752
|
+
const named = isNamedMap({
|
|
753
|
+
node,
|
|
754
|
+
keyword
|
|
755
|
+
});
|
|
709
756
|
const record = node;
|
|
710
|
-
const
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
readsIds: childReadsIds,
|
|
720
|
-
parentKey: isArray ? parentKey : key
|
|
721
|
-
});
|
|
722
|
-
}
|
|
757
|
+
for (const key in record) visit({
|
|
758
|
+
node: record[key],
|
|
759
|
+
readsIds: readsIds && (named || !isDataOrExtension(key)),
|
|
760
|
+
keyword: childKeyword({
|
|
761
|
+
node,
|
|
762
|
+
key,
|
|
763
|
+
named
|
|
764
|
+
})
|
|
765
|
+
});
|
|
723
766
|
};
|
|
724
767
|
visit({
|
|
725
768
|
node: document,
|
|
@@ -763,7 +806,45 @@ function resolveSchemaIds(document) {
|
|
|
763
806
|
}
|
|
764
807
|
// src/sourceLocator.ts
|
|
765
808
|
const flowGap = /(?:[\s,]|#[^\n]*)*/y;
|
|
766
|
-
|
|
809
|
+
/**
|
|
810
|
+
* How long the indexes of the last sources stay in memory after their last use, in milliseconds.
|
|
811
|
+
*/
|
|
812
|
+
const INDEX_IDLE_MS = 6e4;
|
|
813
|
+
var IndexCache = class IndexCache {
|
|
814
|
+
#indexes = IndexCache.#createStore();
|
|
815
|
+
#lastUsed = 0;
|
|
816
|
+
#timer;
|
|
817
|
+
static #createStore() {
|
|
818
|
+
return memo.create({ max: 8 });
|
|
819
|
+
}
|
|
820
|
+
get({ source, compute }) {
|
|
821
|
+
const index = memo(this.#indexes, source, compute);
|
|
822
|
+
this.#keep();
|
|
823
|
+
return index;
|
|
824
|
+
}
|
|
825
|
+
set({ source, index }) {
|
|
826
|
+
this.#indexes.set(source, index);
|
|
827
|
+
this.#keep();
|
|
828
|
+
}
|
|
829
|
+
#keep() {
|
|
830
|
+
this.#lastUsed = Date.now();
|
|
831
|
+
if (!this.#timer) this.#schedule(INDEX_IDLE_MS);
|
|
832
|
+
}
|
|
833
|
+
#schedule(ms) {
|
|
834
|
+
this.#timer = setTimeout(this.#dropWhenIdle, ms);
|
|
835
|
+
this.#timer.unref?.();
|
|
836
|
+
}
|
|
837
|
+
#dropWhenIdle = () => {
|
|
838
|
+
const remaining = this.#lastUsed + INDEX_IDLE_MS - Date.now();
|
|
839
|
+
if (remaining > 0) return this.#schedule(remaining);
|
|
840
|
+
this.#indexes = IndexCache.#createStore();
|
|
841
|
+
this.#timer = void 0;
|
|
842
|
+
};
|
|
843
|
+
};
|
|
844
|
+
function createIndexCache() {
|
|
845
|
+
return new IndexCache();
|
|
846
|
+
}
|
|
847
|
+
const indexes = createIndexCache();
|
|
767
848
|
function toKeyText(text) {
|
|
768
849
|
if (!/^[-+.~\d]|^(?:null|true|false)$/i.test(text)) return text;
|
|
769
850
|
try {
|
|
@@ -955,17 +1036,27 @@ function parse(source) {
|
|
|
955
1036
|
return;
|
|
956
1037
|
}
|
|
957
1038
|
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Keeps the event index of a source the adapter already parsed, so `locate` does not parse it again.
|
|
1041
|
+
*/
|
|
958
1042
|
function rememberEvents({ source, events }) {
|
|
959
|
-
indexes.set(
|
|
1043
|
+
indexes.set({
|
|
1044
|
+
source,
|
|
1045
|
+
index: indexEvents(events)
|
|
1046
|
+
});
|
|
960
1047
|
}
|
|
961
1048
|
const withoutAllOf = (pointer) => pointer.replace(/\/allOf\/\d+(?=\/|$)/g, "");
|
|
962
1049
|
/**
|
|
963
1050
|
* Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source. A pointer through an `allOf`
|
|
964
|
-
* the spec does not have falls back to the same pointer without its `allOf` steps.
|
|
1051
|
+
* the spec does not have falls back to the same pointer without its `allOf` steps. The indexes of the last few
|
|
1052
|
+
* sources are reused until they go unused for `INDEX_IDLE_MS`.
|
|
965
1053
|
*/
|
|
966
1054
|
const locate = (source, location) => {
|
|
967
1055
|
if (!("pointer" in location)) return void 0;
|
|
968
|
-
const index =
|
|
1056
|
+
const index = indexes.get({
|
|
1057
|
+
source,
|
|
1058
|
+
compute: () => parse(source)
|
|
1059
|
+
});
|
|
969
1060
|
if (!index) return void 0;
|
|
970
1061
|
const locator = createSourceLocator({
|
|
971
1062
|
index,
|
|
@@ -980,23 +1071,10 @@ const urlRegExp = /^https?:\/+/i;
|
|
|
980
1071
|
/** How long one request for a document or a `$ref` may take before Kubb gives up on it. */
|
|
981
1072
|
const FETCH_TIMEOUT_MS = 3e4;
|
|
982
1073
|
/**
|
|
983
|
-
* Whether `
|
|
984
|
-
* public. A host that does not resolve, or not within the request timeout, counts as not public,
|
|
985
|
-
* because the check cannot prove it public. `signal` cancels the lookup.
|
|
986
|
-
*
|
|
987
|
-
* shortcut: the name is resolved again when `fetch` connects, so a host that changes its answer in
|
|
988
|
-
* between can still reach a private address. Upgrade to a pinned lookup, like `@kubb/studio`'s
|
|
989
|
-
* `publicFetch`, if a sandbox needs that guarantee.
|
|
1074
|
+
* Whether `error` is a harness refusal. It checks the code, since a host may load another copy of `@kubb/core`.
|
|
990
1075
|
*/
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
if (isIP(host)) return !isPublicAddress(host);
|
|
994
|
-
const deadline = withTimeout(signal);
|
|
995
|
-
deadline.throwIfAborted();
|
|
996
|
-
const stopped = new Promise((_, reject) => deadline.addEventListener("abort", () => reject(deadline.reason), { once: true }));
|
|
997
|
-
const addresses = await Promise.race([lookup(host, { all: true }), stopped]).catch(() => []);
|
|
998
|
-
signal?.throwIfAborted();
|
|
999
|
-
return addresses.length === 0 || addresses.some(({ address }) => !isPublicAddress(address));
|
|
1076
|
+
function isAccessRefused(error) {
|
|
1077
|
+
return error?.diagnostic?.code === Diagnostics.code.accessRefused;
|
|
1000
1078
|
}
|
|
1001
1079
|
function withTimeout(signal) {
|
|
1002
1080
|
const timeout = AbortSignal.timeout(FETCH_TIMEOUT_MS);
|
|
@@ -1018,19 +1096,17 @@ function helpForStatus(status) {
|
|
|
1018
1096
|
if (status >= 500) return "The server failed while serving the document. Check that it is healthy, then run Kubb again.";
|
|
1019
1097
|
return "Open the URL in a browser or with `curl` to see what the server returns, then point `input` at a URL that serves the OpenAPI document.";
|
|
1020
1098
|
}
|
|
1021
|
-
async function fetchSource(url, { signal,
|
|
1099
|
+
async function fetchSource(url, { signal, fetch = globalThis.fetch }) {
|
|
1022
1100
|
const deadline = withTimeout(signal);
|
|
1023
1101
|
try {
|
|
1024
|
-
const response = await fetch(url, {
|
|
1025
|
-
signal: deadline,
|
|
1026
|
-
redirect: publicOnly ? "error" : "follow"
|
|
1027
|
-
});
|
|
1102
|
+
const response = await fetch(url, { signal: deadline });
|
|
1028
1103
|
return {
|
|
1029
1104
|
response,
|
|
1030
1105
|
body: response.ok ? await response.text() : ""
|
|
1031
1106
|
};
|
|
1032
1107
|
} catch (error) {
|
|
1033
1108
|
if (signal?.aborted) throw signal.reason;
|
|
1109
|
+
if (isAccessRefused(error)) throw error;
|
|
1034
1110
|
const reason = deadline.aborted ? `no answer within ${FETCH_TIMEOUT_MS / 1e3} seconds` : describeFetchFailure(error);
|
|
1035
1111
|
throw new Diagnostics.Error({
|
|
1036
1112
|
code: Diagnostics.code.inputUnreachable,
|
|
@@ -1058,7 +1134,7 @@ async function readSource(sourcePath, options) {
|
|
|
1058
1134
|
}
|
|
1059
1135
|
return body;
|
|
1060
1136
|
}
|
|
1061
|
-
return read(sourcePath);
|
|
1137
|
+
return (options.read ?? read)(sourcePath);
|
|
1062
1138
|
}
|
|
1063
1139
|
/**
|
|
1064
1140
|
* Reads and parses one source file or URL referenced during bundling: YAML/JSON is parsed into an
|
|
@@ -1084,9 +1160,8 @@ async function resolveSource(sourcePath, { remember, ...options } = {}) {
|
|
|
1084
1160
|
* value, as every JSON parser does. YAML flow syntax also starts with `{`, so a parse failure falls
|
|
1085
1161
|
* through to YAML. Pass `json: false` for a source known to be YAML.
|
|
1086
1162
|
*
|
|
1087
|
-
* `js-yaml`
|
|
1088
|
-
*
|
|
1089
|
-
* `yaml` does for YAML 1.2.
|
|
1163
|
+
* `js-yaml` rejects duplicate keys instead of keeping the last one. Its core schema keeps
|
|
1164
|
+
* timestamps as strings and `<<` as a plain key, as YAML 1.2 does.
|
|
1090
1165
|
*/
|
|
1091
1166
|
function parseYaml(data, { origin, help, json = true, remember = false }) {
|
|
1092
1167
|
if (json && /^\s*[[{]/.test(data)) try {
|
|
@@ -1098,16 +1173,11 @@ function parseYaml(data, { origin, help, json = true, remember = false }) {
|
|
|
1098
1173
|
source: data,
|
|
1099
1174
|
schema: CORE_SCHEMA
|
|
1100
1175
|
});
|
|
1101
|
-
if (documents.length === 1
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
return documents[0];
|
|
1107
|
-
}
|
|
1108
|
-
} catch {}
|
|
1109
|
-
try {
|
|
1110
|
-
return parse$1(data);
|
|
1176
|
+
if (remember && documents.length === 1) rememberEvents({
|
|
1177
|
+
source: data,
|
|
1178
|
+
events
|
|
1179
|
+
});
|
|
1180
|
+
return documents[0];
|
|
1111
1181
|
} catch (error) {
|
|
1112
1182
|
throw new Diagnostics.Error({
|
|
1113
1183
|
code: Diagnostics.code.invalidDocument,
|
|
@@ -1162,29 +1232,18 @@ function inlinePathRefs(node, resolve, visiting = []) {
|
|
|
1162
1232
|
for (const [key, value] of Object.entries(node)) node[key] = inlinePathRefs(value, resolve, visiting);
|
|
1163
1233
|
return node;
|
|
1164
1234
|
}
|
|
1165
|
-
const namedEntryKeys = /* @__PURE__ */ new Set([
|
|
1166
|
-
"properties",
|
|
1167
|
-
"patternProperties",
|
|
1168
|
-
"headers",
|
|
1169
|
-
"schemas",
|
|
1170
|
-
"responses",
|
|
1171
|
-
"parameters",
|
|
1172
|
-
"requestBodies",
|
|
1173
|
-
"examples",
|
|
1174
|
-
"links",
|
|
1175
|
-
"callbacks",
|
|
1176
|
-
"securitySchemes",
|
|
1177
|
-
"securityDefinitions",
|
|
1178
|
-
"pathItems",
|
|
1179
|
-
"definitions",
|
|
1180
|
-
"$defs"
|
|
1181
|
-
]);
|
|
1182
1235
|
/**
|
|
1183
1236
|
* True when `path` runs through an `x-` vendor extension. Kubb never reads an extension, so a `$ref`
|
|
1184
1237
|
* Kubb cannot read inside one cannot change the output.
|
|
1185
1238
|
*/
|
|
1186
1239
|
function isExtensionPath(path) {
|
|
1187
|
-
|
|
1240
|
+
let inNamedMap = false;
|
|
1241
|
+
for (const token of path) {
|
|
1242
|
+
const key = String(token);
|
|
1243
|
+
if (key.startsWith("x-") && !inNamedMap) return true;
|
|
1244
|
+
inNamedMap = !inNamedMap && namedEntryKeys.has(key);
|
|
1245
|
+
}
|
|
1246
|
+
return false;
|
|
1188
1247
|
}
|
|
1189
1248
|
/**
|
|
1190
1249
|
* Returns the base for relative `$ref`s: the 3.2 `$self` resolved against `pathOrUrl`, else `pathOrUrl`.
|
|
@@ -1202,16 +1261,6 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
1202
1261
|
}
|
|
1203
1262
|
}
|
|
1204
1263
|
/**
|
|
1205
|
-
* Whether `filePath` is inside one of `folders`. Links are followed first, so a link inside a
|
|
1206
|
-
* folder cannot point outside it.
|
|
1207
|
-
*/
|
|
1208
|
-
async function isInsideFolders(filePath, folders) {
|
|
1209
|
-
const real = (item) => realpath(resolve(item)).catch(() => resolve(item));
|
|
1210
|
-
const target = await real(filePath);
|
|
1211
|
-
const realFolders = await Promise.all(folders.map(real));
|
|
1212
|
-
return [...folders, ...realFolders].some((folder) => isPathInside(target, folder));
|
|
1213
|
-
}
|
|
1214
|
-
/**
|
|
1215
1264
|
* Bundles a multi-file OpenAPI document into a single document via `api-ref-bundler`.
|
|
1216
1265
|
*
|
|
1217
1266
|
* External file schemas are hoisted into named `components.schemas` entries, so a property
|
|
@@ -1228,10 +1277,9 @@ async function isInsideFolders(filePath, folders) {
|
|
|
1228
1277
|
* against the OpenAPI 3.2 `$self` when the document sets one. Every other file or URL read,
|
|
1229
1278
|
* including one that fails, lands in `sources`. A `$ref` that cannot be read reports
|
|
1230
1279
|
* `KUBB_REF_NOT_FOUND` at the key that holds it, as a warning when it sits inside an `x-` extension.
|
|
1231
|
-
* So does any local file from a URL input
|
|
1232
|
-
*
|
|
1233
|
-
*
|
|
1234
|
-
* `signal` cancels every request.
|
|
1280
|
+
* So does any local file from a URL input. With `harness`, every `$ref` file and every URL is read
|
|
1281
|
+
* through it, and a target its rules refuse reports `KUBB_ACCESS_REFUSED`. Without one, a `$ref` reads
|
|
1282
|
+
* any file and host. `signal` cancels every request.
|
|
1235
1283
|
*
|
|
1236
1284
|
* @example Local file
|
|
1237
1285
|
* `const document = await bundleDocument('./openapi.yaml')`
|
|
@@ -1239,16 +1287,45 @@ async function isInsideFolders(filePath, folders) {
|
|
|
1239
1287
|
* @example Remote URL
|
|
1240
1288
|
* `const document = await bundleDocument('https://example.com/openapi.yaml')`
|
|
1241
1289
|
*/
|
|
1242
|
-
async function bundleDocument(pathOrUrl, { document, sources = [],
|
|
1290
|
+
async function bundleDocument(pathOrUrl, { document, sources = [], harness, signal } = {}) {
|
|
1243
1291
|
const cache = memo.create();
|
|
1244
1292
|
const toKey = (sourcePath) => urlRegExp.test(sourcePath) ? new URL(sourcePath).href : sourcePath;
|
|
1245
|
-
const folders = urlRegExp.test(pathOrUrl) ? [] : fileRefs;
|
|
1246
|
-
const publicOnly = fileRefs !== void 0;
|
|
1247
1293
|
const refused = /* @__PURE__ */ new Set();
|
|
1248
|
-
const
|
|
1249
|
-
|
|
1250
|
-
if (
|
|
1251
|
-
|
|
1294
|
+
const harnessRefusals = /* @__PURE__ */ new Map();
|
|
1295
|
+
const read = ({ sourcePath, isRef }) => {
|
|
1296
|
+
if (!harness) return resolveSource(sourcePath, {
|
|
1297
|
+
remember: !isRef,
|
|
1298
|
+
signal
|
|
1299
|
+
});
|
|
1300
|
+
return resolveSource(sourcePath, {
|
|
1301
|
+
remember: !isRef,
|
|
1302
|
+
signal,
|
|
1303
|
+
fetch: (url, init) => harness.fetch(url, init),
|
|
1304
|
+
read: isRef ? (file) => harness.read(file) : void 0
|
|
1305
|
+
}).catch((error) => {
|
|
1306
|
+
if (isRef && isAccessRefused(error)) harnessRefusals.set(toKey(sourcePath), error.diagnostic);
|
|
1307
|
+
throw error;
|
|
1308
|
+
});
|
|
1309
|
+
};
|
|
1310
|
+
const refTarget = ({ value, baseFile }) => {
|
|
1311
|
+
const ref = value?.$ref;
|
|
1312
|
+
return typeof ref === "string" ? toKey(parseRef(ref, baseFile).filePath) : void 0;
|
|
1313
|
+
};
|
|
1314
|
+
const refDiagnostic = ({ message, target }) => {
|
|
1315
|
+
const harnessRefusal = target === void 0 ? void 0 : harnessRefusals.get(target);
|
|
1316
|
+
if (harnessRefusal) return harnessRefusal;
|
|
1317
|
+
if (target === void 0 || !refused.has(target)) return {
|
|
1318
|
+
code: Diagnostics.code.refNotFound,
|
|
1319
|
+
severity: "error",
|
|
1320
|
+
message: `Could not bundle an external \`$ref\`. ${message}.`,
|
|
1321
|
+
help: "Check that the file or URL the `$ref` points at exists and is readable, or fix the `$ref`."
|
|
1322
|
+
};
|
|
1323
|
+
return {
|
|
1324
|
+
code: Diagnostics.code.accessRefused,
|
|
1325
|
+
severity: "error",
|
|
1326
|
+
message: `Refused an external \`$ref\` to the local file ${target}.`,
|
|
1327
|
+
help: "This input reads no local file. Inline the schema, or serve the file from a URL."
|
|
1328
|
+
};
|
|
1252
1329
|
};
|
|
1253
1330
|
let entry = pathOrUrl;
|
|
1254
1331
|
let entryDocument = document;
|
|
@@ -1256,19 +1333,15 @@ async function bundleDocument(pathOrUrl, { document, sources = [], fileRefs, sig
|
|
|
1256
1333
|
if (entryDocument !== void 0 && toKey(sourcePath) === toKey(entry)) return entryDocument;
|
|
1257
1334
|
const key = toKey(sourcePath);
|
|
1258
1335
|
const isRef = key !== toKey(pathOrUrl);
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
const refusedFile = isRef && !isUrl && folders !== void 0 && !await isInsideFolders(sourcePath, folders);
|
|
1262
|
-
if (refusedUrl || refusedFile) {
|
|
1263
|
-
refused.add(sourcePath);
|
|
1336
|
+
if (isRef && !urlRegExp.test(sourcePath) && urlRegExp.test(pathOrUrl)) {
|
|
1337
|
+
refused.add(key);
|
|
1264
1338
|
throw new Error(`Refused ${sourcePath}`);
|
|
1265
1339
|
}
|
|
1266
1340
|
return memo(cache, key, () => {
|
|
1267
1341
|
sources.push(key);
|
|
1268
|
-
return
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
publicOnly: publicOnly && isRef
|
|
1342
|
+
return read({
|
|
1343
|
+
sourcePath,
|
|
1344
|
+
isRef
|
|
1272
1345
|
});
|
|
1273
1346
|
});
|
|
1274
1347
|
};
|
|
@@ -1280,15 +1353,18 @@ async function bundleDocument(pathOrUrl, { document, sources = [], fileRefs, sig
|
|
|
1280
1353
|
}
|
|
1281
1354
|
entry = getBaseUri(pathOrUrl, root);
|
|
1282
1355
|
entryDocument = root;
|
|
1283
|
-
return await bundle(entry, resolver, { hooks: { onError: (message, { path, state }) => {
|
|
1356
|
+
return await bundle(entry, resolver, { hooks: { onError: (message, { path, state, value }) => {
|
|
1284
1357
|
signal?.throwIfAborted();
|
|
1285
1358
|
const pointer = toKey(state.baseFile) === toKey(entry) ? `#/${path.map((token) => escapePointerToken(String(token))).join("/")}` : void 0;
|
|
1286
|
-
const refusedPath = [...refused].find((sourcePath) => message.includes(sourcePath));
|
|
1287
1359
|
const diagnostic = {
|
|
1288
|
-
|
|
1360
|
+
...refDiagnostic({
|
|
1361
|
+
message,
|
|
1362
|
+
target: refTarget({
|
|
1363
|
+
value,
|
|
1364
|
+
baseFile: state.baseFile
|
|
1365
|
+
})
|
|
1366
|
+
}),
|
|
1289
1367
|
severity: isExtensionPath(path) ? "warning" : "error",
|
|
1290
|
-
message: refusedPath ? `Refused an external \`$ref\` to ${urlRegExp.test(refusedPath) ? "a host that is not public" : "the local file"} ${refusedPath}.` : `Could not bundle an external \`$ref\`. ${message}.`,
|
|
1291
|
-
help: refusedPath ? refusedHelp(refusedPath) : "Check that the file or URL the `$ref` points at exists and is readable, or fix the `$ref`.",
|
|
1292
1368
|
...pointer && { location: {
|
|
1293
1369
|
kind: "schema",
|
|
1294
1370
|
pointer
|
|
@@ -1297,6 +1373,13 @@ async function bundleDocument(pathOrUrl, { document, sources = [], fileRefs, sig
|
|
|
1297
1373
|
if (!Diagnostics.report(diagnostic) && diagnostic.severity === "error") throw new Diagnostics.Error(diagnostic);
|
|
1298
1374
|
} } });
|
|
1299
1375
|
}
|
|
1376
|
+
function readInlineData(data) {
|
|
1377
|
+
if (typeof data !== "string") return structuredClone(data);
|
|
1378
|
+
return parseYaml(data, {
|
|
1379
|
+
origin: "the inline `input` string",
|
|
1380
|
+
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."
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1300
1383
|
const inlineInputName = "<inline input>";
|
|
1301
1384
|
/**
|
|
1302
1385
|
* Creates a `Document` from an `AdapterSource`.
|
|
@@ -1315,18 +1398,15 @@ const inlineInputName = "<inline input>";
|
|
|
1315
1398
|
* const document = await parseFromConfig({ type: 'data', data: '{"openapi":"3.0.0",...}' })
|
|
1316
1399
|
* ```
|
|
1317
1400
|
*/
|
|
1318
|
-
async function parseFromConfig(source, sources = [], {
|
|
1401
|
+
async function parseFromConfig(source, sources = [], { harness, signal } = {}) {
|
|
1319
1402
|
const raw = await (async () => {
|
|
1320
1403
|
if (source.type === "data") {
|
|
1321
|
-
const data =
|
|
1322
|
-
origin: "the inline `input` string",
|
|
1323
|
-
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."
|
|
1324
|
-
}) : structuredClone(source.data);
|
|
1404
|
+
const data = readInlineData(source.data);
|
|
1325
1405
|
const isObject = Boolean(data) && typeof data === "object";
|
|
1326
1406
|
if (source.root && isObject) return bundleDocument(join(source.root, inlineInputName), {
|
|
1327
1407
|
document: data,
|
|
1328
1408
|
sources,
|
|
1329
|
-
|
|
1409
|
+
harness,
|
|
1330
1410
|
signal
|
|
1331
1411
|
});
|
|
1332
1412
|
const scan = resolveTargets(data);
|
|
@@ -1336,7 +1416,7 @@ async function parseFromConfig(source, sources = [], { fileRefs, signal } = {})
|
|
|
1336
1416
|
await assertInputExists(source.path);
|
|
1337
1417
|
return bundleDocument(source.path, {
|
|
1338
1418
|
sources,
|
|
1339
|
-
|
|
1419
|
+
harness,
|
|
1340
1420
|
signal
|
|
1341
1421
|
});
|
|
1342
1422
|
})();
|
|
@@ -1408,28 +1488,17 @@ function reportInvalidSpec(message) {
|
|
|
1408
1488
|
* ```
|
|
1409
1489
|
*/
|
|
1410
1490
|
async function validateDocument(document, { throwOnError = false } = {}) {
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
if (throwOnError) throw new Diagnostics.Error(diagnostic);
|
|
1419
|
-
Diagnostics.report(diagnostic);
|
|
1420
|
-
return;
|
|
1421
|
-
}
|
|
1422
|
-
if (!throwOnError) {
|
|
1423
|
-
const { validateSpec } = await import("./validateSpec-DL7ObLHI.js");
|
|
1424
|
-
return reportResult(await validateSpec(structuredClone(document)));
|
|
1425
|
-
}
|
|
1426
|
-
const { compileErrors, validate } = await import("@readme/openapi-parser");
|
|
1427
|
-
const result = await validate(structuredClone(document), { validate: { errors: { colorize: true } } });
|
|
1428
|
-
if (result.valid) return;
|
|
1491
|
+
const { validateSpec } = await import("./validateSpec-DCgkUD1L.js");
|
|
1492
|
+
const result = await validateSpec(structuredClone(document));
|
|
1493
|
+
if (throwOnError) return throwResult(result);
|
|
1494
|
+
reportResult(result);
|
|
1495
|
+
}
|
|
1496
|
+
function throwResult(result) {
|
|
1497
|
+
if ("valid" in result && result.valid) return;
|
|
1429
1498
|
throw new Diagnostics.Error({
|
|
1430
1499
|
code: Diagnostics.code.invalidSpec,
|
|
1431
1500
|
severity: "error",
|
|
1432
|
-
message:
|
|
1501
|
+
message: "thrown" in result ? result.thrown : result.message,
|
|
1433
1502
|
help: "Fix each problem listed in the spec, then validate again."
|
|
1434
1503
|
});
|
|
1435
1504
|
}
|
|
@@ -1449,7 +1518,6 @@ const VALIDATION_WORKER_LIMITS = { maxYoungGenerationSizeMb: 4 };
|
|
|
1449
1518
|
* validating in place where worker threads are missing, as in edge runtimes.
|
|
1450
1519
|
*/
|
|
1451
1520
|
async function validateDocumentInWorker(document) {
|
|
1452
|
-
if (isOpenApi32(document)) return validateDocument(document);
|
|
1453
1521
|
let message;
|
|
1454
1522
|
try {
|
|
1455
1523
|
const { Worker } = await import("node:worker_threads");
|
|
@@ -1592,6 +1660,25 @@ function sortSchemas(schemas, renames) {
|
|
|
1592
1660
|
for (const name of sorted) result[name] = schemas[name];
|
|
1593
1661
|
return result;
|
|
1594
1662
|
}
|
|
1663
|
+
var SchemaNames = class {
|
|
1664
|
+
#taken;
|
|
1665
|
+
constructor(reserved) {
|
|
1666
|
+
this.#taken = new Set(reserved);
|
|
1667
|
+
}
|
|
1668
|
+
claim({ base, from }) {
|
|
1669
|
+
let n = from ?? 1;
|
|
1670
|
+
let name = from ? `${base}${n}` : base;
|
|
1671
|
+
while (this.#taken.has(pascalCase(name))) {
|
|
1672
|
+
n = Math.max(n + 1, 2);
|
|
1673
|
+
name = `${base}${n}`;
|
|
1674
|
+
}
|
|
1675
|
+
this.#taken.add(pascalCase(name));
|
|
1676
|
+
return name;
|
|
1677
|
+
}
|
|
1678
|
+
};
|
|
1679
|
+
function createSchemaNames(reserved) {
|
|
1680
|
+
return new SchemaNames(reserved);
|
|
1681
|
+
}
|
|
1595
1682
|
const semanticSuffixes = {
|
|
1596
1683
|
schemas: "Schema",
|
|
1597
1684
|
responses: "Response",
|
|
@@ -1669,15 +1756,21 @@ function getSchemas(document, { contentType }, refs) {
|
|
|
1669
1756
|
const schemas = {};
|
|
1670
1757
|
const renames = /* @__PURE__ */ new Map();
|
|
1671
1758
|
const pointers = /* @__PURE__ */ new Map();
|
|
1759
|
+
const names = createSchemaNames(normalizedNames.keys());
|
|
1672
1760
|
for (const [, items] of normalizedNames) {
|
|
1673
1761
|
const isSingle = items.length === 1;
|
|
1674
1762
|
const hasMultipleSources = !isSingle && new Set(items.map((item) => item.source)).size > 1;
|
|
1675
1763
|
items.forEach((item, index) => {
|
|
1676
|
-
const
|
|
1677
|
-
|
|
1764
|
+
const uniqueName = (() => {
|
|
1765
|
+
if (isSingle || !hasMultipleSources && !index) return item.originalName;
|
|
1766
|
+
return hasMultipleSources ? names.claim({ base: item.originalName + semanticSuffixes[item.source] }) : names.claim({
|
|
1767
|
+
base: item.originalName,
|
|
1768
|
+
from: index + 1
|
|
1769
|
+
});
|
|
1770
|
+
})();
|
|
1678
1771
|
schemas[uniqueName] = item.schema;
|
|
1679
1772
|
pointers.set(uniqueName, item.pointer ?? item.ref);
|
|
1680
|
-
if (
|
|
1773
|
+
if (uniqueName !== item.originalName || item.nested) renames.set(item.ref, uniqueName);
|
|
1681
1774
|
});
|
|
1682
1775
|
}
|
|
1683
1776
|
return {
|
|
@@ -1853,1606 +1946,1674 @@ function toInputTags(tags) {
|
|
|
1853
1946
|
};
|
|
1854
1947
|
});
|
|
1855
1948
|
}
|
|
1856
|
-
// src/
|
|
1857
|
-
const streamMediaTypes = /* @__PURE__ */ new Set([
|
|
1858
|
-
"application/jsonl",
|
|
1859
|
-
"application/x-ndjson",
|
|
1860
|
-
"application/json-seq",
|
|
1861
|
-
"text/event-stream"
|
|
1862
|
-
]);
|
|
1949
|
+
// src/emit/schemaShape.ts
|
|
1863
1950
|
/**
|
|
1864
|
-
*
|
|
1951
|
+
* Whether `format` maps to a dedicated type; `false` is what `KUBB_UNSUPPORTED_FORMAT` flags.
|
|
1865
1952
|
*/
|
|
1866
|
-
function
|
|
1867
|
-
|
|
1868
|
-
return streamMediaTypes.has(baseType) || baseType.startsWith("application/") && baseType.endsWith("+json-seq") || mediaType?.itemSchema !== void 0;
|
|
1953
|
+
function isHandledFormat(format) {
|
|
1954
|
+
return getFormatType(format) !== void 0 || specialCasedFormats.has(format);
|
|
1869
1955
|
}
|
|
1870
1956
|
/**
|
|
1871
|
-
*
|
|
1957
|
+
* Resolves the `dateType` option down to the value for one format. The scalar form of
|
|
1958
|
+
* `dateType` applies to every format; the object form picks `dateTime`/`date`/`time`
|
|
1959
|
+
* individually and defaults an omitted key to `'string'`.
|
|
1872
1960
|
*/
|
|
1873
|
-
function
|
|
1874
|
-
|
|
1961
|
+
function resolveDateTypeValue(dateType, format) {
|
|
1962
|
+
if (typeof dateType !== "object" || dateType === null) return dateType;
|
|
1963
|
+
return dateType[format === "date-time" ? "dateTime" : format] ?? "string";
|
|
1875
1964
|
}
|
|
1876
1965
|
/**
|
|
1877
|
-
*
|
|
1966
|
+
* Reads schema examples as an array. OAS 3.1 uses an `examples` array, but specs (including ones
|
|
1967
|
+
* labeled 3.1) still use the singular OAS 3.0 `example`, which the upgrader only converts on the
|
|
1968
|
+
* 3.0 -> 3.1 hop. Normalize both into one array so the AST node exposes only `examples`.
|
|
1878
1969
|
*/
|
|
1879
|
-
function
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
if (!resolved?.contentSchema || !resolved.contentMediaType || !isJsonMimeType(resolved.contentMediaType)) return schema;
|
|
1883
|
-
return {
|
|
1884
|
-
...schema,
|
|
1885
|
-
properties: {
|
|
1886
|
-
...schema.properties,
|
|
1887
|
-
data: resolved.contentSchema
|
|
1888
|
-
}
|
|
1889
|
-
};
|
|
1970
|
+
function extractExamples(schema) {
|
|
1971
|
+
if (Array.isArray(schema.examples)) return schema.examples;
|
|
1972
|
+
return schema.example !== void 0 ? [schema.example] : void 0;
|
|
1890
1973
|
}
|
|
1891
1974
|
/**
|
|
1892
|
-
*
|
|
1975
|
+
* Whether the schema and its inline `allOf` members hold more than one distinct `pattern`. A value must match every one.
|
|
1893
1976
|
*/
|
|
1894
|
-
function
|
|
1895
|
-
const
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1977
|
+
function hasSeveralPatterns(schema) {
|
|
1978
|
+
const patterns = [schema, ...schema.allOf ?? []].map((item) => isReference(item) ? void 0 : item.pattern).filter((pattern) => pattern !== void 0);
|
|
1979
|
+
return new Set(patterns).size > 1;
|
|
1980
|
+
}
|
|
1981
|
+
function hasStructuralKeywords(fragment) {
|
|
1982
|
+
return Object.keys(fragment).some((key) => structuralKeys.has(key));
|
|
1899
1983
|
}
|
|
1900
1984
|
/**
|
|
1901
|
-
*
|
|
1985
|
+
* Flattens a keyword-only `allOf` into its parent schema.
|
|
1986
|
+
*
|
|
1987
|
+
* Only flattens when every member is a plain fragment, with no `$ref` and no structural keywords
|
|
1988
|
+
* (see `structuralKeys`). Outer schema values take precedence over fragment values. Returns the
|
|
1989
|
+
* original schema unchanged when flattening is unsafe.
|
|
1990
|
+
*
|
|
1991
|
+
* @example
|
|
1992
|
+
* ```ts
|
|
1993
|
+
* flattenSchema({ allOf: [{ description: 'A pet' }], type: 'object', properties: {} })
|
|
1994
|
+
* // { type: 'object', properties: {}, description: 'A pet' }
|
|
1995
|
+
* flattenSchema({ allOf: [{ $ref: '#/components/schemas/Pet' }] })
|
|
1996
|
+
* // returned unchanged, contains a $ref
|
|
1997
|
+
* ```
|
|
1902
1998
|
*/
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1999
|
+
function flattenSchema(schema) {
|
|
2000
|
+
if (!schema.allOf?.length) return schema;
|
|
2001
|
+
const allOfFragments = schema.allOf;
|
|
2002
|
+
if (allOfFragments.some((item) => isReference(item))) return schema;
|
|
2003
|
+
if (allOfFragments.some(hasStructuralKeywords)) return schema;
|
|
2004
|
+
if (hasSeveralPatterns(schema)) return schema;
|
|
2005
|
+
const { allOf: _allOf, ...rest } = schema;
|
|
2006
|
+
const merged = rest;
|
|
2007
|
+
for (const fragment of allOfFragments) for (const [key, value] of Object.entries(fragment)) merged[key] ??= value;
|
|
2008
|
+
return merged;
|
|
2009
|
+
}
|
|
2010
|
+
// src/emit/createNode.ts
|
|
1909
2011
|
/**
|
|
1910
|
-
*
|
|
2012
|
+
* Builds a schema node from a converter's base context plus its type-specific fields. Every
|
|
2013
|
+
* converter needs the same metadata fields (`title`, `description`, `examples`, ...) alongside
|
|
2014
|
+
* whatever makes its node distinct; this folds both into one call.
|
|
1911
2015
|
*/
|
|
1912
|
-
function
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
2016
|
+
function createNode({ schema, name, nullable, defaultValue }, extras) {
|
|
2017
|
+
return ast.factory.createSchema({
|
|
2018
|
+
name,
|
|
2019
|
+
nullable,
|
|
2020
|
+
title: schema.title,
|
|
2021
|
+
description: schema.description,
|
|
2022
|
+
deprecated: schema.deprecated,
|
|
2023
|
+
readOnly: schema.readOnly,
|
|
2024
|
+
writeOnly: schema.writeOnly,
|
|
2025
|
+
default: defaultValue,
|
|
2026
|
+
examples: extractExamples(schema),
|
|
2027
|
+
format: schema.format,
|
|
2028
|
+
externalDocs: getExternalDocs(schema.externalDocs),
|
|
2029
|
+
extensions: pickExtensions(schema),
|
|
2030
|
+
...extras
|
|
2031
|
+
});
|
|
1924
2032
|
}
|
|
2033
|
+
// src/emit/discriminator/preserve.ts
|
|
1925
2034
|
/**
|
|
1926
|
-
*
|
|
1927
|
-
* - Keywords next to a `$ref` apply to the referenced schema.
|
|
1928
|
-
* - Shared `properties` merge into each inline `oneOf`/`anyOf` member.
|
|
1929
|
-
* - A JSON `data` string becomes its `contentSchema`.
|
|
2035
|
+
* Turns string mapping keys into the discriminator property's type when they parse cleanly.
|
|
1930
2036
|
*/
|
|
1931
|
-
function
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
properties,
|
|
1944
|
-
required
|
|
1945
|
-
};
|
|
1946
|
-
return {
|
|
1947
|
-
...merge ? rest : schema,
|
|
1948
|
-
[key]: members.map((member) => isReference(member) ? withParsedRef(member, refs) : withParsedData(merge ? mergeSchemas(shared, member) : member, refs))
|
|
1949
|
-
};
|
|
2037
|
+
function typedKeys({ keys, propertyType }) {
|
|
2038
|
+
return keys.map((key) => {
|
|
2039
|
+
if (propertyType === "integer" || propertyType === "number") {
|
|
2040
|
+
const value = Number(key);
|
|
2041
|
+
return key.trim() !== "" && Number.isFinite(value) ? value : key;
|
|
2042
|
+
}
|
|
2043
|
+
if (propertyType === "boolean") {
|
|
2044
|
+
const lower = key.toLowerCase();
|
|
2045
|
+
return lower === "true" ? true : lower === "false" ? false : key;
|
|
2046
|
+
}
|
|
2047
|
+
return key;
|
|
2048
|
+
});
|
|
1950
2049
|
}
|
|
1951
|
-
const SSE_FIELDS = /* @__PURE__ */ new Set([
|
|
1952
|
-
"event",
|
|
1953
|
-
"id",
|
|
1954
|
-
"retry",
|
|
1955
|
-
"data"
|
|
1956
|
-
]);
|
|
1957
2050
|
/**
|
|
1958
|
-
*
|
|
2051
|
+
* Reads the declared `type` of `propertyName` on a schema, following `$ref`s and `allOf` members.
|
|
2052
|
+
* A nullable array such as `['integer', 'null']` reads as its one non-null type.
|
|
1959
2053
|
*/
|
|
1960
|
-
function
|
|
1961
|
-
|
|
1962
|
-
|
|
2054
|
+
function propertyTypeOf({ schema, propertyName, refs, seen = /* @__PURE__ */ new Set() }) {
|
|
2055
|
+
const deref = (value) => value && isReference(value) ? refs.resolve(value.$ref, { report: false }) : value;
|
|
2056
|
+
if (schema && isReference(schema)) {
|
|
2057
|
+
if (seen.has(schema.$ref)) return void 0;
|
|
1963
2058
|
seen.add(schema.$ref);
|
|
1964
|
-
const resolved = refs.resolve(schema.$ref, { report: false });
|
|
1965
|
-
return resolved ? collectFields(resolved, refs, seen) : [];
|
|
1966
2059
|
}
|
|
1967
|
-
const
|
|
1968
|
-
|
|
2060
|
+
const resolved = deref(schema);
|
|
2061
|
+
const type = deref(resolved?.properties?.[propertyName])?.type ?? resolved?.allOf?.map((member) => propertyTypeOf({
|
|
2062
|
+
schema: member,
|
|
2063
|
+
propertyName,
|
|
2064
|
+
refs,
|
|
2065
|
+
seen
|
|
2066
|
+
})).find(Boolean);
|
|
2067
|
+
const nonNull = Array.isArray(type) ? type.filter((t) => t !== "null") : [];
|
|
2068
|
+
return nonNull.length === 1 ? nonNull[0] : type;
|
|
1969
2069
|
}
|
|
1970
2070
|
/**
|
|
1971
|
-
*
|
|
1972
|
-
*
|
|
1973
|
-
*
|
|
2071
|
+
* Creates a single-property object schema used as a discriminator literal.
|
|
2072
|
+
*
|
|
2073
|
+
* @example
|
|
2074
|
+
* ```ts
|
|
2075
|
+
* createDiscriminantNode({ propertyName: 'type', values: ['dog'] })
|
|
2076
|
+
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
2077
|
+
* ```
|
|
1974
2078
|
*/
|
|
1975
|
-
function
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
2079
|
+
function createDiscriminantNode({ propertyName, values }) {
|
|
2080
|
+
return ast.factory.createSchema({
|
|
2081
|
+
type: "object",
|
|
2082
|
+
primitive: "object",
|
|
2083
|
+
properties: [ast.factory.createProperty({
|
|
2084
|
+
name: propertyName,
|
|
2085
|
+
schema: ast.factory.createSchema({
|
|
2086
|
+
type: "enum",
|
|
2087
|
+
primitive: enumPrimitive(values),
|
|
2088
|
+
enumValues: values
|
|
2089
|
+
}),
|
|
2090
|
+
required: true
|
|
2091
|
+
})]
|
|
2092
|
+
});
|
|
1986
2093
|
}
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
*/
|
|
1990
|
-
function getItemShape(schema, refs) {
|
|
1991
|
-
return declaresEventFields(schema, refs, /* @__PURE__ */ new Set()) ? "event" : "data";
|
|
2094
|
+
function toMappingRef(value) {
|
|
2095
|
+
return value.includes("/") ? value : `${SCHEMA_REF_PREFIX}${value}`;
|
|
1992
2096
|
}
|
|
1993
2097
|
/**
|
|
1994
|
-
* Returns the
|
|
2098
|
+
* Returns the `$ref` of an OpenAPI 3.2 `defaultMapping`, written as a schema name or a `$ref`; `undefined` when none.
|
|
1995
2099
|
*/
|
|
1996
|
-
function
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
return isEventStream(contentType) ? toEventSchema(raw, refs) : raw;
|
|
2100
|
+
function getDefaultMappingRef(discriminator) {
|
|
2101
|
+
const value = discriminator?.defaultMapping;
|
|
2102
|
+
if (!value) return void 0;
|
|
2103
|
+
return toMappingRef(value);
|
|
2001
2104
|
}
|
|
2002
|
-
// src/emit/schemaShape.ts
|
|
2003
2105
|
/**
|
|
2004
|
-
*
|
|
2106
|
+
* Returns every discriminator key whose mapping value matches `ref`, in mapping order.
|
|
2107
|
+
* A bare schema name in the mapping matches its `#/components/schemas/<name>` ref.
|
|
2108
|
+
*
|
|
2109
|
+
* A mapping may point several keys at the same schema, in which case the member has to be
|
|
2110
|
+
* narrowed to all of them — keeping only the first would drop the rest from the union.
|
|
2111
|
+
*
|
|
2112
|
+
* @example
|
|
2113
|
+
* ```ts
|
|
2114
|
+
* findDiscriminators({ mapping: { dog: '#/components/schemas/Dog', hound: '#/components/schemas/Dog' }, ref: '#/components/schemas/Dog' }) // ['dog', 'hound']
|
|
2115
|
+
* ```
|
|
2005
2116
|
*/
|
|
2006
|
-
function
|
|
2007
|
-
|
|
2117
|
+
function findDiscriminators({ mapping, ref }) {
|
|
2118
|
+
if (!mapping || !ref) return [];
|
|
2119
|
+
return Object.entries(mapping).filter(([, value]) => toMappingRef(value) === ref).map(([key]) => key);
|
|
2008
2120
|
}
|
|
2009
2121
|
/**
|
|
2010
|
-
*
|
|
2011
|
-
* `dateType` applies to every format; the object form picks `dateTime`/`date`/`time`
|
|
2012
|
-
* individually and defaults an omitted key to `'string'`.
|
|
2122
|
+
* Finds the discriminator a schema declares itself or through an inline `allOf` member.
|
|
2013
2123
|
*/
|
|
2014
|
-
function
|
|
2015
|
-
|
|
2016
|
-
return dateType[format === "date-time" ? "dateTime" : format] ?? "string";
|
|
2124
|
+
function getDiscriminator(schema) {
|
|
2125
|
+
return (isDiscriminator(schema) ? schema : schema.allOf?.find(isDiscriminator))?.discriminator;
|
|
2017
2126
|
}
|
|
2018
2127
|
/**
|
|
2019
|
-
*
|
|
2020
|
-
*
|
|
2021
|
-
* 3.0 -> 3.1 hop. Normalize both into one array so the AST node exposes only `examples`.
|
|
2128
|
+
* Lists the mapping keys of a discriminator plus the keys of the nested discriminators its mapped
|
|
2129
|
+
* schemas declare on the same property, so a root `$type` accepts values from every level.
|
|
2022
2130
|
*/
|
|
2023
|
-
function
|
|
2024
|
-
|
|
2025
|
-
|
|
2131
|
+
function getMappingKeys({ discriminator: { propertyName, mapping }, refs, seen = /* @__PURE__ */ new Set() }) {
|
|
2132
|
+
return Object.entries(mapping ?? {}).flatMap(([key, value]) => {
|
|
2133
|
+
const target = toMappingRef(value);
|
|
2134
|
+
if (seen.has(target)) return [key];
|
|
2135
|
+
seen.add(target);
|
|
2136
|
+
const resolved = refs.resolve(target, { report: false });
|
|
2137
|
+
const nested = resolved && getDiscriminator(resolved);
|
|
2138
|
+
return nested?.propertyName === propertyName ? [key, ...getMappingKeys({
|
|
2139
|
+
discriminator: nested,
|
|
2140
|
+
refs,
|
|
2141
|
+
seen
|
|
2142
|
+
})] : [key];
|
|
2143
|
+
});
|
|
2026
2144
|
}
|
|
2027
2145
|
/**
|
|
2028
|
-
*
|
|
2146
|
+
* Drops the discriminator from an inline `allOf` member when a `$ref` sibling already declares one on
|
|
2147
|
+
* the same property. That mapping names the schema's own children, and keeping it pins the property
|
|
2148
|
+
* to those keys next to the parent's, which leaves `never`.
|
|
2029
2149
|
*/
|
|
2030
|
-
function
|
|
2031
|
-
const
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
return
|
|
2150
|
+
function removeInheritedDiscriminator({ members, refs }) {
|
|
2151
|
+
const inherited = new Set(members.flatMap((member) => {
|
|
2152
|
+
const parent = isReference(member) ? refs.resolve(member.$ref, { report: false }) : null;
|
|
2153
|
+
return parent && getDiscriminator(parent)?.propertyName || [];
|
|
2154
|
+
}));
|
|
2155
|
+
return members.map((member) => {
|
|
2156
|
+
if (isReference(member) || !isDiscriminator(member) || member.oneOf || member.anyOf) return member;
|
|
2157
|
+
if (!inherited.has(member.discriminator.propertyName)) return member;
|
|
2158
|
+
const { discriminator: _discriminator, ...rest } = member;
|
|
2159
|
+
return rest;
|
|
2160
|
+
});
|
|
2036
2161
|
}
|
|
2037
2162
|
/**
|
|
2038
|
-
*
|
|
2039
|
-
*
|
|
2040
|
-
* Only flattens when every member is a plain fragment, with no `$ref` and no structural keywords
|
|
2041
|
-
* (see `structuralKeys`). Outer schema values take precedence over fragment values. Returns the
|
|
2042
|
-
* original schema unchanged when flattening is unsafe.
|
|
2043
|
-
*
|
|
2044
|
-
* @example
|
|
2045
|
-
* ```ts
|
|
2046
|
-
* flattenSchema({ allOf: [{ description: 'A pet' }], type: 'object', properties: {} })
|
|
2047
|
-
* // { type: 'object', properties: {}, description: 'A pet' }
|
|
2048
|
-
* flattenSchema({ allOf: [{ $ref: '#/components/schemas/Pet' }] })
|
|
2049
|
-
* // returned unchanged, contains a $ref
|
|
2050
|
-
* ```
|
|
2163
|
+
* Whether a member can carry the discriminator property. A discriminator is only a hint, so an array
|
|
2164
|
+
* or primitive member keeps its own type instead of an intersection no value can satisfy.
|
|
2051
2165
|
*/
|
|
2052
|
-
function
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
if (allOfFragments.some(hasStructuralKeywords)) return schema;
|
|
2057
|
-
if (hasSeveralPatterns(schema)) return schema;
|
|
2058
|
-
const { allOf: _allOf, ...rest } = schema;
|
|
2059
|
-
const merged = rest;
|
|
2060
|
-
for (const fragment of allOfFragments) for (const [key, value] of Object.entries(fragment)) merged[key] ??= value;
|
|
2061
|
-
return merged;
|
|
2166
|
+
function isObjectLike(member, refs) {
|
|
2167
|
+
const type = (isReference(member) ? refs.resolve(member.$ref, { report: false }) : member)?.type;
|
|
2168
|
+
if (type === void 0) return true;
|
|
2169
|
+
return Array.isArray(type) ? type.includes("object") : type === "object";
|
|
2062
2170
|
}
|
|
2063
|
-
// src/schemaDiagnostics.ts
|
|
2064
2171
|
/**
|
|
2065
|
-
*
|
|
2066
|
-
*
|
|
2067
|
-
*
|
|
2068
|
-
*
|
|
2069
|
-
*
|
|
2172
|
+
* Narrows each `oneOf`/`anyOf` member with its discriminant value, intersecting the member's own
|
|
2173
|
+
* node with either the shared-properties slice carrying that value, or a synthetic discriminant
|
|
2174
|
+
* literal. The referenced child schema's own definition is left untouched — narrowing happens only
|
|
2175
|
+
* at this union usage site, which is what makes this mode "preserve" (as opposed to `propagate`,
|
|
2176
|
+
* which additionally patches the child schema's own definition in a post-pass).
|
|
2070
2177
|
*/
|
|
2071
|
-
function
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
} finally {
|
|
2081
|
-
const tally = this.#pointers;
|
|
2082
|
-
this.#pointers = void 0;
|
|
2083
|
-
for (const [format, pointers] of tally) reportUnsupportedFormat(format, [...pointers]);
|
|
2084
|
-
}
|
|
2085
|
-
}
|
|
2086
|
-
add(format, pointer) {
|
|
2087
|
-
if (!this.#pointers) return reportUnsupportedFormat(format, [pointer]);
|
|
2088
|
-
memo(this.#pointers, format, () => /* @__PURE__ */ new Set()).add(pointer);
|
|
2178
|
+
function narrowUnionMembers({ unionMembers, discriminator, sharedPropertiesNode, parentType, parse, name, refs }) {
|
|
2179
|
+
function pickDiscriminatorPropertyNode(node, propertyName) {
|
|
2180
|
+
const discriminatorProperty = ast.schema.narrow(node, "object")?.properties?.find((property) => property.name === propertyName);
|
|
2181
|
+
if (!discriminatorProperty) return null;
|
|
2182
|
+
return ast.factory.createSchema({
|
|
2183
|
+
type: "object",
|
|
2184
|
+
primitive: "object",
|
|
2185
|
+
properties: [discriminatorProperty]
|
|
2186
|
+
});
|
|
2089
2187
|
}
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
node.pointer ??= pointer;
|
|
2115
|
-
if (node.deprecated) Diagnostics.report({
|
|
2116
|
-
code: Diagnostics.code.deprecated,
|
|
2117
|
-
severity: "info",
|
|
2118
|
-
message: "This schema is marked as deprecated.",
|
|
2119
|
-
location: {
|
|
2120
|
-
kind: "schema",
|
|
2121
|
-
pointer
|
|
2188
|
+
function implicitDiscriminantValue(member) {
|
|
2189
|
+
if (!discriminator || discriminator.mapping || !isReference(member)) return null;
|
|
2190
|
+
const value = ast.ref.getName(member.$ref);
|
|
2191
|
+
if (!value) return null;
|
|
2192
|
+
const variant = refs.resolve(member.$ref, { report: false });
|
|
2193
|
+
if (!variant) return null;
|
|
2194
|
+
const propertyName = discriminator.propertyName;
|
|
2195
|
+
const seen = /* @__PURE__ */ new Set([member.$ref]);
|
|
2196
|
+
/** Whether the variant, or a schema it composes, pins the discriminator property to literal values. */
|
|
2197
|
+
function constrains(v) {
|
|
2198
|
+
const prop = v.properties?.[propertyName];
|
|
2199
|
+
const resolved = prop && isReference(prop) ? refs.resolve(prop.$ref, { report: false }) : prop;
|
|
2200
|
+
const pins = (candidate) => Array.isArray(candidate.enum) || candidate.const !== void 0;
|
|
2201
|
+
const members = (resolved?.anyOf ?? resolved?.oneOf ?? []).filter((member) => member.type !== "null");
|
|
2202
|
+
if (resolved && (pins(resolved) || members.length > 0 && members.every(pins))) return true;
|
|
2203
|
+
const composition = v.allOf ?? v.oneOf ?? v.anyOf;
|
|
2204
|
+
if (!composition) return false;
|
|
2205
|
+
return composition.some((m) => {
|
|
2206
|
+
if (!isReference(m)) return constrains(m);
|
|
2207
|
+
if (seen.has(m.$ref)) return false;
|
|
2208
|
+
seen.add(m.$ref);
|
|
2209
|
+
const r = refs.resolve(m.$ref, { report: false });
|
|
2210
|
+
return r ? constrains(r) : false;
|
|
2211
|
+
});
|
|
2122
2212
|
}
|
|
2123
|
-
|
|
2124
|
-
if (typeof node.format === "string" && !isHandledFormat(node.format)) formatTally.add(node.format, pointer);
|
|
2125
|
-
if (node.type === "object") {
|
|
2126
|
-
for (const property of node.properties) visit(property.schema, `${pointer}/properties/${escapePointerToken(property.name)}`);
|
|
2127
|
-
if (node.additionalProperties && typeof node.additionalProperties === "object") visit(node.additionalProperties, `${pointer}/additionalProperties`);
|
|
2128
|
-
return;
|
|
2129
|
-
}
|
|
2130
|
-
if (node.type === "array") {
|
|
2131
|
-
for (const item of node.items ?? []) visit(item, `${pointer}/items`);
|
|
2132
|
-
return;
|
|
2133
|
-
}
|
|
2134
|
-
if (node.type === "tuple") {
|
|
2135
|
-
for (const [index, item] of (node.items ?? []).entries()) visit(item, `${pointer}/items/${index}`);
|
|
2136
|
-
return;
|
|
2137
|
-
}
|
|
2138
|
-
if (node.type === "not") {
|
|
2139
|
-
for (const member of node.members ?? []) visit(member, `${pointer}/not`);
|
|
2140
|
-
return;
|
|
2141
|
-
}
|
|
2142
|
-
if (node.type === "union" && node.discriminatorPropertyName) reportDuplicateDiscriminators(node, node.discriminatorPropertyName, pointer);
|
|
2143
|
-
if (node.type === "union" || node.type === "intersection") {
|
|
2144
|
-
const keyword = node.type === "intersection" ? "allOf" : node.strategy === "any" ? "anyOf" : "oneOf";
|
|
2145
|
-
for (const [index, member] of (node.members ?? []).entries()) visit(member, `${pointer}/${keyword}/${index}`);
|
|
2213
|
+
return constrains(variant) ? null : value;
|
|
2146
2214
|
}
|
|
2215
|
+
const defaultRef = getDefaultMappingRef(discriminator);
|
|
2216
|
+
return unionMembers.map((s) => {
|
|
2217
|
+
const ref = isReference(s) ? s.$ref : void 0;
|
|
2218
|
+
const isDefault = ref !== void 0 && ref === defaultRef;
|
|
2219
|
+
const mappedValues = isDefault ? [] : findDiscriminators({
|
|
2220
|
+
mapping: discriminator?.mapping,
|
|
2221
|
+
ref
|
|
2222
|
+
});
|
|
2223
|
+
const implicitValue = mappedValues.length || isDefault ? null : implicitDiscriminantValue(s);
|
|
2224
|
+
const propertyType = propertyTypeOf({
|
|
2225
|
+
schema: s,
|
|
2226
|
+
propertyName: discriminator?.propertyName ?? "",
|
|
2227
|
+
refs
|
|
2228
|
+
}) ?? parentType;
|
|
2229
|
+
const discriminatorValues = typedKeys({
|
|
2230
|
+
keys: mappedValues.length ? mappedValues : implicitValue ? [implicitValue] : [],
|
|
2231
|
+
propertyType
|
|
2232
|
+
});
|
|
2233
|
+
const memberNode = parse({
|
|
2234
|
+
schema: s,
|
|
2235
|
+
name
|
|
2236
|
+
});
|
|
2237
|
+
if (!discriminatorValues.length || !discriminator || !isObjectLike(s, refs)) return memberNode;
|
|
2238
|
+
const narrowedDiscriminatorNode = sharedPropertiesNode ? pickDiscriminatorPropertyNode(ast.macro.apply(sharedPropertiesNode, [macroDiscriminatorEnum({
|
|
2239
|
+
propertyName: discriminator.propertyName,
|
|
2240
|
+
values: discriminatorValues
|
|
2241
|
+
})], { depth: "shallow" }), discriminator.propertyName) : void 0;
|
|
2242
|
+
return ast.factory.createSchema({
|
|
2243
|
+
type: "intersection",
|
|
2244
|
+
members: [memberNode, narrowedDiscriminatorNode ?? createDiscriminantNode({
|
|
2245
|
+
propertyName: discriminator.propertyName,
|
|
2246
|
+
values: discriminatorValues
|
|
2247
|
+
})]
|
|
2248
|
+
});
|
|
2249
|
+
});
|
|
2147
2250
|
}
|
|
2148
2251
|
/**
|
|
2149
|
-
*
|
|
2150
|
-
*
|
|
2151
|
-
*
|
|
2252
|
+
* Filters the discriminated members out of an `allOf` list: an `allOf` member that `$ref`s a
|
|
2253
|
+
* discriminated union's parent, where this schema is itself one of that union's children, is
|
|
2254
|
+
* dropped from `members` and its discriminant value collected instead — the same synthetic
|
|
2255
|
+
* literal `narrowUnionMembers` produces, so the emitted node stays a plain intersection rather
|
|
2256
|
+
* than nesting the whole parent union one level deeper.
|
|
2152
2257
|
*/
|
|
2153
|
-
function
|
|
2154
|
-
const
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2258
|
+
function extractDiscriminatedAllOfMembers({ allOfMembers, name, refs }) {
|
|
2259
|
+
const discriminantValues = [];
|
|
2260
|
+
return {
|
|
2261
|
+
members: allOfMembers.filter((item) => {
|
|
2262
|
+
if (!isReference(item) || !name) return true;
|
|
2263
|
+
const deref = refs.resolve(item.$ref);
|
|
2264
|
+
const discriminator = deref && getDiscriminator(deref);
|
|
2265
|
+
const parentUnion = deref?.oneOf ?? deref?.anyOf;
|
|
2266
|
+
if (!deref || !discriminator || isDiscriminator(deref) && !parentUnion) return true;
|
|
2267
|
+
const childRef = `${SCHEMA_REF_PREFIX}${name}`;
|
|
2268
|
+
const isDefault = childRef === getDefaultMappingRef(discriminator);
|
|
2269
|
+
const values = isDefault ? [] : findDiscriminators({
|
|
2270
|
+
mapping: discriminator.mapping,
|
|
2271
|
+
ref: childRef
|
|
2272
|
+
});
|
|
2273
|
+
if (values.length) {
|
|
2274
|
+
const { propertyName } = discriminator;
|
|
2275
|
+
const propertyType = propertyTypeOf({
|
|
2276
|
+
schema: deref,
|
|
2277
|
+
propertyName,
|
|
2278
|
+
refs
|
|
2279
|
+
}) ?? propertyTypeOf({
|
|
2280
|
+
schema: { $ref: childRef },
|
|
2281
|
+
propertyName,
|
|
2282
|
+
refs
|
|
2283
|
+
});
|
|
2284
|
+
discriminantValues.push({
|
|
2285
|
+
propertyName,
|
|
2286
|
+
values: typedKeys({
|
|
2287
|
+
keys: values,
|
|
2288
|
+
propertyType
|
|
2289
|
+
})
|
|
2290
|
+
});
|
|
2291
|
+
}
|
|
2292
|
+
return !(parentUnion && (isDefault || values.length > 0 || parentUnion.some((member) => isReference(member) && member.$ref === childRef)));
|
|
2293
|
+
}),
|
|
2294
|
+
discriminantValues
|
|
2295
|
+
};
|
|
2186
2296
|
}
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2297
|
+
// src/emit/converters/annotatedEnum.ts
|
|
2298
|
+
const ANNOTATIONS = /* @__PURE__ */ new Set([
|
|
2299
|
+
"title",
|
|
2300
|
+
"description",
|
|
2301
|
+
"default",
|
|
2302
|
+
"deprecated",
|
|
2303
|
+
"readOnly",
|
|
2304
|
+
"writeOnly",
|
|
2305
|
+
"example",
|
|
2306
|
+
"examples",
|
|
2307
|
+
"xml",
|
|
2308
|
+
"externalDocs"
|
|
2309
|
+
]);
|
|
2310
|
+
const UNION_KEYS = /* @__PURE__ */ new Set([
|
|
2311
|
+
...ANNOTATIONS,
|
|
2312
|
+
"type",
|
|
2313
|
+
"nullable",
|
|
2314
|
+
"oneOf",
|
|
2315
|
+
"anyOf"
|
|
2316
|
+
]);
|
|
2317
|
+
const MEMBER_KEYS = /* @__PURE__ */ new Set([
|
|
2318
|
+
...ANNOTATIONS,
|
|
2319
|
+
"type",
|
|
2320
|
+
"const"
|
|
2321
|
+
]);
|
|
2322
|
+
function hasOnlyKeys(schema, keys) {
|
|
2323
|
+
return typeof schema === "object" && schema !== null && !Array.isArray(schema) && Object.keys(schema).every((key) => keys.has(key) || key.startsWith("x-"));
|
|
2190
2324
|
}
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
return value.replace(/[^a-zA-Z0-9]/g, "-").replace(/-{2,}/g, "-").replace(/^-|-$/g, "");
|
|
2325
|
+
function matchesType(type, value) {
|
|
2326
|
+
return type === void 0 || type === typeof value || type === "integer" && typeof value === "number" && Number.isInteger(value);
|
|
2194
2327
|
}
|
|
2195
|
-
/**
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2328
|
+
/** Recognizes annotated const unions only when replacing them cannot discard validation constraints. */
|
|
2329
|
+
function convertAnnotatedEnum(context) {
|
|
2330
|
+
const { schema, document } = context;
|
|
2331
|
+
if (!document.openapi?.startsWith("3.1.")) return void 0;
|
|
2332
|
+
if (!hasOnlyKeys(schema, UNION_KEYS) || schema.oneOf !== void 0 && schema.anyOf !== void 0) return void 0;
|
|
2333
|
+
const members = schema.oneOf ?? schema.anyOf;
|
|
2334
|
+
if (!members?.length) return void 0;
|
|
2335
|
+
const names = /* @__PURE__ */ new Set();
|
|
2336
|
+
const values = /* @__PURE__ */ new Set();
|
|
2337
|
+
const namedEnumValues = [];
|
|
2338
|
+
let primitive;
|
|
2339
|
+
for (const member of members) {
|
|
2340
|
+
if (!hasOnlyKeys(member, MEMBER_KEYS)) return void 0;
|
|
2341
|
+
const value = member.const;
|
|
2342
|
+
if (typeof value !== "string" && (typeof value !== "number" || !Number.isFinite(value))) return void 0;
|
|
2343
|
+
if (typeof member.title !== "string" || !member.title.trim() || names.has(member.title) || values.has(value)) return void 0;
|
|
2344
|
+
if (!matchesType(schema.type, value) || !matchesType(member.type, value)) return void 0;
|
|
2345
|
+
const valueType = typeof value === "number" ? "number" : "string";
|
|
2346
|
+
if (primitive !== void 0 && primitive !== valueType) return void 0;
|
|
2347
|
+
primitive = valueType;
|
|
2348
|
+
names.add(member.title);
|
|
2349
|
+
values.add(value);
|
|
2350
|
+
namedEnumValues.push({
|
|
2351
|
+
name: member.title,
|
|
2352
|
+
value,
|
|
2353
|
+
primitive: valueType,
|
|
2354
|
+
description: member.description
|
|
2355
|
+
});
|
|
2356
|
+
}
|
|
2357
|
+
return createNode(context, {
|
|
2358
|
+
type: "enum",
|
|
2359
|
+
primitive,
|
|
2360
|
+
namedEnumValues
|
|
2361
|
+
});
|
|
2201
2362
|
}
|
|
2363
|
+
// src/emit/converters/composition.ts
|
|
2202
2364
|
/**
|
|
2203
|
-
*
|
|
2365
|
+
* Collects property schemas from all `allOf` members and outer schema properties,
|
|
2366
|
+
* recursively resolving `$ref` pointers.
|
|
2204
2367
|
*/
|
|
2205
|
-
function
|
|
2206
|
-
const
|
|
2207
|
-
|
|
2208
|
-
|
|
2368
|
+
function collectSiblingProperties(allOfMembers, refs, outerProperties) {
|
|
2369
|
+
const properties = Object.assign(Object.create(null), outerProperties);
|
|
2370
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2371
|
+
/** Adds the properties of one member, following its `$ref` and its own `allOf`. */
|
|
2372
|
+
function collectFrom(item) {
|
|
2373
|
+
if (!item) return;
|
|
2374
|
+
if (isReference(item)) {
|
|
2375
|
+
const { $ref, ...siblings } = item;
|
|
2376
|
+
if (!visited.has($ref)) {
|
|
2377
|
+
visited.add($ref);
|
|
2378
|
+
collectFrom(refs.resolve($ref, { report: false }) ?? void 0);
|
|
2379
|
+
}
|
|
2380
|
+
collectFrom(siblings);
|
|
2381
|
+
return;
|
|
2382
|
+
}
|
|
2383
|
+
if (item.properties) Object.assign(properties, item.properties);
|
|
2384
|
+
if (Array.isArray(item.allOf)) for (const member of item.allOf) collectFrom(member);
|
|
2385
|
+
}
|
|
2386
|
+
for (const item of allOfMembers) collectFrom(item);
|
|
2387
|
+
return properties;
|
|
2209
2388
|
}
|
|
2210
2389
|
/**
|
|
2211
|
-
*
|
|
2390
|
+
* Enriches union members that declare `required` properties without declaring corresponding
|
|
2391
|
+
* property schemas, populating the missing property definitions from available sibling schemas
|
|
2392
|
+
* (or an empty schema falling back to the configured emptySchemaType).
|
|
2212
2393
|
*/
|
|
2213
|
-
function
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2394
|
+
function resolveUnionMembers(members, siblingProperties) {
|
|
2395
|
+
return members.map((member) => {
|
|
2396
|
+
if (!member || typeof member !== "object" || isReference(member)) return member;
|
|
2397
|
+
const obj = member;
|
|
2398
|
+
if (!Array.isArray(obj.required) || obj.required.length === 0) return member;
|
|
2399
|
+
const existingProps = obj.properties ?? {};
|
|
2400
|
+
const missingKeys = obj.required.filter((key) => !Object.hasOwn(existingProps, key));
|
|
2401
|
+
if (missingKeys.length === 0) return member;
|
|
2402
|
+
const newProperties = { ...existingProps };
|
|
2403
|
+
for (const key of missingKeys) {
|
|
2404
|
+
const value = Object.hasOwn(siblingProperties, key) ? siblingProperties[key] : {};
|
|
2405
|
+
Object.defineProperty(newProperties, key, {
|
|
2406
|
+
value,
|
|
2407
|
+
enumerable: true,
|
|
2408
|
+
writable: true,
|
|
2409
|
+
configurable: true
|
|
2410
|
+
});
|
|
2411
|
+
}
|
|
2412
|
+
return {
|
|
2413
|
+
type: obj.type ?? "object",
|
|
2414
|
+
...obj,
|
|
2415
|
+
properties: newProperties
|
|
2416
|
+
};
|
|
2417
|
+
});
|
|
2217
2418
|
}
|
|
2218
2419
|
/**
|
|
2219
|
-
*
|
|
2420
|
+
* Enriches a schema object, and any `oneOf` or `anyOf` branches on it, with properties from sibling schemas.
|
|
2220
2421
|
*/
|
|
2221
|
-
function
|
|
2222
|
-
const
|
|
2223
|
-
if (
|
|
2224
|
-
|
|
2422
|
+
function resolveUnionSchema(schema, siblingProperties) {
|
|
2423
|
+
const next = { ...resolveUnionMembers([schema], siblingProperties)[0] ?? schema };
|
|
2424
|
+
if (schema.oneOf) next.oneOf = resolveUnionMembers(schema.oneOf, siblingProperties);
|
|
2425
|
+
if (schema.anyOf) next.anyOf = resolveUnionMembers(schema.anyOf, siblingProperties);
|
|
2426
|
+
return next;
|
|
2225
2427
|
}
|
|
2226
2428
|
/**
|
|
2227
|
-
*
|
|
2228
|
-
*
|
|
2429
|
+
* Converts a `$ref` schema into a `RefSchemaNode`.
|
|
2430
|
+
*
|
|
2431
|
+
* The resolved schema is stored in `node.schema`. Usage-site sibling fields
|
|
2432
|
+
* (description, readOnly, nullable, etc.) are stored directly on the ref node.
|
|
2433
|
+
* Use `ast.ref.mergeSchema(node)` in printers to get a merged view of both.
|
|
2434
|
+
* Circular refs are detected in `refs.resolveNode` and leave `schema` as `null`.
|
|
2229
2435
|
*/
|
|
2230
|
-
function
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
param,
|
|
2239
|
-
pointer: `${pointer}/parameters/${index}`
|
|
2240
|
-
}] : [];
|
|
2241
|
-
});
|
|
2242
|
-
const pathPointer = `#/paths/${escapePointerToken(operation.path)}`;
|
|
2243
|
-
const operationParams = resolveParams(operation.schema?.parameters || [], getOperationPointer(operation));
|
|
2244
|
-
const pathLevelParams = resolveParams(operation.pathItem.parameters ?? [], pathPointer);
|
|
2245
|
-
const paramMap = /* @__PURE__ */ new Map();
|
|
2246
|
-
for (const entry of [...pathLevelParams, ...operationParams]) if (entry.param.name && entry.param.in) paramMap.set(`${entry.param.in}:${entry.param.name}`, entry);
|
|
2247
|
-
return Array.from(paramMap.values());
|
|
2248
|
-
}
|
|
2249
|
-
const PARAMETER_STYLES = /* @__PURE__ */ new Set([
|
|
2250
|
-
"matrix",
|
|
2251
|
-
"label",
|
|
2252
|
-
"form",
|
|
2253
|
-
"simple",
|
|
2254
|
-
"spaceDelimited",
|
|
2255
|
-
"pipeDelimited",
|
|
2256
|
-
"deepObject",
|
|
2257
|
-
"cookie"
|
|
2258
|
-
]);
|
|
2259
|
-
function pickStyleFields({ style, explode, allowReserved }) {
|
|
2260
|
-
return {
|
|
2261
|
-
...style !== void 0 && PARAMETER_STYLES.has(style) ? { style } : {},
|
|
2262
|
-
...explode !== void 0 ? { explode } : {},
|
|
2263
|
-
...allowReserved !== void 0 ? { allowReserved } : {}
|
|
2264
|
-
};
|
|
2265
|
-
}
|
|
2266
|
-
function getEncodingFields({ encoding, prefixEncoding, itemEncoding }) {
|
|
2267
|
-
const entries = Object.entries(encoding ?? {}).flatMap(([property, entry]) => {
|
|
2268
|
-
const node = toEncodingNode(entry);
|
|
2269
|
-
return Object.keys(node).length ? [[property, node]] : [];
|
|
2270
|
-
});
|
|
2271
|
-
const item = itemEncoding && toEncodingNode(itemEncoding);
|
|
2272
|
-
return {
|
|
2273
|
-
...entries.length && { encoding: Object.fromEntries(entries) },
|
|
2274
|
-
...prefixEncoding?.length && { prefixEncoding: prefixEncoding.map(toEncodingNode) },
|
|
2275
|
-
...item && Object.keys(item).length && { itemEncoding: item }
|
|
2436
|
+
function convertRef({ schema, name, nullable, defaultValue, document, parse, refs, renames }) {
|
|
2437
|
+
const refPath = schema.$ref;
|
|
2438
|
+
const resolvedSchema = refPath ? refs.resolveNode(refPath, parse) : null;
|
|
2439
|
+
const ctx = {
|
|
2440
|
+
schema,
|
|
2441
|
+
name,
|
|
2442
|
+
nullable,
|
|
2443
|
+
defaultValue
|
|
2276
2444
|
};
|
|
2277
|
-
}
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2445
|
+
if (refPath && document.components && refs.resolve(refPath, { report: false }) === null) return createNode(ctx, { type: ast.schema.types.unknown });
|
|
2446
|
+
if (refPath && !refPath.startsWith("#/components/schemas/") && resolvedSchema) return ast.factory.createSchema({
|
|
2447
|
+
...resolvedSchema,
|
|
2448
|
+
title: schema.title ?? resolvedSchema.title,
|
|
2449
|
+
description: schema.description ?? resolvedSchema.description,
|
|
2450
|
+
deprecated: schema.deprecated ?? resolvedSchema.deprecated,
|
|
2451
|
+
readOnly: schema.readOnly ?? resolvedSchema.readOnly,
|
|
2452
|
+
writeOnly: schema.writeOnly ?? resolvedSchema.writeOnly,
|
|
2453
|
+
default: defaultValue ?? resolvedSchema.default,
|
|
2454
|
+
name: name ?? resolvedSchema.name,
|
|
2455
|
+
nullable: nullable ?? resolvedSchema.nullable,
|
|
2456
|
+
extensions: pickExtensions({
|
|
2457
|
+
...resolvedSchema.extensions,
|
|
2458
|
+
...schema
|
|
2286
2459
|
})
|
|
2287
|
-
};
|
|
2460
|
+
});
|
|
2461
|
+
const targetName = renames?.get(schema.$ref);
|
|
2462
|
+
return createNode(ctx, {
|
|
2463
|
+
type: ast.schema.types.ref,
|
|
2464
|
+
name: ast.ref.getName(schema.$ref),
|
|
2465
|
+
ref: schema.$ref,
|
|
2466
|
+
...targetName ? { targetName } : {},
|
|
2467
|
+
schema: resolvedSchema
|
|
2468
|
+
});
|
|
2288
2469
|
}
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2470
|
+
/** Converts `{ ...rest, not: X }` into `intersection[rest, not[X]]`; a bare `required` inside `not` borrows the outer `properties`. */
|
|
2471
|
+
function convertNot({ schema, name, nullable, defaultValue, parse }) {
|
|
2472
|
+
const { not, ...rest } = schema;
|
|
2473
|
+
const [subschema] = resolveUnionMembers([not], rest.properties ?? {});
|
|
2474
|
+
const notNode = ast.factory.createSchema({
|
|
2475
|
+
type: ast.schema.types.not,
|
|
2476
|
+
members: [parse({ schema: subschema })]
|
|
2477
|
+
});
|
|
2478
|
+
return createNode({
|
|
2479
|
+
schema,
|
|
2480
|
+
name,
|
|
2481
|
+
nullable,
|
|
2482
|
+
defaultValue
|
|
2483
|
+
}, {
|
|
2484
|
+
type: ast.schema.types.intersection,
|
|
2485
|
+
members: [parse({
|
|
2486
|
+
schema: rest,
|
|
2487
|
+
name
|
|
2488
|
+
}), notNode]
|
|
2302
2489
|
});
|
|
2303
|
-
if (named.length) return { examples: named };
|
|
2304
|
-
return example !== void 0 ? { examples: [{ value: example }] } : {};
|
|
2305
2490
|
}
|
|
2306
2491
|
/**
|
|
2307
|
-
*
|
|
2492
|
+
* Keywords that describe a schema or conditionally constrain it, but add no structure.
|
|
2308
2493
|
*/
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2494
|
+
const annotationKeys = /* @__PURE__ */ new Set([
|
|
2495
|
+
"description",
|
|
2496
|
+
"title",
|
|
2497
|
+
"example",
|
|
2498
|
+
"examples",
|
|
2499
|
+
"default",
|
|
2500
|
+
"deprecated",
|
|
2501
|
+
"readOnly",
|
|
2502
|
+
"writeOnly",
|
|
2503
|
+
"if",
|
|
2504
|
+
"then",
|
|
2505
|
+
"else"
|
|
2506
|
+
]);
|
|
2312
2507
|
/**
|
|
2313
|
-
*
|
|
2314
|
-
*
|
|
2508
|
+
* Moves `allOf` members that hold only annotations or `if`/`then`/`else` onto the parent, so they do
|
|
2509
|
+
* not become `unknown`. The parent's own values win. The parser reads their `if`/`then`/`else` into `conditionals`.
|
|
2315
2510
|
*/
|
|
2316
|
-
function
|
|
2317
|
-
|
|
2318
|
-
content,
|
|
2319
|
-
contentType,
|
|
2320
|
-
refs
|
|
2321
|
-
});
|
|
2322
|
-
if (!contentType || !entry) return null;
|
|
2323
|
-
const schema = getItemSchema({
|
|
2324
|
-
contentType,
|
|
2325
|
-
mediaType: entry,
|
|
2326
|
-
refs,
|
|
2327
|
-
document
|
|
2328
|
-
}) ?? entry.schema;
|
|
2329
|
-
const binary = entry.itemSchema ? void 0 : getBinaryFallbackSchema(contentType, schema);
|
|
2330
|
-
if (binary) return binary;
|
|
2331
|
-
return schema ? refs.derefKeepingRef(schema) : null;
|
|
2332
|
-
}
|
|
2333
|
-
function getLinks(links, refs) {
|
|
2334
|
-
const entries = Object.entries(links ?? {}).flatMap(([name, value]) => {
|
|
2335
|
-
const link = refs.deref(value);
|
|
2336
|
-
if (!link) return [];
|
|
2337
|
-
const server = link.server ? toInputServers([link.server])[0] : void 0;
|
|
2338
|
-
return [[name, {
|
|
2339
|
-
...pickStrings(link, [
|
|
2340
|
-
"operationId",
|
|
2341
|
-
"operationRef",
|
|
2342
|
-
"description"
|
|
2343
|
-
]),
|
|
2344
|
-
...link.parameters && { parameters: link.parameters },
|
|
2345
|
-
..."requestBody" in link && { requestBody: link.requestBody },
|
|
2346
|
-
...server && { server }
|
|
2347
|
-
}]];
|
|
2348
|
-
});
|
|
2349
|
-
return entries.length ? Object.fromEntries(entries) : void 0;
|
|
2511
|
+
function isAnnotationMember(member) {
|
|
2512
|
+
return !!member && typeof member === "object" && !isReference(member) && Object.keys(member).every((key) => annotationKeys.has(key));
|
|
2350
2513
|
}
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
const
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
}
|
|
2360
|
-
return { stream: {
|
|
2361
|
-
...raw ? { schema: node } : {},
|
|
2362
|
-
...raw && isEventStream(contentType) ? { shape: getItemShape(raw, refs) } : {}
|
|
2363
|
-
} };
|
|
2514
|
+
function mergeAnnotations(schema) {
|
|
2515
|
+
const members = schema.allOf;
|
|
2516
|
+
const annotations = members.filter(isAnnotationMember);
|
|
2517
|
+
const rest = members.filter((member) => !isAnnotationMember(member));
|
|
2518
|
+
return annotations.length && rest.length ? {
|
|
2519
|
+
...Object.assign({}, ...annotations),
|
|
2520
|
+
...schema,
|
|
2521
|
+
allOf: rest
|
|
2522
|
+
} : schema;
|
|
2364
2523
|
}
|
|
2365
|
-
/**
|
|
2366
|
-
|
|
2367
|
-
|
|
2524
|
+
/**
|
|
2525
|
+
* Whether the parent's own keywords become an intersection member: always beside `properties`, and
|
|
2526
|
+
* beside several patterns when the parent holds more than its type and annotations, such as `minLength`.
|
|
2527
|
+
*/
|
|
2528
|
+
function keepsOwnKeywords(schema) {
|
|
2529
|
+
if (schema.properties) return true;
|
|
2530
|
+
if (!hasSeveralPatterns(schema)) return false;
|
|
2531
|
+
return Object.keys(schema).some((key) => key !== "allOf" && key !== "type" && key !== "nullable" && !annotationKeys.has(key));
|
|
2368
2532
|
}
|
|
2369
|
-
/**
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2533
|
+
/**
|
|
2534
|
+
* Whether a one-member `allOf` adds nothing but annotations, so it parses as that member.
|
|
2535
|
+
*/
|
|
2536
|
+
function isSingleMemberAllOf(schema) {
|
|
2537
|
+
return schema.allOf.length === 1 && !schema.properties && !(Array.isArray(schema.required) && schema.required.length) && schema.additionalProperties === void 0 && !hasSeveralPatterns(schema);
|
|
2373
2538
|
}
|
|
2374
2539
|
/**
|
|
2375
|
-
*
|
|
2376
|
-
* every path, in document order. `x-` path keys and unresolvable path-item `$ref`s are skipped.
|
|
2540
|
+
* Converts an `allOf` schema into a flattened node or an `IntersectionSchemaNode`.
|
|
2377
2541
|
*/
|
|
2378
|
-
function
|
|
2379
|
-
const
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
if (!pathItem) continue;
|
|
2386
|
-
const item = pathItem;
|
|
2387
|
-
for (const method of Object.keys(item)) {
|
|
2388
|
-
if (!SUPPORTED_METHODS.has(method)) continue;
|
|
2389
|
-
const schema = item[method];
|
|
2390
|
-
if (!schema || typeof schema !== "object") continue;
|
|
2391
|
-
operations.push({
|
|
2392
|
-
path,
|
|
2393
|
-
method,
|
|
2394
|
-
schema,
|
|
2395
|
-
pathItem
|
|
2396
|
-
});
|
|
2397
|
-
}
|
|
2398
|
-
for (const [method, schema] of Object.entries(pathItem.additionalOperations ?? {})) if (schema && typeof schema === "object" && !SUPPORTED_METHODS.has(method.toLowerCase())) operations.push({
|
|
2399
|
-
path,
|
|
2400
|
-
method,
|
|
2401
|
-
schema,
|
|
2402
|
-
pathItem
|
|
2542
|
+
function convertAllOf({ schema: original, name, nullable, defaultValue, parse, refs }) {
|
|
2543
|
+
const schema = mergeAnnotations(original);
|
|
2544
|
+
if (isSingleMemberAllOf(schema)) {
|
|
2545
|
+
const [memberSchema] = schema.allOf;
|
|
2546
|
+
const memberNode = parse({
|
|
2547
|
+
schema: memberSchema,
|
|
2548
|
+
name
|
|
2403
2549
|
});
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
const unique = suffixed(operationId, taken, counts);
|
|
2426
|
-
taken.add(pascalCase(unique));
|
|
2427
|
-
claimed.add(pascalCase(unique));
|
|
2428
|
-
Diagnostics.report({
|
|
2429
|
-
code: Diagnostics.code.duplicateOperationId,
|
|
2430
|
-
severity: "warning",
|
|
2431
|
-
message: `operationId "${operationId}" of ${operation.method.toUpperCase()} ${operation.path} is already used, so it is renamed to "${unique}".`,
|
|
2432
|
-
help: "Give each operation its own `operationId`.",
|
|
2433
|
-
location: {
|
|
2434
|
-
kind: "operation",
|
|
2435
|
-
pointer: getOperationPointer(operation)
|
|
2436
|
-
}
|
|
2550
|
+
const { kind: _kind, ...memberNodeProps } = memberNode;
|
|
2551
|
+
const mergedNullable = nullable || memberNode.nullable || void 0;
|
|
2552
|
+
const mergedDefault = schema.default === null && mergedNullable ? void 0 : schema.default ?? memberNode.default;
|
|
2553
|
+
return ast.factory.createSchema({
|
|
2554
|
+
...memberNodeProps,
|
|
2555
|
+
name,
|
|
2556
|
+
title: schema.title ?? memberNode.title,
|
|
2557
|
+
description: schema.description ?? memberNode.description,
|
|
2558
|
+
deprecated: schema.deprecated ?? memberNode.deprecated,
|
|
2559
|
+
nullable: mergedNullable,
|
|
2560
|
+
readOnly: schema.readOnly ?? memberNode.readOnly,
|
|
2561
|
+
writeOnly: schema.writeOnly ?? memberNode.writeOnly,
|
|
2562
|
+
default: mergedDefault,
|
|
2563
|
+
examples: extractExamples(schema) ?? memberNode.examples,
|
|
2564
|
+
pattern: schema.pattern ?? ("pattern" in memberNode ? memberNode.pattern : void 0),
|
|
2565
|
+
format: schema.format ?? memberNode.format,
|
|
2566
|
+
extensions: pickExtensions({
|
|
2567
|
+
...memberNode.extensions,
|
|
2568
|
+
...schema
|
|
2569
|
+
}),
|
|
2570
|
+
externalDocs: getExternalDocs(schema.externalDocs) ?? memberNode.externalDocs
|
|
2437
2571
|
});
|
|
2438
|
-
return {
|
|
2439
|
-
...operation,
|
|
2440
|
-
schema: {
|
|
2441
|
-
...operation.schema,
|
|
2442
|
-
operationId: unique
|
|
2443
|
-
}
|
|
2444
|
-
};
|
|
2445
|
-
});
|
|
2446
|
-
}
|
|
2447
|
-
/** Property names whose schema has a truthy `readOnly` or `writeOnly` flag; `$ref` entries are skipped. */
|
|
2448
|
-
function collectPropertyKeysByFlag(schema, flag) {
|
|
2449
|
-
if (!schema?.properties) return null;
|
|
2450
|
-
const keys = [];
|
|
2451
|
-
for (const key in schema.properties) {
|
|
2452
|
-
const prop = schema.properties[key];
|
|
2453
|
-
if (prop && !isReference(prop) && prop[flag]) keys.push(key);
|
|
2454
2572
|
}
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
const mediaType = getMediaType({
|
|
2462
|
-
content: param.content,
|
|
2463
|
-
contentType,
|
|
2573
|
+
const { members: discriminatedAllOf, discriminantValues } = extractDiscriminatedAllOfMembers({
|
|
2574
|
+
allOfMembers: removeInheritedDiscriminator({
|
|
2575
|
+
members: schema.allOf,
|
|
2576
|
+
refs
|
|
2577
|
+
}),
|
|
2578
|
+
name,
|
|
2464
2579
|
refs
|
|
2465
2580
|
});
|
|
2466
|
-
const
|
|
2467
|
-
const
|
|
2468
|
-
schema:
|
|
2469
|
-
name
|
|
2470
|
-
})
|
|
2471
|
-
const
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2581
|
+
const siblingProperties = collectSiblingProperties(schema.allOf, refs, schema.properties);
|
|
2582
|
+
const allOfMembers = discriminatedAllOf.map((s) => isReference(s) ? s : resolveUnionSchema(s, siblingProperties)).map((s) => parse({
|
|
2583
|
+
schema: s,
|
|
2584
|
+
name
|
|
2585
|
+
}));
|
|
2586
|
+
const syntheticStart = allOfMembers.length;
|
|
2587
|
+
if (Array.isArray(schema.required) && schema.required.length) {
|
|
2588
|
+
const outerKeys = schema.properties ? new Set(Object.keys(schema.properties)) : /* @__PURE__ */ new Set();
|
|
2589
|
+
const missingRequired = schema.required.filter((key) => !outerKeys.has(key));
|
|
2590
|
+
if (missingRequired.length) {
|
|
2591
|
+
const resolvedMembers = schema.allOf.flatMap((item) => {
|
|
2592
|
+
if (!isReference(item)) return [item];
|
|
2593
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2594
|
+
let deref = item;
|
|
2595
|
+
while (deref && isReference(deref) && !seen.has(deref.$ref)) {
|
|
2596
|
+
seen.add(deref.$ref);
|
|
2597
|
+
deref = refs.resolve(deref.$ref);
|
|
2598
|
+
}
|
|
2599
|
+
return deref && !isReference(deref) ? [deref] : [];
|
|
2600
|
+
});
|
|
2601
|
+
for (const key of missingRequired) for (const resolved of resolvedMembers) {
|
|
2602
|
+
const prop = resolved.properties?.[key];
|
|
2603
|
+
if (prop) {
|
|
2604
|
+
const memberSchema = {
|
|
2605
|
+
properties: { [key]: prop },
|
|
2606
|
+
required: [key]
|
|
2607
|
+
};
|
|
2608
|
+
allOfMembers.push(parse({
|
|
2609
|
+
schema: memberSchema,
|
|
2610
|
+
name
|
|
2611
|
+
}));
|
|
2612
|
+
break;
|
|
2613
|
+
}
|
|
2614
|
+
}
|
|
2615
|
+
}
|
|
2616
|
+
}
|
|
2617
|
+
if (keepsOwnKeywords(schema)) {
|
|
2618
|
+
const { allOf: _allOf, ...schemaWithoutAllOf } = schema;
|
|
2619
|
+
allOfMembers.push(parse({ schema: schemaWithoutAllOf }));
|
|
2620
|
+
}
|
|
2621
|
+
for (const { propertyName, values } of discriminantValues) allOfMembers.push(createDiscriminantNode({
|
|
2622
|
+
propertyName,
|
|
2623
|
+
values
|
|
2624
|
+
}));
|
|
2625
|
+
return createNode({
|
|
2626
|
+
schema,
|
|
2627
|
+
name,
|
|
2628
|
+
nullable,
|
|
2629
|
+
defaultValue
|
|
2630
|
+
}, {
|
|
2631
|
+
type: ast.schema.types.intersection,
|
|
2632
|
+
members: [...mergeAdjacentObjectsLazy(allOfMembers.slice(0, syntheticStart)), ...mergeAdjacentObjectsLazy(allOfMembers.slice(syntheticStart))]
|
|
2493
2633
|
});
|
|
2494
2634
|
}
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2635
|
+
const scalarTypes = /* @__PURE__ */ new Set([
|
|
2636
|
+
"string",
|
|
2637
|
+
"number",
|
|
2638
|
+
"integer",
|
|
2639
|
+
"boolean"
|
|
2640
|
+
]);
|
|
2641
|
+
/**
|
|
2642
|
+
* Copies a scalar parent `type` and `format` onto inline members without a `type`. JSON Schema applies
|
|
2643
|
+
* sibling keywords to every member, so `type: 'integer', anyOf: [{ minimum: 10 }]` keeps each member an integer.
|
|
2644
|
+
*/
|
|
2645
|
+
function inheritScalarType(members, { schema, type }) {
|
|
2646
|
+
if (!type || !scalarTypes.has(type)) return members;
|
|
2647
|
+
if (Array.isArray(schema.type) && schema.type.filter((t) => t !== "null").length > 1) return members;
|
|
2648
|
+
return members.map((member) => {
|
|
2649
|
+
if (!member || typeof member !== "object" || isReference(member) || member.type !== void 0) return member;
|
|
2650
|
+
const obj = member;
|
|
2651
|
+
return {
|
|
2652
|
+
type,
|
|
2653
|
+
...schema.format !== void 0 && obj.format === void 0 && { format: schema.format },
|
|
2654
|
+
...obj
|
|
2655
|
+
};
|
|
2513
2656
|
});
|
|
2514
2657
|
}
|
|
2515
2658
|
/**
|
|
2516
|
-
* Converts
|
|
2659
|
+
* Converts a `oneOf` / `anyOf` schema into a `UnionSchemaNode`.
|
|
2517
2660
|
*/
|
|
2518
|
-
function
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
operation,
|
|
2525
|
-
refs
|
|
2526
|
-
}).map((entry) => parseParameter({
|
|
2527
|
-
options,
|
|
2528
|
-
parseSchema,
|
|
2529
|
-
refs,
|
|
2530
|
-
...entry,
|
|
2531
|
-
parentName: operationName
|
|
2532
|
-
})).flatMap(spreadQuerystring);
|
|
2533
|
-
const body = getRequestBody({
|
|
2534
|
-
operation,
|
|
2535
|
-
refs
|
|
2536
|
-
});
|
|
2537
|
-
const bodyRequired = body?.required === true;
|
|
2538
|
-
const requestBodyName = operationName ? `${operationName}Request` : void 0;
|
|
2539
|
-
const content = (contentType ? [contentType] : Object.keys(body?.content ?? {})).flatMap((ct) => {
|
|
2540
|
-
const schema = getBodySchema({
|
|
2541
|
-
content: body?.content,
|
|
2542
|
-
contentType: ct,
|
|
2543
|
-
refs,
|
|
2544
|
-
document
|
|
2661
|
+
function convertUnion(context) {
|
|
2662
|
+
if (context.schema.enum?.length) {
|
|
2663
|
+
const { enum: values, ...union } = context.schema;
|
|
2664
|
+
const nullable = context.nullable || [...union.oneOf ?? [], ...union.anyOf ?? []].some((member) => {
|
|
2665
|
+
const resolved = isReference(member) ? context.refs.resolve(member.$ref, { report: false }) : member;
|
|
2666
|
+
return !!resolved && !isReference(resolved) && isNullable(resolved, context.document);
|
|
2545
2667
|
});
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2668
|
+
const enumSchema = {
|
|
2669
|
+
type: union.type,
|
|
2670
|
+
enum: nullable && isLegacyDocument(context.document) ? [...values, null] : values
|
|
2671
|
+
};
|
|
2672
|
+
const members = [convertUnion({
|
|
2673
|
+
...context,
|
|
2674
|
+
schema: union
|
|
2675
|
+
}), context.parse({ schema: enumSchema })];
|
|
2676
|
+
return createNode(context, {
|
|
2677
|
+
type: ast.schema.types.intersection,
|
|
2678
|
+
members
|
|
2551
2679
|
});
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2680
|
+
}
|
|
2681
|
+
const annotatedEnum = convertAnnotatedEnum(context);
|
|
2682
|
+
if (annotatedEnum) return annotatedEnum;
|
|
2683
|
+
const { schema, name, nullable, defaultValue, parse, refs } = context;
|
|
2684
|
+
const ctx = {
|
|
2685
|
+
schema,
|
|
2686
|
+
name,
|
|
2687
|
+
nullable,
|
|
2688
|
+
defaultValue
|
|
2689
|
+
};
|
|
2690
|
+
const discriminator = isDiscriminator(schema) ? schema.discriminator : void 0;
|
|
2691
|
+
const declaredMembers = [...schema.oneOf ?? [], ...schema.anyOf ?? []];
|
|
2692
|
+
const defaultRef = getDefaultMappingRef(typeof schema.discriminator === "object" ? schema.discriminator : void 0);
|
|
2693
|
+
const unionMembers = resolveUnionMembers(inheritScalarType(defaultRef && !declaredMembers.some((member) => isReference(member) && member.$ref === defaultRef) ? [...declaredMembers, { $ref: defaultRef }] : declaredMembers, context), schema.properties ?? {});
|
|
2694
|
+
const strategy = schema.oneOf ? "one" : "any";
|
|
2695
|
+
const explicitDiscriminatorPropertyName = discriminator?.propertyName;
|
|
2696
|
+
const { oneOf: _o, anyOf: _a, discriminator: _d, ...memberBaseSchema } = schema;
|
|
2697
|
+
const sharedPropertiesNode = schema.properties ? parse({
|
|
2698
|
+
schema: memberBaseSchema,
|
|
2699
|
+
name
|
|
2700
|
+
}) : void 0;
|
|
2701
|
+
const missingRequired = (Array.isArray(schema.required) ? schema.required : []).filter((key) => !Object.hasOwn(schema.properties ?? {}, key));
|
|
2702
|
+
if (sharedPropertiesNode || discriminator || missingRequired.length) {
|
|
2703
|
+
const members = narrowUnionMembers({
|
|
2704
|
+
unionMembers,
|
|
2705
|
+
discriminator,
|
|
2706
|
+
sharedPropertiesNode,
|
|
2707
|
+
parentType: discriminator && propertyTypeOf({
|
|
2708
|
+
schema,
|
|
2709
|
+
propertyName: discriminator.propertyName,
|
|
2710
|
+
refs
|
|
2569
2711
|
}),
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
const requestBody = content.length > 0 || body?.description ? {
|
|
2574
|
-
pointer: `${pointer}/requestBody`,
|
|
2575
|
-
description: body?.description,
|
|
2576
|
-
required: bodyRequired || void 0,
|
|
2577
|
-
content: content.length > 0 ? content : void 0,
|
|
2578
|
-
extensions: pickExtensions(body)
|
|
2579
|
-
} : void 0;
|
|
2580
|
-
const responses = getResponseStatusCodes(operation).map((key) => {
|
|
2581
|
-
const response = getResponseByStatusCode({
|
|
2582
|
-
operation,
|
|
2583
|
-
refs,
|
|
2584
|
-
statusCode: key
|
|
2712
|
+
parse,
|
|
2713
|
+
name,
|
|
2714
|
+
refs
|
|
2585
2715
|
});
|
|
2586
|
-
const
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
content: response?.content,
|
|
2597
|
-
contentType: ct,
|
|
2598
|
-
refs
|
|
2599
|
-
});
|
|
2600
|
-
const node = raw && Object.keys(raw).length > 0 ? parseSchema({
|
|
2601
|
-
schema: raw,
|
|
2602
|
-
name: responseName
|
|
2603
|
-
}) : ast.factory.createSchema({ type: options.emptySchemaType });
|
|
2604
|
-
if (raw && ct) scanSchema(node, `${mediaTypePointer(response?.content, ct, `${pointer}/responses/${key}/content/${escapePointerToken(ct)}`)}/${schemaKeyword(mediaType)}`);
|
|
2605
|
-
return {
|
|
2606
|
-
schema: node,
|
|
2607
|
-
keysToOmit: collectPropertyKeysByFlag(raw, "writeOnly"),
|
|
2608
|
-
...mediaType && getEncodingFields(mediaType),
|
|
2609
|
-
...mediaType && getExamples(mediaType, refs),
|
|
2610
|
-
...ct ? getStreamField({
|
|
2611
|
-
contentType: ct,
|
|
2612
|
-
mediaType,
|
|
2613
|
-
refs,
|
|
2614
|
-
document,
|
|
2615
|
-
node
|
|
2616
|
-
}) : {},
|
|
2617
|
-
extensions: pickExtensions(mediaType)
|
|
2618
|
-
};
|
|
2619
|
-
};
|
|
2620
|
-
const declaredContentTypes = Object.keys(response?.content ?? {});
|
|
2621
|
-
const responseContent = (contentType && declaredContentTypes.length ? [contentType] : declaredContentTypes).map((ct) => ast.factory.createContent({
|
|
2622
|
-
contentType: ct,
|
|
2623
|
-
...parseEntrySchema(ct)
|
|
2716
|
+
const unionNode = createNode(ctx, {
|
|
2717
|
+
type: ast.schema.types.union,
|
|
2718
|
+
strategy,
|
|
2719
|
+
members,
|
|
2720
|
+
discriminatorPropertyName: explicitDiscriminatorPropertyName ?? ast.schema.inferDiscriminator(members)
|
|
2721
|
+
});
|
|
2722
|
+
const requiredProperties = missingRequired.map((name) => ast.factory.createProperty({
|
|
2723
|
+
name,
|
|
2724
|
+
required: true,
|
|
2725
|
+
schema: ast.factory.createSchema({ type: ast.schema.types.unknown })
|
|
2624
2726
|
}));
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2727
|
+
const siblings = sharedPropertiesNode ? [sharedPropertiesNode] : [];
|
|
2728
|
+
if (requiredProperties.length) siblings.push(ast.factory.createSchema({
|
|
2729
|
+
type: ast.schema.types.object,
|
|
2730
|
+
properties: requiredProperties
|
|
2628
2731
|
}));
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
return [parseParameter({
|
|
2634
|
-
param: {
|
|
2635
|
-
...header,
|
|
2636
|
-
name,
|
|
2637
|
-
in: "header"
|
|
2638
|
-
},
|
|
2639
|
-
pointer: `${responsePointer}/headers/${escapePointerToken(name)}`,
|
|
2640
|
-
parentName: responseName,
|
|
2641
|
-
options,
|
|
2642
|
-
parseSchema,
|
|
2643
|
-
refs
|
|
2644
|
-
})];
|
|
2645
|
-
});
|
|
2646
|
-
const links = getLinks(response?.links, refs);
|
|
2647
|
-
return ast.factory.createResponse({
|
|
2648
|
-
pointer: responsePointer,
|
|
2649
|
-
statusCode,
|
|
2650
|
-
summary: response?.summary,
|
|
2651
|
-
description: response?.description,
|
|
2652
|
-
content: responseContent,
|
|
2653
|
-
...headers.length && { headers },
|
|
2654
|
-
...links && { links },
|
|
2655
|
-
extensions: pickExtensions(response)
|
|
2732
|
+
if (!siblings.length) return unionNode;
|
|
2733
|
+
return createNode(ctx, {
|
|
2734
|
+
type: ast.schema.types.intersection,
|
|
2735
|
+
members: [unionNode, ...siblings]
|
|
2656
2736
|
});
|
|
2737
|
+
}
|
|
2738
|
+
const members = unionMembers.map((s) => parse({
|
|
2739
|
+
schema: s,
|
|
2740
|
+
name
|
|
2741
|
+
}));
|
|
2742
|
+
const unionNode = createNode(ctx, {
|
|
2743
|
+
type: ast.schema.types.union,
|
|
2744
|
+
strategy,
|
|
2745
|
+
members,
|
|
2746
|
+
discriminatorPropertyName: explicitDiscriminatorPropertyName ?? ast.schema.inferDiscriminator(members)
|
|
2657
2747
|
});
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2748
|
+
return ast.macro.apply(unionNode, [macroDedupeUnion], { depth: "shallow" });
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
2751
|
+
* Converts an OAS 3.1 multi-type array (e.g. `type: ['string', 'number']`) into a `UnionSchemaNode`.
|
|
2752
|
+
* Only called once the multi-type rule's `match` has confirmed more than one non-`null` type
|
|
2753
|
+
* remains; a single remaining type (e.g. `['string', 'null']`) is handled as that type instead,
|
|
2754
|
+
* with nullability already folded in.
|
|
2755
|
+
*/
|
|
2756
|
+
function convertMultiType({ schema, name, nullable, defaultValue, parse }) {
|
|
2757
|
+
const nonNullTypes = schema.type.filter((t) => t !== "null");
|
|
2758
|
+
return createNode({
|
|
2759
|
+
schema,
|
|
2760
|
+
name,
|
|
2761
|
+
nullable,
|
|
2762
|
+
defaultValue
|
|
2763
|
+
}, {
|
|
2764
|
+
type: ast.schema.types.union,
|
|
2765
|
+
members: nonNullTypes.map((t) => {
|
|
2766
|
+
return parse({
|
|
2767
|
+
schema: {
|
|
2768
|
+
...Object.fromEntries(Object.entries(schema).filter(([key]) => !key.startsWith("x-"))),
|
|
2769
|
+
type: t
|
|
2770
|
+
},
|
|
2771
|
+
name
|
|
2772
|
+
});
|
|
2773
|
+
})
|
|
2683
2774
|
});
|
|
2684
2775
|
}
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
} : param)
|
|
2696
|
-
};
|
|
2776
|
+
// src/schemaDiagnostics.ts
|
|
2777
|
+
/**
|
|
2778
|
+
* Links one freshly converted schema and its nested schemas to their RFC 6901 pointers
|
|
2779
|
+
* (`#/components/schemas/Pet/properties/owner/properties/name`), and reports the advisory
|
|
2780
|
+
* diagnostics (`KUBB_UNSUPPORTED_FORMAT`, `KUBB_DEPRECATED`, `KUBB_DUPLICATE_DISCRIMINATOR`) against them.
|
|
2781
|
+
* Refs are not followed, so the resolved schema is handled under its own walk.
|
|
2782
|
+
* Reports land in the active build run, are a no-op outside one, and repeats are deduped by the build.
|
|
2783
|
+
*/
|
|
2784
|
+
function scanSchema(node, pointer = node.pointer) {
|
|
2785
|
+
if (pointer) visit(node, pointer);
|
|
2697
2786
|
}
|
|
2698
|
-
|
|
2787
|
+
var FormatTally = class {
|
|
2788
|
+
#pointers;
|
|
2789
|
+
group(fn) {
|
|
2790
|
+
this.#pointers = memo.create();
|
|
2791
|
+
try {
|
|
2792
|
+
return fn();
|
|
2793
|
+
} finally {
|
|
2794
|
+
const tally = this.#pointers;
|
|
2795
|
+
this.#pointers = void 0;
|
|
2796
|
+
for (const [format, pointers] of tally) reportUnsupportedFormat(format, [...pointers]);
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
add(format, pointer) {
|
|
2800
|
+
if (!this.#pointers) return reportUnsupportedFormat(format, [pointer]);
|
|
2801
|
+
memo(this.#pointers, format, () => /* @__PURE__ */ new Set()).add(pointer);
|
|
2802
|
+
}
|
|
2803
|
+
};
|
|
2804
|
+
const formatTally = new FormatTally();
|
|
2699
2805
|
/**
|
|
2700
|
-
*
|
|
2701
|
-
*
|
|
2702
|
-
*
|
|
2806
|
+
* Runs `fn` and reports `KUBB_UNSUPPORTED_FORMAT` once per format with the number of schemas that use it,
|
|
2807
|
+
* instead of once per schema. `format` is an open value in OpenAPI, so a large spec would otherwise print
|
|
2808
|
+
* thousands of warnings that say the same thing. `fn` must be synchronous.
|
|
2703
2809
|
*/
|
|
2704
|
-
function
|
|
2705
|
-
return
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
...extras
|
|
2810
|
+
function groupUnsupportedFormats(fn) {
|
|
2811
|
+
return formatTally.group(fn);
|
|
2812
|
+
}
|
|
2813
|
+
function reportUnsupportedFormat(format, pointers) {
|
|
2814
|
+
const count = pointers.length;
|
|
2815
|
+
Diagnostics.report({
|
|
2816
|
+
code: Diagnostics.code.unsupportedFormat,
|
|
2817
|
+
severity: "warning",
|
|
2818
|
+
message: count === 1 ? `Kubb does not map the format "${format}" to a specific type, so it falls back to the base type.` : `Kubb does not map the format "${format}" to a specific type, so ${count} schemas fall back to the base type.`,
|
|
2819
|
+
help: `Use a format Kubb supports, or handle "${format}" with a custom parser or plugin.`,
|
|
2820
|
+
location: {
|
|
2821
|
+
kind: "schema",
|
|
2822
|
+
pointer: pointers[0]
|
|
2823
|
+
}
|
|
2719
2824
|
});
|
|
2720
2825
|
}
|
|
2721
|
-
|
|
2826
|
+
function visit(node, pointer) {
|
|
2827
|
+
node.pointer ??= pointer;
|
|
2828
|
+
if (node.deprecated) Diagnostics.report({
|
|
2829
|
+
code: Diagnostics.code.deprecated,
|
|
2830
|
+
severity: "info",
|
|
2831
|
+
message: "This schema is marked as deprecated.",
|
|
2832
|
+
location: {
|
|
2833
|
+
kind: "schema",
|
|
2834
|
+
pointer
|
|
2835
|
+
}
|
|
2836
|
+
});
|
|
2837
|
+
if (typeof node.format === "string" && !isHandledFormat(node.format)) formatTally.add(node.format, pointer);
|
|
2838
|
+
if (node.type === "object") {
|
|
2839
|
+
for (const property of node.properties) visit(property.schema, `${pointer}/properties/${escapePointerToken(property.name)}`);
|
|
2840
|
+
if (node.additionalProperties && typeof node.additionalProperties === "object") visit(node.additionalProperties, `${pointer}/additionalProperties`);
|
|
2841
|
+
return;
|
|
2842
|
+
}
|
|
2843
|
+
if (node.type === "array") {
|
|
2844
|
+
for (const item of node.items ?? []) visit(item, `${pointer}/items`);
|
|
2845
|
+
return;
|
|
2846
|
+
}
|
|
2847
|
+
if (node.type === "tuple") {
|
|
2848
|
+
for (const [index, item] of (node.items ?? []).entries()) visit(item, `${pointer}/items/${index}`);
|
|
2849
|
+
return;
|
|
2850
|
+
}
|
|
2851
|
+
if (node.type === "not") {
|
|
2852
|
+
for (const member of node.members ?? []) visit(member, `${pointer}/not`);
|
|
2853
|
+
return;
|
|
2854
|
+
}
|
|
2855
|
+
if (node.type === "union" && node.discriminatorPropertyName) reportDuplicateDiscriminators(node, node.discriminatorPropertyName, pointer);
|
|
2856
|
+
if (node.type === "union" || node.type === "intersection") {
|
|
2857
|
+
const keyword = node.type === "intersection" ? "allOf" : node.strategy === "any" ? "anyOf" : "oneOf";
|
|
2858
|
+
for (const [index, member] of (node.members ?? []).entries()) visit(member, `${pointer}/${keyword}/${index}`);
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2722
2861
|
/**
|
|
2723
|
-
*
|
|
2862
|
+
* Reports a declared discriminator whose value pins more than one member. The spec cannot tell
|
|
2863
|
+
* those members apart, and Zod's `z.discriminatedUnion` throws on the first parse.
|
|
2864
|
+
* One diagnostic per union, since the build dedupes by code and pointer.
|
|
2724
2865
|
*/
|
|
2725
|
-
function
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2866
|
+
function reportDuplicateDiscriminators(node, propertyName, pointer) {
|
|
2867
|
+
const keyword = node.strategy === "any" ? "anyOf" : "oneOf";
|
|
2868
|
+
const membersByValue = memo.create();
|
|
2869
|
+
for (const [index, member] of (node.members ?? []).entries()) {
|
|
2870
|
+
const label = memberName(member) ?? `${keyword}/${index}`;
|
|
2871
|
+
for (const value of memberValues(member, propertyName)) memo(membersByValue, value, () => []).push(label);
|
|
2872
|
+
}
|
|
2873
|
+
const duplicates = [...membersByValue].filter(([, members]) => members.length > 1);
|
|
2874
|
+
if (!duplicates.length) return;
|
|
2875
|
+
Diagnostics.report({
|
|
2876
|
+
code: Diagnostics.code.duplicateDiscriminator,
|
|
2877
|
+
severity: "warning",
|
|
2878
|
+
message: duplicates.map(([value, members]) => `Discriminator "${propertyName}" value ${JSON.stringify(value)} matches more than one member (${members.join(", ")}).`).join(" "),
|
|
2879
|
+
help: `Give each member its own "${propertyName}" value, or drop the discriminator. Zod's z.discriminatedUnion throws on the first parse of this union.`,
|
|
2880
|
+
location: {
|
|
2881
|
+
kind: "schema",
|
|
2882
|
+
pointer
|
|
2734
2883
|
}
|
|
2735
|
-
return key;
|
|
2736
2884
|
});
|
|
2737
2885
|
}
|
|
2886
|
+
function memberValues(node, propertyName, seen = /* @__PURE__ */ new Set()) {
|
|
2887
|
+
if (seen.has(node)) return [];
|
|
2888
|
+
seen.add(node);
|
|
2889
|
+
const target = node.type === "ref" && node.schema ? node.schema : node;
|
|
2890
|
+
if (target.type === "union") return [...new Set(target.members?.flatMap((member) => memberValues(member, propertyName, seen)))];
|
|
2891
|
+
return intersect(ast.schema.findProperties({
|
|
2892
|
+
node,
|
|
2893
|
+
propertyName
|
|
2894
|
+
}).map((property) => [...ast.schema.getLiterals(property.schema)]));
|
|
2895
|
+
}
|
|
2896
|
+
function intersect(slices) {
|
|
2897
|
+
const pinned = slices.filter((values) => values.length > 0);
|
|
2898
|
+
return pinned.reduce((acc, values) => acc.filter((value) => values.includes(value)), pinned[0] ?? []);
|
|
2899
|
+
}
|
|
2900
|
+
function memberName(member) {
|
|
2901
|
+
if (member.type === "ref") return member.name ?? void 0;
|
|
2902
|
+
if (member.type === "intersection") return member.members?.map(memberName).find(Boolean);
|
|
2903
|
+
}
|
|
2904
|
+
// src/model/mediaType.ts
|
|
2905
|
+
const streamMediaTypes = /* @__PURE__ */ new Set([
|
|
2906
|
+
"application/jsonl",
|
|
2907
|
+
"application/x-ndjson",
|
|
2908
|
+
"application/json-seq",
|
|
2909
|
+
"text/event-stream"
|
|
2910
|
+
]);
|
|
2738
2911
|
/**
|
|
2739
|
-
*
|
|
2740
|
-
* A nullable array such as `['integer', 'null']` reads as its one non-null type.
|
|
2912
|
+
* Returns `true` for a media type read as a stream of items: SSE, JSON Lines, a `+json-seq` sequence, or any media type that declares `itemSchema`.
|
|
2741
2913
|
*/
|
|
2742
|
-
function
|
|
2743
|
-
const
|
|
2744
|
-
|
|
2745
|
-
if (seen.has(schema.$ref)) return void 0;
|
|
2746
|
-
seen.add(schema.$ref);
|
|
2747
|
-
}
|
|
2748
|
-
const resolved = deref(schema);
|
|
2749
|
-
const type = deref(resolved?.properties?.[propertyName])?.type ?? resolved?.allOf?.map((member) => propertyTypeOf({
|
|
2750
|
-
schema: member,
|
|
2751
|
-
propertyName,
|
|
2752
|
-
refs,
|
|
2753
|
-
seen
|
|
2754
|
-
})).find(Boolean);
|
|
2755
|
-
const nonNull = Array.isArray(type) ? type.filter((t) => t !== "null") : [];
|
|
2756
|
-
return nonNull.length === 1 ? nonNull[0] : type;
|
|
2914
|
+
function isStream(contentType, mediaType) {
|
|
2915
|
+
const baseType = ast.mediaType.getBase(contentType);
|
|
2916
|
+
return streamMediaTypes.has(baseType) || baseType.startsWith("application/") && baseType.endsWith("+json-seq") || mediaType?.itemSchema !== void 0;
|
|
2757
2917
|
}
|
|
2758
2918
|
/**
|
|
2759
|
-
*
|
|
2760
|
-
*
|
|
2761
|
-
* @example
|
|
2762
|
-
* ```ts
|
|
2763
|
-
* createDiscriminantNode({ propertyName: 'type', values: ['dog'] })
|
|
2764
|
-
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
|
|
2765
|
-
* ```
|
|
2919
|
+
* Whether the media type is `text/event-stream`, with or without parameters.
|
|
2766
2920
|
*/
|
|
2767
|
-
function
|
|
2768
|
-
return ast.
|
|
2769
|
-
type: "object",
|
|
2770
|
-
primitive: "object",
|
|
2771
|
-
properties: [ast.factory.createProperty({
|
|
2772
|
-
name: propertyName,
|
|
2773
|
-
schema: ast.factory.createSchema({
|
|
2774
|
-
type: "enum",
|
|
2775
|
-
primitive: enumPrimitive(values),
|
|
2776
|
-
enumValues: values
|
|
2777
|
-
}),
|
|
2778
|
-
required: true
|
|
2779
|
-
})]
|
|
2780
|
-
});
|
|
2921
|
+
function isEventStream(contentType) {
|
|
2922
|
+
return ast.mediaType.getBase(contentType) === "text/event-stream";
|
|
2781
2923
|
}
|
|
2782
2924
|
/**
|
|
2783
|
-
*
|
|
2925
|
+
* Replaces a `data` string that holds JSON with its `contentSchema`, so the event reads as parsed.
|
|
2784
2926
|
*/
|
|
2785
|
-
function
|
|
2786
|
-
const
|
|
2787
|
-
|
|
2788
|
-
|
|
2927
|
+
function withParsedData(schema, refs) {
|
|
2928
|
+
const data = schema.properties?.data;
|
|
2929
|
+
const resolved = data && isReference(data) ? refs.resolve(data.$ref, { report: false }) : data;
|
|
2930
|
+
if (!resolved?.contentSchema || !resolved.contentMediaType || !isJsonMimeType(resolved.contentMediaType)) return schema;
|
|
2931
|
+
return {
|
|
2932
|
+
...schema,
|
|
2933
|
+
properties: {
|
|
2934
|
+
...schema.properties,
|
|
2935
|
+
data: resolved.contentSchema
|
|
2936
|
+
}
|
|
2937
|
+
};
|
|
2789
2938
|
}
|
|
2790
2939
|
/**
|
|
2791
|
-
*
|
|
2792
|
-
*
|
|
2793
|
-
* A mapping may point several keys at the same schema, in which case the member has to be
|
|
2794
|
-
* narrowed to all of them — keeping only the first would drop the rest from the union.
|
|
2795
|
-
*
|
|
2796
|
-
* @example
|
|
2797
|
-
* ```ts
|
|
2798
|
-
* findDiscriminators({ mapping: { dog: '#/components/schemas/Dog', hound: '#/components/schemas/Dog' }, ref: '#/components/schemas/Dog' }) // ['dog', 'hound']
|
|
2799
|
-
* ```
|
|
2940
|
+
* Inlines a `$ref` event whose JSON `data` string needs its `contentSchema`, so the shared component keeps `data` as a string elsewhere.
|
|
2800
2941
|
*/
|
|
2801
|
-
function
|
|
2802
|
-
|
|
2803
|
-
|
|
2942
|
+
function withParsedRef(schema, refs) {
|
|
2943
|
+
const resolved = refs.resolve(schema.$ref, { report: false });
|
|
2944
|
+
if (!resolved || isReference(resolved)) return schema;
|
|
2945
|
+
const parsed = withParsedData(resolved, refs);
|
|
2946
|
+
return parsed === resolved ? schema : parsed;
|
|
2804
2947
|
}
|
|
2805
2948
|
/**
|
|
2806
|
-
*
|
|
2949
|
+
* Keywords next to `$ref` that only annotate it, so a reference that carries nothing else stays a reference.
|
|
2950
|
+
*/
|
|
2951
|
+
const REF_ANNOTATIONS = /* @__PURE__ */ new Set([
|
|
2952
|
+
"$ref",
|
|
2953
|
+
"summary",
|
|
2954
|
+
"description",
|
|
2955
|
+
"$comment"
|
|
2956
|
+
]);
|
|
2957
|
+
/**
|
|
2958
|
+
* Applies `extra` on top of `base` the way JSON Schema does: `properties` and `required` add up, other keywords of `extra` win.
|
|
2959
|
+
*/
|
|
2960
|
+
function mergeSchemas(base, extra) {
|
|
2961
|
+
const required = [.../* @__PURE__ */ new Set([...Array.isArray(base.required) ? base.required : [], ...Array.isArray(extra.required) ? extra.required : []])];
|
|
2962
|
+
return {
|
|
2963
|
+
type: "object",
|
|
2964
|
+
...base,
|
|
2965
|
+
...extra,
|
|
2966
|
+
properties: {
|
|
2967
|
+
...base.properties,
|
|
2968
|
+
...extra.properties
|
|
2969
|
+
},
|
|
2970
|
+
...required.length ? { required } : {}
|
|
2971
|
+
};
|
|
2972
|
+
}
|
|
2973
|
+
/**
|
|
2974
|
+
* Normalizes the OpenAPI 3.2 `text/event-stream` item schema into one schema per event.
|
|
2975
|
+
* - Keywords next to a `$ref` apply to the referenced schema.
|
|
2976
|
+
* - Shared `properties` merge into each inline `oneOf`/`anyOf` member.
|
|
2977
|
+
* - A JSON `data` string becomes its `contentSchema`.
|
|
2807
2978
|
*/
|
|
2808
|
-
function
|
|
2809
|
-
|
|
2979
|
+
function toEventSchema(schema, refs) {
|
|
2980
|
+
if (isReference(schema)) {
|
|
2981
|
+
const siblings = Object.fromEntries(Object.entries(schema).filter(([key]) => !REF_ANNOTATIONS.has(key)));
|
|
2982
|
+
const resolved = Object.keys(siblings).length ? refs.resolve(schema.$ref, { report: false }) : null;
|
|
2983
|
+
return resolved ? toEventSchema(mergeSchemas(resolved, siblings), refs) : withParsedRef(schema, refs);
|
|
2984
|
+
}
|
|
2985
|
+
const key = schema.oneOf?.length ? "oneOf" : schema.anyOf?.length ? "anyOf" : void 0;
|
|
2986
|
+
if (!key) return withParsedData(schema, refs);
|
|
2987
|
+
const members = schema[key];
|
|
2988
|
+
const { properties, required, ...rest } = schema;
|
|
2989
|
+
const merge = !!properties && !members.some(isReference);
|
|
2990
|
+
const shared = {
|
|
2991
|
+
properties,
|
|
2992
|
+
required
|
|
2993
|
+
};
|
|
2994
|
+
return {
|
|
2995
|
+
...merge ? rest : schema,
|
|
2996
|
+
[key]: members.map((member) => isReference(member) ? withParsedRef(member, refs) : withParsedData(merge ? mergeSchemas(shared, member) : member, refs))
|
|
2997
|
+
};
|
|
2810
2998
|
}
|
|
2999
|
+
const SSE_FIELDS = /* @__PURE__ */ new Set([
|
|
3000
|
+
"event",
|
|
3001
|
+
"id",
|
|
3002
|
+
"retry",
|
|
3003
|
+
"data"
|
|
3004
|
+
]);
|
|
2811
3005
|
/**
|
|
2812
|
-
*
|
|
2813
|
-
* schemas declare on the same property, so a root `$type` accepts values from every level.
|
|
3006
|
+
* Property names of the schema and its `allOf` members, following `$ref`s, as one combined object declares them.
|
|
2814
3007
|
*/
|
|
2815
|
-
function
|
|
2816
|
-
|
|
2817
|
-
if (seen.has(
|
|
2818
|
-
seen.add(
|
|
2819
|
-
const resolved = refs.resolve(
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
seen
|
|
2825
|
-
})] : [key];
|
|
2826
|
-
});
|
|
3008
|
+
function collectFields(schema, refs, seen) {
|
|
3009
|
+
if (isReference(schema)) {
|
|
3010
|
+
if (seen.has(schema.$ref)) return [];
|
|
3011
|
+
seen.add(schema.$ref);
|
|
3012
|
+
const resolved = refs.resolve(schema.$ref, { report: false });
|
|
3013
|
+
return resolved ? collectFields(resolved, refs, seen) : [];
|
|
3014
|
+
}
|
|
3015
|
+
const members = schema.allOf ?? [];
|
|
3016
|
+
return [...Object.keys(schema.properties ?? {}), ...members.flatMap((member) => collectFields(member, refs, seen))];
|
|
2827
3017
|
}
|
|
2828
3018
|
/**
|
|
2829
|
-
*
|
|
2830
|
-
*
|
|
2831
|
-
*
|
|
3019
|
+
* Whether the schema, following `$ref`s, or a `oneOf`/`anyOf` member of it is a whole Server-Sent Event: its combined
|
|
3020
|
+
* `allOf` fields declare `data`, or `event` with no field an event cannot carry. An `event` next to fields such as
|
|
3021
|
+
* `planet` is the payload's own property, sent as JSON in the `data:` line.
|
|
2832
3022
|
*/
|
|
2833
|
-
function
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
3023
|
+
function declaresEventFields(schema, refs, seen) {
|
|
3024
|
+
if (isReference(schema)) {
|
|
3025
|
+
if (seen.has(schema.$ref)) return false;
|
|
3026
|
+
seen.add(schema.$ref);
|
|
3027
|
+
const resolved = refs.resolve(schema.$ref, { report: false });
|
|
3028
|
+
return !!resolved && declaresEventFields(resolved, refs, seen);
|
|
3029
|
+
}
|
|
3030
|
+
const fields = collectFields(schema, refs, new Set(seen));
|
|
3031
|
+
if (fields.includes("data")) return true;
|
|
3032
|
+
if (fields.includes("event")) return fields.every((field) => SSE_FIELDS.has(field));
|
|
3033
|
+
return [...schema.oneOf ?? [], ...schema.anyOf ?? []].some((member) => declaresEventFields(member, refs, seen));
|
|
2844
3034
|
}
|
|
2845
3035
|
/**
|
|
2846
|
-
*
|
|
2847
|
-
* or primitive member keeps its own type instead of an intersection no value can satisfy.
|
|
3036
|
+
* Returns `'event'` when the item is a whole event, directly or in a composition member (see `declaresEventFields`), else `'data'`.
|
|
2848
3037
|
*/
|
|
2849
|
-
function
|
|
2850
|
-
|
|
2851
|
-
if (type === void 0) return true;
|
|
2852
|
-
return Array.isArray(type) ? type.includes("object") : type === "object";
|
|
3038
|
+
function getItemShape(schema, refs) {
|
|
3039
|
+
return declaresEventFields(schema, refs, /* @__PURE__ */ new Set()) ? "event" : "data";
|
|
2853
3040
|
}
|
|
2854
3041
|
/**
|
|
2855
|
-
*
|
|
2856
|
-
* node with either the shared-properties slice carrying that value, or a synthetic discriminant
|
|
2857
|
-
* literal. The referenced child schema's own definition is left untouched — narrowing happens only
|
|
2858
|
-
* at this union usage site, which is what makes this mode "preserve" (as opposed to `propagate`,
|
|
2859
|
-
* which additionally patches the child schema's own definition in a post-pass).
|
|
3042
|
+
* Returns the raw item schema: `itemSchema`, or `schema` on `text/event-stream` as 3.0 and 3.1 documents write one event.
|
|
2860
3043
|
*/
|
|
2861
|
-
function
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
type: "object",
|
|
2867
|
-
primitive: "object",
|
|
2868
|
-
properties: [discriminatorProperty]
|
|
2869
|
-
});
|
|
2870
|
-
}
|
|
2871
|
-
function implicitDiscriminantValue(member) {
|
|
2872
|
-
if (!discriminator || discriminator.mapping || !isReference(member)) return null;
|
|
2873
|
-
const value = ast.ref.getName(member.$ref);
|
|
2874
|
-
if (!value) return null;
|
|
2875
|
-
const variant = refs.resolve(member.$ref, { report: false });
|
|
2876
|
-
if (!variant) return null;
|
|
2877
|
-
const propertyName = discriminator.propertyName;
|
|
2878
|
-
const seen = /* @__PURE__ */ new Set([member.$ref]);
|
|
2879
|
-
/** Whether the variant, or a schema it composes, pins the discriminator property to literal values. */
|
|
2880
|
-
function constrains(v) {
|
|
2881
|
-
const prop = v.properties?.[propertyName];
|
|
2882
|
-
const resolved = prop && isReference(prop) ? refs.resolve(prop.$ref, { report: false }) : prop;
|
|
2883
|
-
const pins = (candidate) => Array.isArray(candidate.enum) || candidate.const !== void 0;
|
|
2884
|
-
const members = (resolved?.anyOf ?? resolved?.oneOf ?? []).filter((member) => member.type !== "null");
|
|
2885
|
-
if (resolved && (pins(resolved) || members.length > 0 && members.every(pins))) return true;
|
|
2886
|
-
const composition = v.allOf ?? v.oneOf ?? v.anyOf;
|
|
2887
|
-
if (!composition) return false;
|
|
2888
|
-
return composition.some((m) => {
|
|
2889
|
-
if (!isReference(m)) return constrains(m);
|
|
2890
|
-
if (seen.has(m.$ref)) return false;
|
|
2891
|
-
seen.add(m.$ref);
|
|
2892
|
-
const r = refs.resolve(m.$ref, { report: false });
|
|
2893
|
-
return r ? constrains(r) : false;
|
|
2894
|
-
});
|
|
2895
|
-
}
|
|
2896
|
-
return constrains(variant) ? null : value;
|
|
2897
|
-
}
|
|
2898
|
-
const defaultRef = getDefaultMappingRef(discriminator);
|
|
2899
|
-
return unionMembers.map((s) => {
|
|
2900
|
-
const ref = isReference(s) ? s.$ref : void 0;
|
|
2901
|
-
const isDefault = ref !== void 0 && ref === defaultRef;
|
|
2902
|
-
const mappedValues = isDefault ? [] : findDiscriminators({
|
|
2903
|
-
mapping: discriminator?.mapping,
|
|
2904
|
-
ref
|
|
2905
|
-
});
|
|
2906
|
-
const implicitValue = mappedValues.length || isDefault ? null : implicitDiscriminantValue(s);
|
|
2907
|
-
const propertyType = propertyTypeOf({
|
|
2908
|
-
schema: s,
|
|
2909
|
-
propertyName: discriminator?.propertyName ?? "",
|
|
2910
|
-
refs
|
|
2911
|
-
}) ?? parentType;
|
|
2912
|
-
const discriminatorValues = typedKeys({
|
|
2913
|
-
keys: mappedValues.length ? mappedValues : implicitValue ? [implicitValue] : [],
|
|
2914
|
-
propertyType
|
|
2915
|
-
});
|
|
2916
|
-
const memberNode = parse({
|
|
2917
|
-
schema: s,
|
|
2918
|
-
name
|
|
2919
|
-
});
|
|
2920
|
-
if (!discriminatorValues.length || !discriminator || !isObjectLike(s, refs)) return memberNode;
|
|
2921
|
-
const narrowedDiscriminatorNode = sharedPropertiesNode ? pickDiscriminatorPropertyNode(ast.macro.apply(sharedPropertiesNode, [macroDiscriminatorEnum({
|
|
2922
|
-
propertyName: discriminator.propertyName,
|
|
2923
|
-
values: discriminatorValues
|
|
2924
|
-
})], { depth: "shallow" }), discriminator.propertyName) : void 0;
|
|
2925
|
-
return ast.factory.createSchema({
|
|
2926
|
-
type: "intersection",
|
|
2927
|
-
members: [memberNode, narrowedDiscriminatorNode ?? createDiscriminantNode({
|
|
2928
|
-
propertyName: discriminator.propertyName,
|
|
2929
|
-
values: discriminatorValues
|
|
2930
|
-
})]
|
|
2931
|
-
});
|
|
2932
|
-
});
|
|
3044
|
+
function getItemSchema({ contentType, mediaType, refs, document }) {
|
|
3045
|
+
if (!mediaType || !isStream(contentType, mediaType)) return null;
|
|
3046
|
+
const raw = mediaType.itemSchema ?? (isEventStream(contentType) && !isOpenApi32(document) ? mediaType.schema : void 0);
|
|
3047
|
+
if (!raw) return null;
|
|
3048
|
+
return isEventStream(contentType) ? toEventSchema(raw, refs) : raw;
|
|
2933
3049
|
}
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
3050
|
+
// src/model/content.ts
|
|
3051
|
+
const PARAMETER_STYLES = /* @__PURE__ */ new Set([
|
|
3052
|
+
"matrix",
|
|
3053
|
+
"label",
|
|
3054
|
+
"form",
|
|
3055
|
+
"simple",
|
|
3056
|
+
"spaceDelimited",
|
|
3057
|
+
"pipeDelimited",
|
|
3058
|
+
"deepObject",
|
|
3059
|
+
"cookie"
|
|
3060
|
+
]);
|
|
3061
|
+
function pickStyleFields({ style, explode, allowReserved }) {
|
|
2943
3062
|
return {
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
const discriminator = deref && getDiscriminator(deref);
|
|
2948
|
-
const parentUnion = deref?.oneOf ?? deref?.anyOf;
|
|
2949
|
-
if (!deref || !discriminator || isDiscriminator(deref) && !parentUnion) return true;
|
|
2950
|
-
const childRef = `${SCHEMA_REF_PREFIX}${name}`;
|
|
2951
|
-
const isDefault = childRef === getDefaultMappingRef(discriminator);
|
|
2952
|
-
const values = isDefault ? [] : findDiscriminators({
|
|
2953
|
-
mapping: discriminator.mapping,
|
|
2954
|
-
ref: childRef
|
|
2955
|
-
});
|
|
2956
|
-
if (values.length) {
|
|
2957
|
-
const { propertyName } = discriminator;
|
|
2958
|
-
const propertyType = propertyTypeOf({
|
|
2959
|
-
schema: deref,
|
|
2960
|
-
propertyName,
|
|
2961
|
-
refs
|
|
2962
|
-
}) ?? propertyTypeOf({
|
|
2963
|
-
schema: { $ref: childRef },
|
|
2964
|
-
propertyName,
|
|
2965
|
-
refs
|
|
2966
|
-
});
|
|
2967
|
-
discriminantValues.push({
|
|
2968
|
-
propertyName,
|
|
2969
|
-
values: typedKeys({
|
|
2970
|
-
keys: values,
|
|
2971
|
-
propertyType
|
|
2972
|
-
})
|
|
2973
|
-
});
|
|
2974
|
-
}
|
|
2975
|
-
return !(parentUnion && (isDefault || values.length > 0 || parentUnion.some((member) => isReference(member) && member.$ref === childRef)));
|
|
2976
|
-
}),
|
|
2977
|
-
discriminantValues
|
|
3063
|
+
...style !== void 0 && PARAMETER_STYLES.has(style) ? { style } : {},
|
|
3064
|
+
...explode !== void 0 ? { explode } : {},
|
|
3065
|
+
...allowReserved !== void 0 ? { allowReserved } : {}
|
|
2978
3066
|
};
|
|
2979
3067
|
}
|
|
2980
|
-
|
|
2981
|
-
const
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
"externalDocs"
|
|
2992
|
-
]);
|
|
2993
|
-
const UNION_KEYS = /* @__PURE__ */ new Set([
|
|
2994
|
-
...ANNOTATIONS,
|
|
2995
|
-
"type",
|
|
2996
|
-
"nullable",
|
|
2997
|
-
"oneOf",
|
|
2998
|
-
"anyOf"
|
|
2999
|
-
]);
|
|
3000
|
-
const MEMBER_KEYS = /* @__PURE__ */ new Set([
|
|
3001
|
-
...ANNOTATIONS,
|
|
3002
|
-
"type",
|
|
3003
|
-
"const"
|
|
3004
|
-
]);
|
|
3005
|
-
function hasOnlyKeys(schema, keys) {
|
|
3006
|
-
return typeof schema === "object" && schema !== null && !Array.isArray(schema) && Object.keys(schema).every((key) => keys.has(key) || key.startsWith("x-"));
|
|
3068
|
+
function getEncodingFields({ encoding, prefixEncoding, itemEncoding }) {
|
|
3069
|
+
const entries = Object.entries(encoding ?? {}).flatMap(([property, entry]) => {
|
|
3070
|
+
const node = toEncodingNode(entry);
|
|
3071
|
+
return Object.keys(node).length ? [[property, node]] : [];
|
|
3072
|
+
});
|
|
3073
|
+
const item = itemEncoding && toEncodingNode(itemEncoding);
|
|
3074
|
+
return {
|
|
3075
|
+
...entries.length && { encoding: Object.fromEntries(entries) },
|
|
3076
|
+
...prefixEncoding?.length && { prefixEncoding: prefixEncoding.map(toEncodingNode) },
|
|
3077
|
+
...item && Object.keys(item).length && { itemEncoding: item }
|
|
3078
|
+
};
|
|
3007
3079
|
}
|
|
3008
|
-
function
|
|
3009
|
-
return
|
|
3080
|
+
function toEncodingNode({ contentType, encoding, prefixEncoding, itemEncoding, ...rest }) {
|
|
3081
|
+
return {
|
|
3082
|
+
...contentType !== void 0 && { contentType },
|
|
3083
|
+
...pickStyleFields(rest),
|
|
3084
|
+
...getEncodingFields({
|
|
3085
|
+
encoding,
|
|
3086
|
+
prefixEncoding,
|
|
3087
|
+
itemEncoding
|
|
3088
|
+
})
|
|
3089
|
+
};
|
|
3010
3090
|
}
|
|
3011
|
-
/**
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
if (!matchesType(schema.type, value) || !matchesType(member.type, value)) return void 0;
|
|
3028
|
-
const valueType = typeof value === "number" ? "number" : "string";
|
|
3029
|
-
if (primitive !== void 0 && primitive !== valueType) return void 0;
|
|
3030
|
-
primitive = valueType;
|
|
3031
|
-
names.add(member.title);
|
|
3032
|
-
values.add(value);
|
|
3033
|
-
namedEnumValues.push({
|
|
3034
|
-
name: member.title,
|
|
3035
|
-
value,
|
|
3036
|
-
primitive: valueType,
|
|
3037
|
-
description: member.description
|
|
3038
|
-
});
|
|
3039
|
-
}
|
|
3040
|
-
return createNode(context, {
|
|
3041
|
-
type: "enum",
|
|
3042
|
-
primitive,
|
|
3043
|
-
namedEnumValues
|
|
3091
|
+
/**
|
|
3092
|
+
* The examples of a media type or a parameter. Named `examples` win over a single `example`.
|
|
3093
|
+
*/
|
|
3094
|
+
function getExamples({ example, examples }, refs) {
|
|
3095
|
+
const named = Object.entries(examples ?? {}).flatMap(([name, entry]) => {
|
|
3096
|
+
const resolved = refs.deref(entry);
|
|
3097
|
+
if (!resolved) return [];
|
|
3098
|
+
const { summary, description, dataValue, value, serializedValue } = resolved;
|
|
3099
|
+
const data = dataValue !== void 0 ? dataValue : value;
|
|
3100
|
+
return [{
|
|
3101
|
+
name,
|
|
3102
|
+
...typeof summary === "string" && { summary },
|
|
3103
|
+
...typeof description === "string" && { description },
|
|
3104
|
+
...data !== void 0 && { value: data },
|
|
3105
|
+
...typeof serializedValue === "string" && { serializedValue }
|
|
3106
|
+
}];
|
|
3044
3107
|
});
|
|
3108
|
+
if (named.length) return { examples: named };
|
|
3109
|
+
return example !== void 0 ? { examples: [{ value: example }] } : {};
|
|
3045
3110
|
}
|
|
3046
|
-
// src/emit/converters/composition.ts
|
|
3047
3111
|
/**
|
|
3048
|
-
*
|
|
3049
|
-
* recursively resolving `$ref` pointers.
|
|
3112
|
+
* Returns the media type entry for `contentType`, resolving a `$ref` into `components.mediaTypes`. `null` when absent.
|
|
3050
3113
|
*/
|
|
3051
|
-
function
|
|
3052
|
-
|
|
3053
|
-
const visited = /* @__PURE__ */ new Set();
|
|
3054
|
-
/** Adds the properties of one member, following its `$ref` and its own `allOf`. */
|
|
3055
|
-
function collectFrom(item) {
|
|
3056
|
-
if (!item) return;
|
|
3057
|
-
if (isReference(item)) {
|
|
3058
|
-
if (visited.has(item.$ref)) return;
|
|
3059
|
-
visited.add(item.$ref);
|
|
3060
|
-
collectFrom(refs.resolve(item.$ref, { report: false }) ?? void 0);
|
|
3061
|
-
return;
|
|
3062
|
-
}
|
|
3063
|
-
if (item.properties) Object.assign(properties, item.properties);
|
|
3064
|
-
if (Array.isArray(item.allOf)) for (const member of item.allOf) collectFrom(member);
|
|
3065
|
-
}
|
|
3066
|
-
for (const item of allOfMembers) collectFrom(item);
|
|
3067
|
-
return properties;
|
|
3114
|
+
function getMediaType({ content, contentType, refs }) {
|
|
3115
|
+
return contentType ? refs.deref(content?.[contentType]) : null;
|
|
3068
3116
|
}
|
|
3069
3117
|
/**
|
|
3070
|
-
*
|
|
3071
|
-
*
|
|
3072
|
-
* (or an empty schema falling back to the configured emptySchemaType).
|
|
3118
|
+
* Returns the schema for one media type of a `content` map, before its `$ref` is resolved, with the binary fallback
|
|
3119
|
+
* for an emptied non-JSON entry. For a stream this is the item schema, so `schema: string` next to `itemSchema` types one item.
|
|
3073
3120
|
*/
|
|
3074
|
-
function
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3121
|
+
function getDeclaredBodySchema({ content, contentType, refs, document }) {
|
|
3122
|
+
const entry = getMediaType({
|
|
3123
|
+
content,
|
|
3124
|
+
contentType,
|
|
3125
|
+
refs
|
|
3126
|
+
});
|
|
3127
|
+
if (!contentType || !entry) return null;
|
|
3128
|
+
const schema = getItemSchema({
|
|
3129
|
+
contentType,
|
|
3130
|
+
mediaType: entry,
|
|
3131
|
+
refs,
|
|
3132
|
+
document
|
|
3133
|
+
}) ?? entry.schema;
|
|
3134
|
+
return (entry.itemSchema ? void 0 : getBinaryFallbackSchema(contentType, schema)) ?? schema ?? null;
|
|
3135
|
+
}
|
|
3136
|
+
function getStreamField({ contentType, mediaType, refs, document, node }) {
|
|
3137
|
+
if (!isStream(contentType, mediaType)) return {};
|
|
3138
|
+
const raw = getItemSchema({
|
|
3139
|
+
contentType,
|
|
3140
|
+
mediaType,
|
|
3141
|
+
refs,
|
|
3142
|
+
document
|
|
3143
|
+
});
|
|
3144
|
+
return { stream: {
|
|
3145
|
+
...raw ? { schema: node } : {},
|
|
3146
|
+
...raw && isEventStream(contentType) ? { shape: getItemShape(raw, refs) } : {}
|
|
3147
|
+
} };
|
|
3148
|
+
}
|
|
3149
|
+
function schemaKeyword(mediaType) {
|
|
3150
|
+
return mediaType?.itemSchema ? "itemSchema" : "schema";
|
|
3151
|
+
}
|
|
3152
|
+
function mediaTypePointer(content, contentType, fallback) {
|
|
3153
|
+
const entry = content?.[contentType];
|
|
3154
|
+
return isReference(entry) ? entry.$ref : fallback;
|
|
3155
|
+
}
|
|
3156
|
+
function collectPropertyKeysByFlag({ schema, flag, refs }) {
|
|
3157
|
+
if (!schema) return null;
|
|
3158
|
+
const keys = Object.entries(collectSiblingProperties([schema], refs)).flatMap(([key, prop]) => hasFlag({
|
|
3159
|
+
prop,
|
|
3160
|
+
flag,
|
|
3161
|
+
refs
|
|
3162
|
+
}) ? [key] : []);
|
|
3163
|
+
return keys.length ? keys : null;
|
|
3164
|
+
}
|
|
3165
|
+
function hasFlag({ prop, flag, refs }) {
|
|
3166
|
+
if (!prop || typeof prop !== "object") return false;
|
|
3167
|
+
if (prop[flag]) return true;
|
|
3168
|
+
return !!(isReference(prop) ? refs.resolve(prop.$ref, { report: false }) : null)?.[flag];
|
|
3169
|
+
}
|
|
3170
|
+
function parseContentEntry({ content, contentType, contentPointer, omit, refs, document, parse }) {
|
|
3171
|
+
const declared = getDeclaredBodySchema({
|
|
3172
|
+
content,
|
|
3173
|
+
contentType,
|
|
3174
|
+
refs,
|
|
3175
|
+
document
|
|
3176
|
+
});
|
|
3177
|
+
const schema = declared ? refs.derefKeepingRef(declared) : null;
|
|
3178
|
+
const node = parse(schema);
|
|
3179
|
+
if (!node) return null;
|
|
3180
|
+
const mediaType = getMediaType({
|
|
3181
|
+
content,
|
|
3182
|
+
contentType,
|
|
3183
|
+
refs
|
|
3184
|
+
});
|
|
3185
|
+
if (schema && contentType) scanSchema(node, `${mediaTypePointer(content, contentType, `${contentPointer}/${escapePointerToken(contentType)}`)}/${schemaKeyword(mediaType)}`);
|
|
3186
|
+
return {
|
|
3187
|
+
schema: node,
|
|
3188
|
+
keysToOmit: collectPropertyKeysByFlag({
|
|
3189
|
+
schema: declared,
|
|
3190
|
+
flag: omit,
|
|
3191
|
+
refs
|
|
3192
|
+
}),
|
|
3193
|
+
...mediaType && getEncodingFields(mediaType),
|
|
3194
|
+
...mediaType && getExamples(mediaType, refs),
|
|
3195
|
+
...contentType ? getStreamField({
|
|
3196
|
+
contentType,
|
|
3197
|
+
mediaType,
|
|
3198
|
+
refs,
|
|
3199
|
+
document,
|
|
3200
|
+
node
|
|
3201
|
+
}) : {},
|
|
3202
|
+
extensions: pickExtensions(mediaType)
|
|
3203
|
+
};
|
|
3204
|
+
}
|
|
3205
|
+
// src/model/parameters.ts
|
|
3206
|
+
function withEnumName({ schema, param, parentName, options }) {
|
|
3207
|
+
if (!parentName || !param.name) return schema;
|
|
3208
|
+
return ast.macro.apply(schema, [macroEnumName({
|
|
3209
|
+
parentName,
|
|
3210
|
+
propName: param.name,
|
|
3211
|
+
enumSuffix: options.enumSuffix
|
|
3212
|
+
})], { depth: "shallow" });
|
|
3213
|
+
}
|
|
3214
|
+
function parseParameterSchema(ctx) {
|
|
3215
|
+
const { param, pointer, parentName, options, parseSchema, contentType, mediaType } = ctx;
|
|
3216
|
+
const rawSchema = param.schema ?? mediaType?.schema;
|
|
3217
|
+
const schemaName = parentName && param.name ? pascalCase(`${parentName} ${param.name}`) : void 0;
|
|
3218
|
+
const schema = withEnumName({
|
|
3219
|
+
schema: rawSchema ? parseSchema({
|
|
3220
|
+
schema: rawSchema,
|
|
3221
|
+
name: schemaName
|
|
3222
|
+
}) : ast.factory.createSchema({ type: options.unknownType }),
|
|
3223
|
+
param,
|
|
3224
|
+
parentName,
|
|
3225
|
+
options
|
|
3097
3226
|
});
|
|
3227
|
+
if (rawSchema) scanSchema(schema, contentType ? `${pointer}/content/${escapePointerToken(contentType)}/schema` : `${pointer}/schema`);
|
|
3228
|
+
return schema;
|
|
3098
3229
|
}
|
|
3099
3230
|
/**
|
|
3100
|
-
*
|
|
3231
|
+
* Converts one parameter into a `ParameterNode`, naming an inline schema after the operation and the parameter.
|
|
3101
3232
|
*/
|
|
3102
|
-
function
|
|
3103
|
-
const
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3233
|
+
function parseParameter(ctx) {
|
|
3234
|
+
const { param, pointer, refs } = ctx;
|
|
3235
|
+
const contentType = param.schema ? void 0 : Object.keys(param.content ?? {})[0];
|
|
3236
|
+
const mediaType = getMediaType({
|
|
3237
|
+
content: param.content,
|
|
3238
|
+
contentType,
|
|
3239
|
+
refs
|
|
3240
|
+
});
|
|
3241
|
+
const schema = parseParameterSchema({
|
|
3242
|
+
...ctx,
|
|
3243
|
+
contentType,
|
|
3244
|
+
mediaType
|
|
3245
|
+
});
|
|
3246
|
+
return ast.factory.createParameter({
|
|
3247
|
+
pointer,
|
|
3248
|
+
name: param.name,
|
|
3249
|
+
in: param.in,
|
|
3250
|
+
schema: {
|
|
3251
|
+
...schema,
|
|
3252
|
+
description: param.description ?? schema.description
|
|
3253
|
+
},
|
|
3254
|
+
required: param.required ?? false,
|
|
3255
|
+
...pickStyleFields(param),
|
|
3256
|
+
...contentType !== void 0 ? { contentType } : {},
|
|
3257
|
+
...mediaType?.encoding && { encoding: getEncodingFields({ encoding: mediaType.encoding }).encoding },
|
|
3258
|
+
...getExamples(mediaType ?? param, refs),
|
|
3259
|
+
...param.deprecated && { deprecated: true },
|
|
3260
|
+
...param.allowEmptyValue && { allowEmptyValue: true },
|
|
3261
|
+
extensions: pickExtensions(param)
|
|
3262
|
+
});
|
|
3107
3263
|
}
|
|
3108
3264
|
/**
|
|
3109
|
-
*
|
|
3110
|
-
*
|
|
3111
|
-
* The resolved schema is stored in `node.schema`. Usage-site sibling fields
|
|
3112
|
-
* (description, readOnly, nullable, etc.) are stored directly on the ref node.
|
|
3113
|
-
* Use `ast.ref.mergeSchema(node)` in printers to get a merged view of both.
|
|
3114
|
-
* Circular refs are detected in `refs.resolveNode` and leave `schema` as `null`.
|
|
3265
|
+
* Spreads a form-encoded OpenAPI 3.2 `querystring` object into one query parameter per property. Another content type stays whole.
|
|
3115
3266
|
*/
|
|
3116
|
-
function
|
|
3117
|
-
|
|
3118
|
-
const
|
|
3119
|
-
const
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
name: name ?? resolvedSchema.name,
|
|
3135
|
-
nullable: nullable ?? resolvedSchema.nullable,
|
|
3136
|
-
extensions: pickExtensions({
|
|
3137
|
-
...resolvedSchema.extensions,
|
|
3138
|
-
...schema
|
|
3139
|
-
})
|
|
3140
|
-
});
|
|
3141
|
-
const targetName = renames?.get(schema.$ref);
|
|
3142
|
-
return createNode(ctx, {
|
|
3143
|
-
type: ast.schema.types.ref,
|
|
3144
|
-
name: ast.ref.getName(schema.$ref),
|
|
3145
|
-
ref: schema.$ref,
|
|
3146
|
-
...targetName ? { targetName } : {},
|
|
3147
|
-
schema: resolvedSchema
|
|
3267
|
+
function spreadQuerystring(param) {
|
|
3268
|
+
if (param.in !== "querystring") return [param];
|
|
3269
|
+
const schema = ast.ref.getSchema(param.schema);
|
|
3270
|
+
const formEncoded = !param.contentType || ast.mediaType.getBase(param.contentType) === "application/x-www-form-urlencoded";
|
|
3271
|
+
if (schema.type !== "object" || !formEncoded) return [param];
|
|
3272
|
+
return schema.properties.map((property) => {
|
|
3273
|
+
const { style, explode, allowReserved } = param.encoding?.[property.name] ?? {};
|
|
3274
|
+
return ast.factory.createParameter({
|
|
3275
|
+
...param.pointer ? { pointer: param.pointer } : {},
|
|
3276
|
+
name: property.name,
|
|
3277
|
+
in: "query",
|
|
3278
|
+
schema: property.schema,
|
|
3279
|
+
required: param.required && property.required,
|
|
3280
|
+
...style !== void 0 && { style },
|
|
3281
|
+
...explode !== void 0 && { explode },
|
|
3282
|
+
...allowReserved !== void 0 && { allowReserved },
|
|
3283
|
+
...param.extensions && { extensions: param.extensions }
|
|
3284
|
+
});
|
|
3148
3285
|
});
|
|
3149
3286
|
}
|
|
3150
|
-
|
|
3151
|
-
function
|
|
3152
|
-
|
|
3153
|
-
const [subschema] = resolveUnionMembers([not], rest.properties ?? {});
|
|
3154
|
-
const notNode = ast.factory.createSchema({
|
|
3155
|
-
type: ast.schema.types.not,
|
|
3156
|
-
members: [parse({ schema: subschema })]
|
|
3157
|
-
});
|
|
3158
|
-
return createNode({
|
|
3159
|
-
schema,
|
|
3160
|
-
name,
|
|
3161
|
-
nullable,
|
|
3162
|
-
defaultValue
|
|
3163
|
-
}, {
|
|
3164
|
-
type: ast.schema.types.intersection,
|
|
3165
|
-
members: [parse({
|
|
3166
|
-
schema: rest,
|
|
3167
|
-
name
|
|
3168
|
-
}), notNode]
|
|
3169
|
-
});
|
|
3287
|
+
// src/operation.ts
|
|
3288
|
+
function slugify(value) {
|
|
3289
|
+
return value.replace(/[^a-zA-Z0-9]/g, "-").replace(/-{2,}/g, "-").replace(/^-|-$/g, "");
|
|
3170
3290
|
}
|
|
3171
3291
|
/**
|
|
3172
|
-
*
|
|
3292
|
+
* Returns the operation's JSON pointer, under `additionalOperations` for an OpenAPI 3.2 method outside the fixed fields.
|
|
3173
3293
|
*/
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
"examples",
|
|
3179
|
-
"default",
|
|
3180
|
-
"deprecated",
|
|
3181
|
-
"readOnly",
|
|
3182
|
-
"writeOnly",
|
|
3183
|
-
"if",
|
|
3184
|
-
"then",
|
|
3185
|
-
"else"
|
|
3186
|
-
]);
|
|
3294
|
+
function getOperationPointer({ path, method }) {
|
|
3295
|
+
const pathPointer = `#/paths/${escapePointerToken(path)}`;
|
|
3296
|
+
return SUPPORTED_METHODS.has(method.toLowerCase()) ? `${pathPointer}/${method.toLowerCase()}` : `${pathPointer}/additionalOperations/${escapePointerToken(method)}`;
|
|
3297
|
+
}
|
|
3187
3298
|
/**
|
|
3188
|
-
*
|
|
3189
|
-
* not become `unknown`. The parent's own values win. The parser reads their `if`/`then`/`else` into `conditionals`.
|
|
3299
|
+
* Returns the operation's `operationId`, falling back to `<method>_<slugified-path>` when absent.
|
|
3190
3300
|
*/
|
|
3191
|
-
function
|
|
3192
|
-
|
|
3301
|
+
function getOperationId({ path, method, schema }) {
|
|
3302
|
+
const { operationId } = schema;
|
|
3303
|
+
if (typeof operationId === "string" && operationId.length > 0) return operationId;
|
|
3304
|
+
return `${method}_${slugify(path).toLowerCase()}`;
|
|
3193
3305
|
}
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
allOf: rest
|
|
3202
|
-
} : schema;
|
|
3306
|
+
/**
|
|
3307
|
+
* Returns the declared response status codes, skipping `x-` extensions and non-object entries.
|
|
3308
|
+
*/
|
|
3309
|
+
function getResponseStatusCodes({ schema }) {
|
|
3310
|
+
const responses = schema.responses;
|
|
3311
|
+
if (!responses || isReference(responses)) return [];
|
|
3312
|
+
return Object.keys(responses).filter((key) => !key.startsWith("x-") && !!responses[key] && typeof responses[key] === "object");
|
|
3203
3313
|
}
|
|
3204
3314
|
/**
|
|
3205
|
-
*
|
|
3206
|
-
* beside several patterns when the parent holds more than its type and annotations, such as `minLength`.
|
|
3315
|
+
* Returns the response object for a status code, resolving a `$ref` through `refs`. `null` when absent.
|
|
3207
3316
|
*/
|
|
3208
|
-
function
|
|
3209
|
-
|
|
3210
|
-
if (!
|
|
3211
|
-
return
|
|
3317
|
+
function getResponseByStatusCode({ operation, refs, statusCode }) {
|
|
3318
|
+
const responses = operation.schema.responses;
|
|
3319
|
+
if (!responses || isReference(responses)) return null;
|
|
3320
|
+
return refs.deref(responses[statusCode]);
|
|
3212
3321
|
}
|
|
3213
3322
|
/**
|
|
3214
|
-
*
|
|
3323
|
+
* Resolves the operation's request body, dereferencing a `$ref` through `refs`. Returns `null`
|
|
3324
|
+
* when the operation has no request body or it cannot be resolved.
|
|
3215
3325
|
*/
|
|
3216
|
-
function
|
|
3217
|
-
return
|
|
3326
|
+
function getRequestBody({ operation, refs }) {
|
|
3327
|
+
return refs.deref(operation.schema.requestBody);
|
|
3328
|
+
}
|
|
3329
|
+
/** Merges path-level and operation-level parameters; operation-level wins per `in:name`. */
|
|
3330
|
+
function getParameters({ operation, refs }) {
|
|
3331
|
+
const resolveParams = (params, pointer) => params.flatMap((entry, index) => {
|
|
3332
|
+
const param = refs.derefKeepingRef(entry);
|
|
3333
|
+
return param && typeof param === "object" && "in" in param && "name" in param ? [{
|
|
3334
|
+
param,
|
|
3335
|
+
pointer: `${pointer}/parameters/${index}`
|
|
3336
|
+
}] : [];
|
|
3337
|
+
});
|
|
3338
|
+
const pathPointer = `#/paths/${escapePointerToken(operation.path)}`;
|
|
3339
|
+
const operationParams = resolveParams(operation.schema?.parameters || [], getOperationPointer(operation));
|
|
3340
|
+
const pathLevelParams = resolveParams(operation.pathItem.parameters ?? [], pathPointer);
|
|
3341
|
+
const paramMap = /* @__PURE__ */ new Map();
|
|
3342
|
+
for (const entry of [...pathLevelParams, ...operationParams]) if (entry.param.name && entry.param.in) paramMap.set(`${entry.param.in}:${entry.param.name}`, entry);
|
|
3343
|
+
return Array.from(paramMap.values());
|
|
3344
|
+
}
|
|
3345
|
+
function getLinks(links, refs) {
|
|
3346
|
+
const entries = Object.entries(links ?? {}).flatMap(([name, value]) => {
|
|
3347
|
+
const link = refs.deref(value);
|
|
3348
|
+
if (!link) return [];
|
|
3349
|
+
const server = link.server ? toInputServers([link.server])[0] : void 0;
|
|
3350
|
+
return [[name, {
|
|
3351
|
+
...pickStrings(link, [
|
|
3352
|
+
"operationId",
|
|
3353
|
+
"operationRef",
|
|
3354
|
+
"description"
|
|
3355
|
+
]),
|
|
3356
|
+
...link.parameters && { parameters: link.parameters },
|
|
3357
|
+
..."requestBody" in link && { requestBody: link.requestBody },
|
|
3358
|
+
...server && { server }
|
|
3359
|
+
}]];
|
|
3360
|
+
});
|
|
3361
|
+
return entries.length ? Object.fromEntries(entries) : void 0;
|
|
3218
3362
|
}
|
|
3219
3363
|
/**
|
|
3220
|
-
*
|
|
3364
|
+
* Builds an `Operation` for every supported HTTP method and every OpenAPI 3.2 `additionalOperations` entry on
|
|
3365
|
+
* every path, in document order. `x-` path keys and unresolvable path-item `$ref`s are skipped.
|
|
3221
3366
|
*/
|
|
3222
|
-
function
|
|
3223
|
-
const
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
const
|
|
3231
|
-
const
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
...memberNode.extensions,
|
|
3248
|
-
...schema
|
|
3249
|
-
}),
|
|
3250
|
-
externalDocs: getExternalDocs(schema.externalDocs) ?? memberNode.externalDocs
|
|
3367
|
+
function getOperations(document, refs) {
|
|
3368
|
+
const operations = [];
|
|
3369
|
+
const paths = document.paths;
|
|
3370
|
+
if (!paths) return operations;
|
|
3371
|
+
for (const path of Object.keys(paths)) {
|
|
3372
|
+
if (path.startsWith("x-")) continue;
|
|
3373
|
+
const pathItem = refs.deref(paths[path]);
|
|
3374
|
+
if (!pathItem) continue;
|
|
3375
|
+
const item = pathItem;
|
|
3376
|
+
for (const method of Object.keys(item)) {
|
|
3377
|
+
if (!SUPPORTED_METHODS.has(method)) continue;
|
|
3378
|
+
const schema = item[method];
|
|
3379
|
+
if (!schema || typeof schema !== "object") continue;
|
|
3380
|
+
operations.push({
|
|
3381
|
+
path,
|
|
3382
|
+
method,
|
|
3383
|
+
schema,
|
|
3384
|
+
pathItem
|
|
3385
|
+
});
|
|
3386
|
+
}
|
|
3387
|
+
for (const [method, schema] of Object.entries(pathItem.additionalOperations ?? {})) if (schema && typeof schema === "object" && !SUPPORTED_METHODS.has(method.toLowerCase())) operations.push({
|
|
3388
|
+
path,
|
|
3389
|
+
method,
|
|
3390
|
+
schema,
|
|
3391
|
+
pathItem
|
|
3251
3392
|
});
|
|
3252
3393
|
}
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
const
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
const memberSchema = {
|
|
3285
|
-
properties: { [key]: prop },
|
|
3286
|
-
required: [key]
|
|
3287
|
-
};
|
|
3288
|
-
allOfMembers.push(parse({
|
|
3289
|
-
schema: memberSchema,
|
|
3290
|
-
name
|
|
3291
|
-
}));
|
|
3292
|
-
break;
|
|
3293
|
-
}
|
|
3394
|
+
return withUniqueOperationIds(operations);
|
|
3395
|
+
}
|
|
3396
|
+
function suffixed(id, taken, counts) {
|
|
3397
|
+
const count = (counts.get(id) ?? 1) + 1;
|
|
3398
|
+
counts.set(id, count);
|
|
3399
|
+
const candidate = `${id}_${count}`;
|
|
3400
|
+
return taken.has(pascalCase(candidate)) ? suffixed(id, taken, counts) : candidate;
|
|
3401
|
+
}
|
|
3402
|
+
function withUniqueOperationIds(operations) {
|
|
3403
|
+
const taken = new Set(operations.flatMap(({ schema }) => typeof schema.operationId === "string" && schema.operationId ? [pascalCase(schema.operationId)] : []));
|
|
3404
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
3405
|
+
const counts = /* @__PURE__ */ new Map();
|
|
3406
|
+
return operations.map((operation) => {
|
|
3407
|
+
const operationId = getOperationId(operation);
|
|
3408
|
+
const name = pascalCase(operationId);
|
|
3409
|
+
if (!claimed.has(name)) {
|
|
3410
|
+
claimed.add(name);
|
|
3411
|
+
taken.add(name);
|
|
3412
|
+
return operation;
|
|
3413
|
+
}
|
|
3414
|
+
const unique = suffixed(operationId, taken, counts);
|
|
3415
|
+
taken.add(pascalCase(unique));
|
|
3416
|
+
claimed.add(pascalCase(unique));
|
|
3417
|
+
Diagnostics.report({
|
|
3418
|
+
code: Diagnostics.code.duplicateOperationId,
|
|
3419
|
+
severity: "warning",
|
|
3420
|
+
message: `operationId "${operationId}" of ${operation.method.toUpperCase()} ${operation.path} is already used, so it is renamed to "${unique}".`,
|
|
3421
|
+
help: "Give each operation its own `operationId`.",
|
|
3422
|
+
location: {
|
|
3423
|
+
kind: "operation",
|
|
3424
|
+
pointer: getOperationPointer(operation)
|
|
3294
3425
|
}
|
|
3295
|
-
}
|
|
3296
|
-
}
|
|
3297
|
-
if (keepsOwnKeywords(schema)) {
|
|
3298
|
-
const { allOf: _allOf, ...schemaWithoutAllOf } = schema;
|
|
3299
|
-
allOfMembers.push(parse({ schema: schemaWithoutAllOf }));
|
|
3300
|
-
}
|
|
3301
|
-
for (const { propertyName, values } of discriminantValues) allOfMembers.push(createDiscriminantNode({
|
|
3302
|
-
propertyName,
|
|
3303
|
-
values
|
|
3304
|
-
}));
|
|
3305
|
-
return createNode({
|
|
3306
|
-
schema,
|
|
3307
|
-
name,
|
|
3308
|
-
nullable,
|
|
3309
|
-
defaultValue
|
|
3310
|
-
}, {
|
|
3311
|
-
type: ast.schema.types.intersection,
|
|
3312
|
-
members: [...mergeAdjacentObjectsLazy(allOfMembers.slice(0, syntheticStart)), ...mergeAdjacentObjectsLazy(allOfMembers.slice(syntheticStart))]
|
|
3313
|
-
});
|
|
3314
|
-
}
|
|
3315
|
-
const scalarTypes = /* @__PURE__ */ new Set([
|
|
3316
|
-
"string",
|
|
3317
|
-
"number",
|
|
3318
|
-
"integer",
|
|
3319
|
-
"boolean"
|
|
3320
|
-
]);
|
|
3321
|
-
/**
|
|
3322
|
-
* Copies a scalar parent `type` and `format` onto inline members without a `type`. JSON Schema applies
|
|
3323
|
-
* sibling keywords to every member, so `type: 'integer', anyOf: [{ minimum: 10 }]` keeps each member an integer.
|
|
3324
|
-
*/
|
|
3325
|
-
function inheritScalarType(members, { schema, type }) {
|
|
3326
|
-
if (!type || !scalarTypes.has(type)) return members;
|
|
3327
|
-
if (Array.isArray(schema.type) && schema.type.filter((t) => t !== "null").length > 1) return members;
|
|
3328
|
-
return members.map((member) => {
|
|
3329
|
-
if (!member || typeof member !== "object" || isReference(member) || member.type !== void 0) return member;
|
|
3330
|
-
const obj = member;
|
|
3426
|
+
});
|
|
3331
3427
|
return {
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3428
|
+
...operation,
|
|
3429
|
+
schema: {
|
|
3430
|
+
...operation.schema,
|
|
3431
|
+
operationId: unique
|
|
3432
|
+
}
|
|
3335
3433
|
};
|
|
3336
3434
|
});
|
|
3337
3435
|
}
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
const
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3436
|
+
function parseRequestBody({ operation, operationName, pointer, ctx }) {
|
|
3437
|
+
const { document, refs, contentType, parseSchema } = ctx;
|
|
3438
|
+
const body = getRequestBody({
|
|
3439
|
+
operation,
|
|
3440
|
+
refs
|
|
3441
|
+
});
|
|
3442
|
+
if (!body) return void 0;
|
|
3443
|
+
const bodyRequired = body.required === true;
|
|
3444
|
+
const requestBodyName = operationName ? `${operationName}Request` : void 0;
|
|
3445
|
+
const content = (contentType ? [contentType] : Object.keys(body.content ?? {})).flatMap((ct) => {
|
|
3446
|
+
const entry = parseContentEntry({
|
|
3447
|
+
content: body.content,
|
|
3448
|
+
contentType: ct,
|
|
3449
|
+
contentPointer: `${pointer}/requestBody/content`,
|
|
3450
|
+
omit: "readOnly",
|
|
3451
|
+
refs,
|
|
3452
|
+
document,
|
|
3453
|
+
parse: (schema) => schema ? ast.schema.applyOptionality(parseSchema({
|
|
3454
|
+
schema,
|
|
3455
|
+
name: requestBodyName
|
|
3456
|
+
}), bodyRequired) : null
|
|
3359
3457
|
});
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3458
|
+
return entry ? [ast.factory.createContent({
|
|
3459
|
+
contentType: ct,
|
|
3460
|
+
...entry
|
|
3461
|
+
})] : [];
|
|
3462
|
+
});
|
|
3463
|
+
if (content.length === 0 && !body.description) return void 0;
|
|
3464
|
+
return {
|
|
3465
|
+
pointer: `${pointer}/requestBody`,
|
|
3466
|
+
description: body.description,
|
|
3467
|
+
required: bodyRequired || void 0,
|
|
3468
|
+
content: content.length > 0 ? content : void 0,
|
|
3469
|
+
extensions: pickExtensions(body)
|
|
3369
3470
|
};
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
const
|
|
3373
|
-
|
|
3374
|
-
const
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
name
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
}),
|
|
3392
|
-
parse,
|
|
3393
|
-
name,
|
|
3471
|
+
}
|
|
3472
|
+
function pickDoc({ operation, key }) {
|
|
3473
|
+
const own = operation.schema[key];
|
|
3474
|
+
if (typeof own === "string") return own;
|
|
3475
|
+
const fallback = operation.pathItem[key];
|
|
3476
|
+
return typeof fallback === "string" ? fallback : void 0;
|
|
3477
|
+
}
|
|
3478
|
+
function parseResponseHeaders({ response, responsePointer, responseName, ctx: { options, parseSchema, refs } }) {
|
|
3479
|
+
return Object.entries(response?.headers ?? {}).flatMap(([name, value]) => {
|
|
3480
|
+
const header = name.toLowerCase() === "content-type" ? null : refs.deref(value);
|
|
3481
|
+
if (!header) return [];
|
|
3482
|
+
return [parseParameter({
|
|
3483
|
+
param: {
|
|
3484
|
+
...header,
|
|
3485
|
+
name,
|
|
3486
|
+
in: "header"
|
|
3487
|
+
},
|
|
3488
|
+
pointer: `${responsePointer}/headers/${escapePointerToken(name)}`,
|
|
3489
|
+
parentName: responseName,
|
|
3490
|
+
options,
|
|
3491
|
+
parseSchema,
|
|
3394
3492
|
refs
|
|
3395
|
-
});
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3493
|
+
})];
|
|
3494
|
+
});
|
|
3495
|
+
}
|
|
3496
|
+
function parseResponse({ operation, key, operationName, pointer, ctx }) {
|
|
3497
|
+
const { document, refs, contentType, options, parseSchema } = ctx;
|
|
3498
|
+
const response = getResponseByStatusCode({
|
|
3499
|
+
operation,
|
|
3500
|
+
refs,
|
|
3501
|
+
statusCode: key
|
|
3502
|
+
});
|
|
3503
|
+
const { content, summary, description, links: rawLinks } = response ?? {};
|
|
3504
|
+
const statusCode = key.toLowerCase();
|
|
3505
|
+
const responseName = operationName ? `${operationName}Status${statusCode}` : void 0;
|
|
3506
|
+
const responsePointer = `${pointer}/responses/${key}`;
|
|
3507
|
+
const parseEntrySchema = (ct) => parseContentEntry({
|
|
3508
|
+
content,
|
|
3509
|
+
contentType: ct,
|
|
3510
|
+
contentPointer: `${responsePointer}/content`,
|
|
3511
|
+
omit: "writeOnly",
|
|
3512
|
+
refs,
|
|
3513
|
+
document,
|
|
3514
|
+
parse: (schema) => schema && Object.keys(schema).length > 0 ? parseSchema({
|
|
3515
|
+
schema,
|
|
3516
|
+
name: responseName
|
|
3517
|
+
}) : ast.factory.createSchema({ type: options.emptySchemaType })
|
|
3518
|
+
});
|
|
3519
|
+
const declaredContentTypes = Object.keys(content ?? {});
|
|
3520
|
+
const responseContent = (contentType && declaredContentTypes.length ? [contentType] : declaredContentTypes).map((ct) => ast.factory.createContent({
|
|
3521
|
+
contentType: ct,
|
|
3522
|
+
...parseEntrySchema(ct)
|
|
3421
3523
|
}));
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
|
|
3426
|
-
|
|
3524
|
+
if (responseContent.length === 0) responseContent.push(ast.factory.createContent({
|
|
3525
|
+
contentType: "",
|
|
3526
|
+
...parseEntrySchema(contentType)
|
|
3527
|
+
}));
|
|
3528
|
+
const headers = parseResponseHeaders({
|
|
3529
|
+
response,
|
|
3530
|
+
responsePointer,
|
|
3531
|
+
responseName,
|
|
3532
|
+
ctx
|
|
3533
|
+
});
|
|
3534
|
+
const links = getLinks(rawLinks, refs);
|
|
3535
|
+
return ast.factory.createResponse({
|
|
3536
|
+
pointer: responsePointer,
|
|
3537
|
+
statusCode,
|
|
3538
|
+
summary,
|
|
3539
|
+
description,
|
|
3540
|
+
content: responseContent,
|
|
3541
|
+
...headers.length && { headers },
|
|
3542
|
+
...links && { links },
|
|
3543
|
+
extensions: pickExtensions(response)
|
|
3427
3544
|
});
|
|
3428
|
-
return ast.macro.apply(unionNode, [macroDedupeUnion], { depth: "shallow" });
|
|
3429
3545
|
}
|
|
3430
3546
|
/**
|
|
3431
|
-
* Converts an OAS
|
|
3432
|
-
* Only called once the multi-type rule's `match` has confirmed more than one non-`null` type
|
|
3433
|
-
* remains; a single remaining type (e.g. `['string', 'null']`) is handled as that type instead,
|
|
3434
|
-
* with nullability already folded in.
|
|
3547
|
+
* Converts an OAS `Operation` into an `OperationNode`.
|
|
3435
3548
|
*/
|
|
3436
|
-
function
|
|
3437
|
-
const
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3549
|
+
function parseOperation({ operation, ...ctx }) {
|
|
3550
|
+
const { document, refs, options, parseSchema } = ctx;
|
|
3551
|
+
const operationId = getOperationId(operation);
|
|
3552
|
+
const operationName = operationId ? pascalCase(operationId) : void 0;
|
|
3553
|
+
const pointer = getOperationPointer(operation);
|
|
3554
|
+
const parameters = getParameters({
|
|
3555
|
+
operation,
|
|
3556
|
+
refs
|
|
3557
|
+
}).map((entry) => parseParameter({
|
|
3558
|
+
options,
|
|
3559
|
+
parseSchema,
|
|
3560
|
+
refs,
|
|
3561
|
+
...entry,
|
|
3562
|
+
parentName: operationName
|
|
3563
|
+
})).flatMap(spreadQuerystring);
|
|
3564
|
+
const requestBody = parseRequestBody({
|
|
3565
|
+
operation,
|
|
3566
|
+
operationName,
|
|
3567
|
+
pointer,
|
|
3568
|
+
ctx
|
|
3569
|
+
});
|
|
3570
|
+
const responses = getResponseStatusCodes(operation).map((key) => parseResponse({
|
|
3571
|
+
operation,
|
|
3572
|
+
key,
|
|
3573
|
+
operationName,
|
|
3574
|
+
pointer,
|
|
3575
|
+
ctx
|
|
3576
|
+
}));
|
|
3577
|
+
const servers = operation.schema.servers?.length ? operation.schema.servers : operation.pathItem.servers;
|
|
3578
|
+
const auth = toAuthRequirements(operation.schema.security ?? document.security, document);
|
|
3579
|
+
return ast.factory.createOperation({
|
|
3580
|
+
pointer,
|
|
3581
|
+
operationId,
|
|
3582
|
+
protocol: "http",
|
|
3583
|
+
method: SUPPORTED_METHODS.has(operation.method) ? operation.method.toUpperCase() : operation.method,
|
|
3584
|
+
path: operation.path,
|
|
3585
|
+
tags: Array.isArray(operation.schema.tags) ? operation.schema.tags.map(String) : [],
|
|
3586
|
+
summary: pickDoc({
|
|
3587
|
+
operation,
|
|
3588
|
+
key: "summary"
|
|
3589
|
+
}) || void 0,
|
|
3590
|
+
description: pickDoc({
|
|
3591
|
+
operation,
|
|
3592
|
+
key: "description"
|
|
3593
|
+
}) || void 0,
|
|
3594
|
+
deprecated: operation.schema.deprecated || void 0,
|
|
3595
|
+
externalDocs: getExternalDocs(operation.schema.externalDocs),
|
|
3596
|
+
parameters,
|
|
3597
|
+
requestBody,
|
|
3598
|
+
responses,
|
|
3599
|
+
extensions: pickExtensions(operation.schema),
|
|
3600
|
+
...servers && { servers: toInputServers(servers) },
|
|
3601
|
+
...auth && { auth }
|
|
3454
3602
|
});
|
|
3455
3603
|
}
|
|
3604
|
+
/** Renames path parameters and their `{placeholder}` to the camelCase names from {@link Url.toParamNames}. */
|
|
3605
|
+
function camelCasePathParams(node) {
|
|
3606
|
+
if (!node.path || node.parameters.some((param) => param.in === "path" && param.style === "matrix")) return node;
|
|
3607
|
+
const names = Url.toParamNames(node.path);
|
|
3608
|
+
return {
|
|
3609
|
+
...node,
|
|
3610
|
+
path: node.path.replace(/\{([^}]+)\}/g, (_match, name) => `{${names.get(name) ?? name}}`),
|
|
3611
|
+
parameters: node.parameters.map((param) => param.in === "path" && names.has(param.name) ? {
|
|
3612
|
+
...param,
|
|
3613
|
+
name: names.get(param.name)
|
|
3614
|
+
} : param)
|
|
3615
|
+
};
|
|
3616
|
+
}
|
|
3456
3617
|
// src/emit/converters/scalar.ts
|
|
3457
3618
|
/**
|
|
3458
3619
|
* Normalizes malformed `{ type: 'array', enum: [...] }` schemas by moving enum values into items.
|
|
@@ -3580,7 +3741,7 @@ function convertFormat(context) {
|
|
|
3580
3741
|
representation: value === "date" ? "date" : "string"
|
|
3581
3742
|
});
|
|
3582
3743
|
}
|
|
3583
|
-
const specialType =
|
|
3744
|
+
const specialType = getFormatType(schema.format);
|
|
3584
3745
|
if (specialType === "blob") return convertBlob(context);
|
|
3585
3746
|
if (specialType === "string") {
|
|
3586
3747
|
if (type === "integer" || type === "number") return convertNumeric(context, type);
|
|
@@ -3901,7 +4062,7 @@ function formatPrintsPlainType({ schema, options, type }) {
|
|
|
3901
4062
|
const format = schema.format;
|
|
3902
4063
|
if (format === "date-time" || format === "date" || format === "time") return resolveDateTypeValue(options.dateType, format) !== "date";
|
|
3903
4064
|
if ((format === "int64" || format === "uint64") && type !== "string") return options.integerType !== "bigint";
|
|
3904
|
-
return
|
|
4065
|
+
return getFormatType(format) !== "blob";
|
|
3905
4066
|
}
|
|
3906
4067
|
/**
|
|
3907
4068
|
* Ordered schema rule table. Order is significant: composition keywords (`not`, `$ref`, `allOf`,
|
|
@@ -4237,9 +4398,15 @@ var Adapter = class {
|
|
|
4237
4398
|
};
|
|
4238
4399
|
this.#parserOptions = parserOptions;
|
|
4239
4400
|
}
|
|
4401
|
+
/**
|
|
4402
|
+
* @deprecated Read `document` from the result of `parse`. It follows the last parse. Removed in Kubb v6.
|
|
4403
|
+
*/
|
|
4240
4404
|
get document() {
|
|
4241
4405
|
return this.#document;
|
|
4242
4406
|
}
|
|
4407
|
+
/**
|
|
4408
|
+
* @deprecated Read `sources` from the result of `parse`. It follows the last parse. Removed in Kubb v6.
|
|
4409
|
+
*/
|
|
4243
4410
|
get sources() {
|
|
4244
4411
|
return this.#sources;
|
|
4245
4412
|
}
|
|
@@ -4247,30 +4414,46 @@ var Adapter = class {
|
|
|
4247
4414
|
const document = await parseFromConfig({
|
|
4248
4415
|
type: "path",
|
|
4249
4416
|
path: input
|
|
4250
|
-
}, [], {
|
|
4417
|
+
}, [], { harness: options?.harness });
|
|
4251
4418
|
assertDocument(document);
|
|
4252
4419
|
await validateDocument(document, options);
|
|
4253
4420
|
};
|
|
4254
4421
|
settle = async () => {
|
|
4255
4422
|
await Promise.all(this.#pendingValidations.splice(0));
|
|
4256
4423
|
};
|
|
4257
|
-
|
|
4424
|
+
/**
|
|
4425
|
+
* Returns the AST with the document and the files this call read, so two overlapping parses on one instance each
|
|
4426
|
+
* get their own.
|
|
4427
|
+
*/
|
|
4428
|
+
parse = async (source, options) => {
|
|
4429
|
+
const entry = this.#start(source, options);
|
|
4430
|
+
return {
|
|
4431
|
+
node: await entry.promise,
|
|
4432
|
+
document: entry.document,
|
|
4433
|
+
sources: entry.sources
|
|
4434
|
+
};
|
|
4435
|
+
};
|
|
4436
|
+
#start(source, { signal, harness } = {}) {
|
|
4258
4437
|
signal?.throwIfAborted();
|
|
4259
|
-
const cached = this.#
|
|
4438
|
+
const cached = this.#cached({
|
|
4439
|
+
source,
|
|
4440
|
+
harness
|
|
4441
|
+
});
|
|
4260
4442
|
if (cached) {
|
|
4261
4443
|
this.#sources = cached.sources;
|
|
4262
4444
|
this.#document = cached.document;
|
|
4263
|
-
return cached
|
|
4445
|
+
return cached;
|
|
4264
4446
|
}
|
|
4265
4447
|
const read = [];
|
|
4266
4448
|
this.#sources = read;
|
|
4267
4449
|
const entry = {
|
|
4268
4450
|
sources: read,
|
|
4269
|
-
document: null
|
|
4451
|
+
document: null,
|
|
4452
|
+
harness
|
|
4270
4453
|
};
|
|
4271
4454
|
const promise = (async () => {
|
|
4272
4455
|
const document = await parseFromConfig(source, read, {
|
|
4273
|
-
|
|
4456
|
+
harness,
|
|
4274
4457
|
signal
|
|
4275
4458
|
});
|
|
4276
4459
|
signal?.throwIfAborted();
|
|
@@ -4293,13 +4476,18 @@ var Adapter = class {
|
|
|
4293
4476
|
pointers,
|
|
4294
4477
|
parseSchema
|
|
4295
4478
|
}));
|
|
4296
|
-
})()
|
|
4297
|
-
this.#inputCache.set(source, Object.assign(entry, { promise }));
|
|
4298
|
-
return promise.catch((error) => {
|
|
4479
|
+
})().catch((error) => {
|
|
4299
4480
|
if (signal?.aborted) this.#inputCache.delete(source);
|
|
4300
4481
|
throw error;
|
|
4301
4482
|
});
|
|
4302
|
-
|
|
4483
|
+
const started = Object.assign(entry, { promise });
|
|
4484
|
+
this.#inputCache.set(source, started);
|
|
4485
|
+
return started;
|
|
4486
|
+
}
|
|
4487
|
+
#cached({ source, harness }) {
|
|
4488
|
+
const cached = this.#inputCache.get(source);
|
|
4489
|
+
return cached?.harness === harness ? cached : void 0;
|
|
4490
|
+
}
|
|
4303
4491
|
/**
|
|
4304
4492
|
* Parses every schema and operation once, resolving ref aliases and discriminator children from that same pass.
|
|
4305
4493
|
*/
|