@kubb/adapter-oas 5.0.0-beta.17 → 5.0.0-beta.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +85 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +85 -65
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/adapter.ts +24 -13
- package/src/discriminator.ts +4 -7
- package/src/parser.ts +26 -23
- package/src/refs.ts +2 -5
- package/src/resolvers.ts +7 -6
package/dist/index.cjs
CHANGED
|
@@ -194,11 +194,14 @@ function buildDiscriminatorChildMap(schemas) {
|
|
|
194
194
|
const enumValues = enumNode.enumValues.filter((v) => v !== null);
|
|
195
195
|
if (!enumValues.length) continue;
|
|
196
196
|
const existing = childMap.get(refNode.name);
|
|
197
|
-
if (existing)
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
197
|
+
if (!existing) {
|
|
198
|
+
childMap.set(refNode.name, {
|
|
199
|
+
propertyName: discriminatorPropertyName,
|
|
200
|
+
enumValues: [...enumValues]
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
existing.enumValues.push(...enumValues);
|
|
202
205
|
}
|
|
203
206
|
}
|
|
204
207
|
return childMap;
|
|
@@ -781,8 +784,8 @@ function resolveRef(document, $ref) {
|
|
|
781
784
|
const origRef = $ref;
|
|
782
785
|
$ref = $ref.trim();
|
|
783
786
|
if ($ref === "") return null;
|
|
784
|
-
if (
|
|
785
|
-
|
|
787
|
+
if (!$ref.startsWith("#")) return null;
|
|
788
|
+
$ref = globalThis.decodeURIComponent($ref.substring(1));
|
|
786
789
|
let docCache = _refCache.get(document);
|
|
787
790
|
if (!docCache) {
|
|
788
791
|
docCache = /* @__PURE__ */ new Map();
|
|
@@ -1013,12 +1016,14 @@ function* collectRefs(schema) {
|
|
|
1013
1016
|
}
|
|
1014
1017
|
if (schema && typeof schema === "object") for (const key in schema) {
|
|
1015
1018
|
const value = schema[key];
|
|
1016
|
-
if (key === "$ref" && typeof value === "string") {
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1019
|
+
if (!(key === "$ref" && typeof value === "string")) {
|
|
1020
|
+
yield* collectRefs(value);
|
|
1021
|
+
continue;
|
|
1022
|
+
}
|
|
1023
|
+
if (value.startsWith("#/components/schemas/")) {
|
|
1024
|
+
const name = value.slice(21);
|
|
1025
|
+
if (name) yield name;
|
|
1026
|
+
}
|
|
1022
1027
|
}
|
|
1023
1028
|
}
|
|
1024
1029
|
/**
|
|
@@ -1170,7 +1175,8 @@ function buildSchemaNode(schema, name, nullable, defaultValue) {
|
|
|
1170
1175
|
readOnly: schema.readOnly,
|
|
1171
1176
|
writeOnly: schema.writeOnly,
|
|
1172
1177
|
default: defaultValue,
|
|
1173
|
-
example: schema.example
|
|
1178
|
+
example: schema.example,
|
|
1179
|
+
format: schema.format
|
|
1174
1180
|
};
|
|
1175
1181
|
}
|
|
1176
1182
|
/**
|
|
@@ -1253,17 +1259,19 @@ function createSchemaParser(ctx) {
|
|
|
1253
1259
|
function convertRef({ schema, name, nullable, defaultValue, rawOptions }) {
|
|
1254
1260
|
let resolvedSchema;
|
|
1255
1261
|
const refPath = schema.$ref;
|
|
1256
|
-
if (refPath && !resolvingRefs.has(refPath))
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1262
|
+
if (refPath && !resolvingRefs.has(refPath)) {
|
|
1263
|
+
if (!resolvedRefCache.has(refPath)) {
|
|
1264
|
+
try {
|
|
1265
|
+
const referenced = resolveRef(document, refPath);
|
|
1266
|
+
if (referenced) {
|
|
1267
|
+
resolvingRefs.add(refPath);
|
|
1268
|
+
resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
|
|
1269
|
+
resolvingRefs.delete(refPath);
|
|
1270
|
+
}
|
|
1271
|
+
} catch {}
|
|
1272
|
+
resolvedRefCache.set(refPath, resolvedSchema);
|
|
1273
|
+
}
|
|
1274
|
+
resolvedSchema = resolvedRefCache.get(refPath);
|
|
1267
1275
|
}
|
|
1268
1276
|
return _kubb_core.ast.createSchema({
|
|
1269
1277
|
...buildSchemaNode(schema, name, nullable, defaultValue),
|
|
@@ -1297,7 +1305,8 @@ function createSchemaParser(ctx) {
|
|
|
1297
1305
|
writeOnly: schema.writeOnly ?? memberNode.writeOnly,
|
|
1298
1306
|
default: mergedDefault,
|
|
1299
1307
|
example: schema.example ?? memberNode.example,
|
|
1300
|
-
pattern: schema.pattern ?? ("pattern" in memberNode ? memberNode.pattern : void 0)
|
|
1308
|
+
pattern: schema.pattern ?? ("pattern" in memberNode ? memberNode.pattern : void 0),
|
|
1309
|
+
format: schema.format ?? memberNode.format
|
|
1301
1310
|
});
|
|
1302
1311
|
}
|
|
1303
1312
|
const filteredDiscriminantValues = [];
|
|
@@ -1429,7 +1438,8 @@ function createSchemaParser(ctx) {
|
|
|
1429
1438
|
name,
|
|
1430
1439
|
title: schema.title,
|
|
1431
1440
|
description: schema.description,
|
|
1432
|
-
deprecated: schema.deprecated
|
|
1441
|
+
deprecated: schema.deprecated,
|
|
1442
|
+
format: schema.format
|
|
1433
1443
|
});
|
|
1434
1444
|
const constPrimitive = getPrimitiveType(typeof constValue === "number" ? "number" : typeof constValue === "boolean" ? "boolean" : "string");
|
|
1435
1445
|
return _kubb_core.ast.createSchema({
|
|
@@ -1533,7 +1543,8 @@ function createSchemaParser(ctx) {
|
|
|
1533
1543
|
readOnly: schema.readOnly,
|
|
1534
1544
|
writeOnly: schema.writeOnly,
|
|
1535
1545
|
default: enumDefault,
|
|
1536
|
-
example: schema.example
|
|
1546
|
+
example: schema.example,
|
|
1547
|
+
format: schema.format
|
|
1537
1548
|
};
|
|
1538
1549
|
const extensionKey = enumExtensionKeys.find((key) => key in schema);
|
|
1539
1550
|
if (extensionKey || enumPrimitive === "number" || enumPrimitive === "integer" || enumPrimitive === "boolean") {
|
|
@@ -1572,15 +1583,18 @@ function createSchemaParser(ctx) {
|
|
|
1572
1583
|
schema: resolvedPropSchema,
|
|
1573
1584
|
name: _kubb_core.ast.childName(name, propName)
|
|
1574
1585
|
}, rawOptions);
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1586
|
+
const schemaNode = (() => {
|
|
1587
|
+
const node = _kubb_core.ast.setEnumName(propNode, name, propName, options.enumSuffix);
|
|
1588
|
+
const tupleNode = _kubb_core.ast.narrowSchema(node, "tuple");
|
|
1589
|
+
if (tupleNode?.items) {
|
|
1590
|
+
const namedItems = tupleNode.items.map((item) => _kubb_core.ast.setEnumName(item, name, propName, options.enumSuffix));
|
|
1591
|
+
if (namedItems.some((item, i) => item !== tupleNode.items[i])) return {
|
|
1592
|
+
...tupleNode,
|
|
1593
|
+
items: namedItems
|
|
1594
|
+
};
|
|
1595
|
+
}
|
|
1596
|
+
return node;
|
|
1597
|
+
})();
|
|
1584
1598
|
return _kubb_core.ast.createProperty({
|
|
1585
1599
|
name: propName,
|
|
1586
1600
|
schema: {
|
|
@@ -1591,11 +1605,12 @@ function createSchemaParser(ctx) {
|
|
|
1591
1605
|
});
|
|
1592
1606
|
}) : [];
|
|
1593
1607
|
const additionalProperties = schema.additionalProperties;
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1608
|
+
const additionalPropertiesNode = (() => {
|
|
1609
|
+
if (additionalProperties === true) return true;
|
|
1610
|
+
if (additionalProperties === false) return false;
|
|
1611
|
+
if (additionalProperties && Object.keys(additionalProperties).length > 0) return parseSchema({ schema: additionalProperties }, rawOptions);
|
|
1612
|
+
if (additionalProperties) return _kubb_core.ast.createSchema({ type: typeOptionMap.get(options.unknownType) });
|
|
1613
|
+
})();
|
|
1599
1614
|
const rawPatternProperties = "patternProperties" in schema ? schema.patternProperties : void 0;
|
|
1600
1615
|
const patternProperties = rawPatternProperties ? Object.fromEntries(Object.entries(rawPatternProperties).map(([pattern, patternSchema]) => [pattern, patternSchema === true || typeof patternSchema === "object" && Object.keys(patternSchema).length === 0 ? _kubb_core.ast.createSchema({ type: typeOptionMap.get(options.unknownType) }) : parseSchema({ schema: patternSchema }, rawOptions)])) : void 0;
|
|
1601
1616
|
const objectNode = _kubb_core.ast.createSchema({
|
|
@@ -1706,7 +1721,8 @@ function createSchemaParser(ctx) {
|
|
|
1706
1721
|
title: schema.title,
|
|
1707
1722
|
description: schema.description,
|
|
1708
1723
|
deprecated: schema.deprecated,
|
|
1709
|
-
nullable
|
|
1724
|
+
nullable,
|
|
1725
|
+
format: schema.format
|
|
1710
1726
|
});
|
|
1711
1727
|
}
|
|
1712
1728
|
/**
|
|
@@ -1784,7 +1800,8 @@ function createSchemaParser(ctx) {
|
|
|
1784
1800
|
type: emptyType,
|
|
1785
1801
|
name,
|
|
1786
1802
|
title: schema.title,
|
|
1787
|
-
description: schema.description
|
|
1803
|
+
description: schema.description,
|
|
1804
|
+
format: schema.format
|
|
1788
1805
|
});
|
|
1789
1806
|
}
|
|
1790
1807
|
/**
|
|
@@ -1974,6 +1991,8 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
|
|
|
1974
1991
|
let nameMapping = /* @__PURE__ */ new Map();
|
|
1975
1992
|
let parsedDocument = null;
|
|
1976
1993
|
let schemaObjects = null;
|
|
1994
|
+
let baseOasInstance = null;
|
|
1995
|
+
let schemaParserInstance = null;
|
|
1977
1996
|
function resolveBaseURL(document) {
|
|
1978
1997
|
const server = serverIndex !== void 0 ? document.servers?.at(serverIndex) : void 0;
|
|
1979
1998
|
return server?.url ? resolveServerUrl(server, serverVariables) : void 0;
|
|
@@ -1993,6 +2012,17 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
|
|
|
1993
2012
|
}
|
|
1994
2013
|
return schemaObjects;
|
|
1995
2014
|
}
|
|
2015
|
+
function ensureBaseOas(document) {
|
|
2016
|
+
if (!baseOasInstance) baseOasInstance = new oas.default(document);
|
|
2017
|
+
return baseOasInstance;
|
|
2018
|
+
}
|
|
2019
|
+
function ensureSchemaParser(document) {
|
|
2020
|
+
if (!schemaParserInstance) schemaParserInstance = createSchemaParser({
|
|
2021
|
+
document,
|
|
2022
|
+
contentType
|
|
2023
|
+
});
|
|
2024
|
+
return schemaParserInstance;
|
|
2025
|
+
}
|
|
1996
2026
|
return {
|
|
1997
2027
|
name: "oas",
|
|
1998
2028
|
get options() {
|
|
@@ -2055,7 +2085,7 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
|
|
|
2055
2085
|
async count(source) {
|
|
2056
2086
|
const document = await ensureDocument(source);
|
|
2057
2087
|
const schemas = await ensureSchemas(document);
|
|
2058
|
-
const baseOas =
|
|
2088
|
+
const baseOas = ensureBaseOas(document);
|
|
2059
2089
|
const operationCount = Object.values(baseOas.getPaths()).flatMap(Object.values).filter(Boolean).length;
|
|
2060
2090
|
return {
|
|
2061
2091
|
schemas: Object.keys(schemas).length,
|
|
@@ -2065,43 +2095,33 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
|
|
|
2065
2095
|
async stream(source) {
|
|
2066
2096
|
const document = await ensureDocument(source);
|
|
2067
2097
|
const schemas = await ensureSchemas(document);
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
const { parseSchema: _preParser } =
|
|
2071
|
-
document,
|
|
2072
|
-
contentType
|
|
2073
|
-
});
|
|
2098
|
+
const discriminatorChildMap = (() => {
|
|
2099
|
+
if (discriminator !== "inherit") return null;
|
|
2100
|
+
const { parseSchema: _preParser } = ensureSchemaParser(document);
|
|
2074
2101
|
const parentNodes = [];
|
|
2075
2102
|
for (const [name, schema] of Object.entries(schemas)) if ((schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName) parentNodes.push(_preParser({
|
|
2076
2103
|
schema,
|
|
2077
2104
|
name
|
|
2078
2105
|
}, parserOptions));
|
|
2079
|
-
|
|
2080
|
-
}
|
|
2106
|
+
return parentNodes.length > 0 ? buildDiscriminatorChildMap(parentNodes) : null;
|
|
2107
|
+
})();
|
|
2081
2108
|
const schemasIterable = { [Symbol.asyncIterator]() {
|
|
2082
2109
|
return (async function* () {
|
|
2083
|
-
const { parseSchema: _parseSchema } =
|
|
2084
|
-
document,
|
|
2085
|
-
contentType
|
|
2086
|
-
});
|
|
2110
|
+
const { parseSchema: _parseSchema } = ensureSchemaParser(document);
|
|
2087
2111
|
for (const [name, schema] of Object.entries(schemas)) {
|
|
2088
|
-
|
|
2112
|
+
const parsedNode = _parseSchema({
|
|
2089
2113
|
schema,
|
|
2090
2114
|
name
|
|
2091
2115
|
}, parserOptions);
|
|
2092
2116
|
const entry = discriminatorChildMap?.get(name);
|
|
2093
|
-
|
|
2094
|
-
yield node;
|
|
2117
|
+
yield entry ? patchDiscriminatorNode(parsedNode, entry) : parsedNode;
|
|
2095
2118
|
}
|
|
2096
2119
|
})();
|
|
2097
2120
|
} };
|
|
2098
2121
|
const operationsIterable = { [Symbol.asyncIterator]() {
|
|
2099
2122
|
return (async function* () {
|
|
2100
|
-
const { parseOperation: _parseOperation } =
|
|
2101
|
-
|
|
2102
|
-
contentType
|
|
2103
|
-
});
|
|
2104
|
-
const paths = new oas.default(document).getPaths();
|
|
2123
|
+
const { parseOperation: _parseOperation } = ensureSchemaParser(document);
|
|
2124
|
+
const paths = ensureBaseOas(document).getPaths();
|
|
2105
2125
|
for (const methods of Object.values(paths)) for (const operation of Object.values(methods)) {
|
|
2106
2126
|
if (!operation) continue;
|
|
2107
2127
|
const node = _parseOperation(parserOptions, operation);
|