@kubb/adapter-oas 5.0.0-alpha.16 → 5.0.0-alpha.17
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 +64 -125
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -25
- package/dist/index.js +66 -123
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/adapter.ts +10 -3
- package/src/constants.ts +1 -2
- package/src/parser.ts +79 -52
- package/src/types.ts +1 -26
- package/src/discriminator.ts +0 -25
- package/src/infer.ts +0 -85
- package/src/naming.ts +0 -25
- package/src/refResolver.ts +0 -65
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as _kubb_core0 from "@kubb/core";
|
|
|
3
3
|
import { AdapterFactoryOptions } from "@kubb/core";
|
|
4
4
|
import { DiscriminatorObject, OASDocument, ParameterObject, SchemaObject } from "oas/types";
|
|
5
5
|
import BaseOas from "oas";
|
|
6
|
+
import { ParserOptions } from "@kubb/ast/types";
|
|
6
7
|
import * as oas_normalize_lib_types0 from "oas-normalize/lib/types";
|
|
7
8
|
import { Operation } from "oas/operation";
|
|
8
9
|
import { OpenAPIV3 } from "openapi-types";
|
|
@@ -94,31 +95,6 @@ declare class Oas extends BaseOas {
|
|
|
94
95
|
}
|
|
95
96
|
//#endregion
|
|
96
97
|
//#region src/types.d.ts
|
|
97
|
-
/**
|
|
98
|
-
* Controls how various OAS constructs are mapped to Kubb AST nodes.
|
|
99
|
-
*/
|
|
100
|
-
type ParserOptions = {
|
|
101
|
-
/**
|
|
102
|
-
* How `format: 'date-time'` schemas are represented. `false` falls through to a plain string.
|
|
103
|
-
*/
|
|
104
|
-
dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date';
|
|
105
|
-
/**
|
|
106
|
-
* Whether `type: 'integer'` and `format: 'int64'` produce `number` or `bigint` nodes.
|
|
107
|
-
*/
|
|
108
|
-
integerType?: 'number' | 'bigint';
|
|
109
|
-
/**
|
|
110
|
-
* AST type used when no schema type can be inferred.
|
|
111
|
-
*/
|
|
112
|
-
unknownType: 'any' | 'unknown' | 'void';
|
|
113
|
-
/**
|
|
114
|
-
* AST type used for completely empty schemas (`{}`).
|
|
115
|
-
*/
|
|
116
|
-
emptySchemaType: 'any' | 'unknown' | 'void';
|
|
117
|
-
/**
|
|
118
|
-
* Suffix appended to derived enum names when building property schema names.
|
|
119
|
-
*/
|
|
120
|
-
enumSuffix: 'enum' | (string & {});
|
|
121
|
-
};
|
|
122
98
|
type OasAdapterOptions = {
|
|
123
99
|
/**
|
|
124
100
|
* Validate the OpenAPI spec before parsing.
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./chunk--u3MIqq1.js";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import {
|
|
3
|
+
import { childName, collectImports, createDiscriminantNode, createOperation, createParameter, createProperty, createResponse, createRoot, createSchema, enumPropName, extractRefName, findDiscriminator, mediaTypes, mergeAdjacentObjects, narrowSchema, resolveNames, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion } from "@kubb/ast";
|
|
4
4
|
import { createAdapter } from "@kubb/core";
|
|
5
5
|
import { bundle, loadConfig } from "@redocly/openapi-core";
|
|
6
6
|
import yaml from "@stoplight/yaml";
|
|
@@ -882,91 +882,6 @@ function resolveCollisions(schemasWithMeta) {
|
|
|
882
882
|
};
|
|
883
883
|
}
|
|
884
884
|
//#endregion
|
|
885
|
-
//#region src/discriminator.ts
|
|
886
|
-
function resolveDiscriminatorValue(mapping, ref) {
|
|
887
|
-
if (!mapping || !ref) return void 0;
|
|
888
|
-
return Object.entries(mapping).find(([, value]) => value === ref)?.[0];
|
|
889
|
-
}
|
|
890
|
-
function createDiscriminantNode(propertyName, value) {
|
|
891
|
-
return createSchema({
|
|
892
|
-
type: "object",
|
|
893
|
-
primitive: "object",
|
|
894
|
-
properties: [createProperty({
|
|
895
|
-
name: propertyName,
|
|
896
|
-
schema: createSchema({
|
|
897
|
-
type: "enum",
|
|
898
|
-
primitive: "string",
|
|
899
|
-
enumValues: [value]
|
|
900
|
-
}),
|
|
901
|
-
required: true
|
|
902
|
-
})]
|
|
903
|
-
});
|
|
904
|
-
}
|
|
905
|
-
//#endregion
|
|
906
|
-
//#region src/naming.ts
|
|
907
|
-
function resolveChildName(parentName, propName) {
|
|
908
|
-
return parentName ? pascalCase([parentName, propName].join(" ")) : void 0;
|
|
909
|
-
}
|
|
910
|
-
function resolveEnumPropName(parentName, propName, enumSuffix) {
|
|
911
|
-
return pascalCase([
|
|
912
|
-
parentName,
|
|
913
|
-
propName,
|
|
914
|
-
enumSuffix
|
|
915
|
-
].filter(Boolean).join(" "));
|
|
916
|
-
}
|
|
917
|
-
function applyEnumName(propNode, parentName, propName, enumSuffix) {
|
|
918
|
-
const enumNode = narrowSchema(propNode, "enum");
|
|
919
|
-
if (enumNode?.primitive === "boolean") return {
|
|
920
|
-
...propNode,
|
|
921
|
-
name: void 0
|
|
922
|
-
};
|
|
923
|
-
if (enumNode) return {
|
|
924
|
-
...propNode,
|
|
925
|
-
name: resolveEnumPropName(parentName, propName, enumSuffix)
|
|
926
|
-
};
|
|
927
|
-
return propNode;
|
|
928
|
-
}
|
|
929
|
-
//#endregion
|
|
930
|
-
//#region src/refResolver.ts
|
|
931
|
-
/**
|
|
932
|
-
* Collects import entries for all `ref` schema nodes in `node`.
|
|
933
|
-
*/
|
|
934
|
-
function getImports({ node, nameMapping, resolve }) {
|
|
935
|
-
return collect(node, { schema(schemaNode) {
|
|
936
|
-
if (schemaNode.type !== "ref" || !schemaNode.ref) return;
|
|
937
|
-
const rawName = extractRefName(schemaNode.ref);
|
|
938
|
-
const result = resolve(nameMapping.get(rawName) ?? rawName);
|
|
939
|
-
if (!result) return;
|
|
940
|
-
return {
|
|
941
|
-
name: [result.name],
|
|
942
|
-
path: result.path
|
|
943
|
-
};
|
|
944
|
-
} });
|
|
945
|
-
}
|
|
946
|
-
/**
|
|
947
|
-
* Walks a schema tree and resolves `ref`/`enum` names through callbacks.
|
|
948
|
-
*/
|
|
949
|
-
function resolveRefs({ node, nameMapping, resolveName, resolveEnumName }) {
|
|
950
|
-
return transform(node, { schema(schemaNode) {
|
|
951
|
-
const schemaRef = narrowSchema(schemaNode, schemaTypes.ref);
|
|
952
|
-
if (schemaRef && (schemaRef.ref || schemaRef.name)) {
|
|
953
|
-
const rawRef = schemaRef.ref ?? schemaRef.name;
|
|
954
|
-
const resolved = resolveName(nameMapping.get(rawRef) ?? rawRef);
|
|
955
|
-
if (resolved) return {
|
|
956
|
-
...schemaNode,
|
|
957
|
-
name: resolved
|
|
958
|
-
};
|
|
959
|
-
}
|
|
960
|
-
if (schemaNode.type === "enum" && schemaNode.name) {
|
|
961
|
-
const resolved = (resolveEnumName ?? resolveName)(schemaNode.name);
|
|
962
|
-
if (resolved) return {
|
|
963
|
-
...schemaNode,
|
|
964
|
-
name: resolved
|
|
965
|
-
};
|
|
966
|
-
}
|
|
967
|
-
} });
|
|
968
|
-
}
|
|
969
|
-
//#endregion
|
|
970
885
|
//#region src/parser.ts
|
|
971
886
|
/**
|
|
972
887
|
* Looks up the Kubb `SchemaType` for a given OAS `format` string.
|
|
@@ -1156,7 +1071,7 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1156
1071
|
const inOneOf = parentUnion.some((oneOfItem) => isReference(oneOfItem) && oneOfItem.$ref === childRef);
|
|
1157
1072
|
const inMapping = Object.values(deref.discriminator.mapping ?? {}).some((v) => v === childRef);
|
|
1158
1073
|
if (inOneOf || inMapping) {
|
|
1159
|
-
const discriminatorValue =
|
|
1074
|
+
const discriminatorValue = findDiscriminator(deref.discriminator.mapping, childRef);
|
|
1160
1075
|
if (discriminatorValue) filteredDiscriminantValues.push({
|
|
1161
1076
|
propertyName: deref.discriminator.propertyName,
|
|
1162
1077
|
value: discriminatorValue
|
|
@@ -1188,10 +1103,13 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1188
1103
|
const { allOf: _allOf, ...schemaWithoutAllOf } = schema;
|
|
1189
1104
|
allOfMembers.push(convertSchema({ schema: schemaWithoutAllOf }, rawOptions));
|
|
1190
1105
|
}
|
|
1191
|
-
for (const { propertyName, value } of filteredDiscriminantValues) allOfMembers.push(createDiscriminantNode(
|
|
1106
|
+
for (const { propertyName, value } of filteredDiscriminantValues) allOfMembers.push(createDiscriminantNode({
|
|
1107
|
+
propertyName,
|
|
1108
|
+
value
|
|
1109
|
+
}));
|
|
1192
1110
|
return createSchema({
|
|
1193
1111
|
type: "intersection",
|
|
1194
|
-
members: [...
|
|
1112
|
+
members: [...mergeAdjacentObjects(allOfMembers.slice(0, syntheticStart)), ...mergeAdjacentObjects(allOfMembers.slice(syntheticStart))],
|
|
1195
1113
|
...renderSchemaBase(schema, name, nullable, defaultValue)
|
|
1196
1114
|
});
|
|
1197
1115
|
}
|
|
@@ -1204,6 +1122,15 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1204
1122
|
* adding common fields next to a discriminated union.
|
|
1205
1123
|
*/
|
|
1206
1124
|
function convertUnion({ schema, name, nullable, defaultValue, rawOptions }) {
|
|
1125
|
+
function pickDiscriminatorPropertyNode(node, propertyName) {
|
|
1126
|
+
const discriminatorProperty = narrowSchema(node, "object")?.properties?.find((property) => property.name === propertyName);
|
|
1127
|
+
if (!discriminatorProperty) return;
|
|
1128
|
+
return createSchema({
|
|
1129
|
+
type: "object",
|
|
1130
|
+
primitive: "object",
|
|
1131
|
+
properties: [discriminatorProperty]
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1207
1134
|
const unionMembers = [...schema.oneOf ?? [], ...schema.anyOf ?? []];
|
|
1208
1135
|
const unionBase = {
|
|
1209
1136
|
...renderSchemaBase(schema, name, nullable, defaultValue),
|
|
@@ -1217,32 +1144,40 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1217
1144
|
name
|
|
1218
1145
|
}, rawOptions);
|
|
1219
1146
|
})() : void 0;
|
|
1220
|
-
if (sharedPropertiesNode || discriminator?.mapping)
|
|
1221
|
-
|
|
1222
|
-
...unionBase,
|
|
1223
|
-
members: unionMembers.map((s) => {
|
|
1147
|
+
if (sharedPropertiesNode || discriminator?.mapping) {
|
|
1148
|
+
const members = unionMembers.map((s) => {
|
|
1224
1149
|
const ref = isReference(s) ? s.$ref : void 0;
|
|
1225
|
-
const discriminatorValue =
|
|
1150
|
+
const discriminatorValue = findDiscriminator(discriminator?.mapping, ref);
|
|
1226
1151
|
const memberNode = convertSchema({ schema: s }, rawOptions);
|
|
1227
|
-
if (
|
|
1152
|
+
if (!discriminatorValue || !discriminator) return memberNode;
|
|
1153
|
+
return createSchema({
|
|
1228
1154
|
type: "intersection",
|
|
1229
|
-
members: [memberNode,
|
|
1155
|
+
members: [memberNode, (sharedPropertiesNode ? pickDiscriminatorPropertyNode(setDiscriminatorEnum({
|
|
1230
1156
|
node: sharedPropertiesNode,
|
|
1231
1157
|
propertyName: discriminator.propertyName,
|
|
1232
1158
|
values: [discriminatorValue]
|
|
1233
|
-
}) :
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
type: "intersection",
|
|
1238
|
-
members: [memberNode, createDiscriminantNode(discriminator.propertyName, discriminatorValue)]
|
|
1159
|
+
}), discriminator.propertyName) : void 0) ?? createDiscriminantNode({
|
|
1160
|
+
propertyName: discriminator.propertyName,
|
|
1161
|
+
value: discriminatorValue
|
|
1162
|
+
})]
|
|
1239
1163
|
});
|
|
1240
|
-
})
|
|
1241
|
-
|
|
1164
|
+
});
|
|
1165
|
+
const unionNode = createSchema({
|
|
1166
|
+
type: "union",
|
|
1167
|
+
...unionBase,
|
|
1168
|
+
members
|
|
1169
|
+
});
|
|
1170
|
+
if (!sharedPropertiesNode) return unionNode;
|
|
1171
|
+
return createSchema({
|
|
1172
|
+
type: "intersection",
|
|
1173
|
+
...renderSchemaBase(schema, name, nullable, defaultValue),
|
|
1174
|
+
members: [unionNode, sharedPropertiesNode]
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1242
1177
|
return createSchema({
|
|
1243
1178
|
type: "union",
|
|
1244
1179
|
...unionBase,
|
|
1245
|
-
members:
|
|
1180
|
+
members: simplifyUnion(unionMembers.map((s) => convertSchema({ schema: s }, rawOptions)))
|
|
1246
1181
|
});
|
|
1247
1182
|
}
|
|
1248
1183
|
/**
|
|
@@ -1354,15 +1289,15 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1354
1289
|
};
|
|
1355
1290
|
const extensionKey = enumExtensionKeys.find((key) => key in schema);
|
|
1356
1291
|
if (extensionKey || enumPrimitive === "number" || enumPrimitive === "integer" || enumPrimitive === "boolean") {
|
|
1357
|
-
const
|
|
1292
|
+
const enumPrimitiveType = enumPrimitive === "number" || enumPrimitive === "integer" ? "number" : enumPrimitive === "boolean" ? "boolean" : "string";
|
|
1358
1293
|
const sourceValues = extensionKey ? [...new Set(schema[extensionKey])] : [...new Set(filteredValues)];
|
|
1359
1294
|
return createSchema({
|
|
1360
1295
|
...enumBase,
|
|
1361
|
-
|
|
1296
|
+
primitive: enumPrimitiveType,
|
|
1362
1297
|
namedEnumValues: sourceValues.map((label, index) => ({
|
|
1363
1298
|
name: String(label),
|
|
1364
1299
|
value: extensionKey ? filteredValues[index] ?? label : label,
|
|
1365
|
-
|
|
1300
|
+
primitive: enumPrimitiveType
|
|
1366
1301
|
}))
|
|
1367
1302
|
});
|
|
1368
1303
|
}
|
|
@@ -1388,13 +1323,13 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1388
1323
|
const required = Array.isArray(schema.required) ? schema.required.includes(propName) : !!schema.required;
|
|
1389
1324
|
const resolvedPropSchema = propSchema;
|
|
1390
1325
|
const propNullable = isNullable(resolvedPropSchema);
|
|
1391
|
-
let schemaNode =
|
|
1326
|
+
let schemaNode = setEnumName(convertSchema({
|
|
1392
1327
|
schema: resolvedPropSchema,
|
|
1393
|
-
name:
|
|
1328
|
+
name: childName(name, propName)
|
|
1394
1329
|
}, rawOptions), name, propName, options.enumSuffix);
|
|
1395
1330
|
const tupleNode = narrowSchema(schemaNode, "tuple");
|
|
1396
1331
|
if (tupleNode?.items) {
|
|
1397
|
-
const namedItems = tupleNode.items.map((item) =>
|
|
1332
|
+
const namedItems = tupleNode.items.map((item) => setEnumName(item, name, propName, options.enumSuffix));
|
|
1398
1333
|
if (namedItems.some((item, i) => item !== tupleNode.items[i])) schemaNode = {
|
|
1399
1334
|
...tupleNode,
|
|
1400
1335
|
items: namedItems
|
|
@@ -1427,11 +1362,11 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1427
1362
|
});
|
|
1428
1363
|
if (isDiscriminator(schema) && schema.discriminator.mapping) {
|
|
1429
1364
|
const discPropName = schema.discriminator.propertyName;
|
|
1430
|
-
return
|
|
1365
|
+
return setDiscriminatorEnum({
|
|
1431
1366
|
node: objectNode,
|
|
1432
1367
|
propertyName: discPropName,
|
|
1433
1368
|
values: Object.keys(schema.discriminator.mapping),
|
|
1434
|
-
enumName: name ?
|
|
1369
|
+
enumName: name ? enumPropName(name, discPropName, options.enumSuffix) : void 0
|
|
1435
1370
|
});
|
|
1436
1371
|
}
|
|
1437
1372
|
return objectNode;
|
|
@@ -1463,7 +1398,7 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1463
1398
|
*/
|
|
1464
1399
|
function convertArray({ schema, name, nullable, defaultValue, rawOptions, options }) {
|
|
1465
1400
|
const rawItems = schema.items;
|
|
1466
|
-
const itemName = rawItems?.enum?.length && name ?
|
|
1401
|
+
const itemName = rawItems?.enum?.length && name ? enumPropName(void 0, name, options.enumSuffix) : void 0;
|
|
1467
1402
|
return createSchema({
|
|
1468
1403
|
type: "array",
|
|
1469
1404
|
primitive: "array",
|
|
@@ -1694,15 +1629,16 @@ function createOasParser(oas, { contentType } = {}) {
|
|
|
1694
1629
|
operations: Object.entries(paths).flatMap(([_path, methods]) => Object.entries(methods).map(([, operation]) => operation ? parseOperation(mergedOptions, oas, operation) : null).filter((op) => op !== null))
|
|
1695
1630
|
});
|
|
1696
1631
|
}
|
|
1632
|
+
const resolveRefs = (node, resolveName, resolveEnumName) => resolveNames({
|
|
1633
|
+
node,
|
|
1634
|
+
nameMapping,
|
|
1635
|
+
resolveName,
|
|
1636
|
+
resolveEnumName
|
|
1637
|
+
});
|
|
1697
1638
|
return {
|
|
1698
1639
|
parse,
|
|
1699
1640
|
convertSchema,
|
|
1700
|
-
resolveRefs
|
|
1701
|
-
node,
|
|
1702
|
-
nameMapping,
|
|
1703
|
-
resolveName,
|
|
1704
|
-
resolveEnumName
|
|
1705
|
-
}), "resolveRefs"),
|
|
1641
|
+
resolveRefs,
|
|
1706
1642
|
nameMapping
|
|
1707
1643
|
};
|
|
1708
1644
|
}
|
|
@@ -1747,10 +1683,17 @@ const adapterOas = createAdapter((options) => {
|
|
|
1747
1683
|
nameMapping
|
|
1748
1684
|
},
|
|
1749
1685
|
getImports(node, resolve) {
|
|
1750
|
-
return
|
|
1686
|
+
return collectImports({
|
|
1751
1687
|
node,
|
|
1752
1688
|
nameMapping,
|
|
1753
|
-
resolve
|
|
1689
|
+
resolve: (schemaName) => {
|
|
1690
|
+
const result = resolve(schemaName);
|
|
1691
|
+
if (!result) return;
|
|
1692
|
+
return {
|
|
1693
|
+
name: [result.name],
|
|
1694
|
+
path: result.path
|
|
1695
|
+
};
|
|
1696
|
+
}
|
|
1754
1697
|
});
|
|
1755
1698
|
},
|
|
1756
1699
|
async parse(source) {
|