@kubb/adapter-oas 5.0.0-alpha.12 → 5.0.0-alpha.13
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 +83 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +83 -21
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/adapter.ts +2 -3
- package/src/parser.ts +100 -15
package/dist/index.cjs
CHANGED
|
@@ -1281,6 +1281,7 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1281
1281
|
pattern: schema.pattern ?? ("pattern" in memberNode ? memberNode.pattern : void 0)
|
|
1282
1282
|
});
|
|
1283
1283
|
}
|
|
1284
|
+
const filteredDiscriminantValues = [];
|
|
1284
1285
|
const allOfMembers = schema.allOf.filter((item) => {
|
|
1285
1286
|
if (!isReference(item) || !name) return true;
|
|
1286
1287
|
const deref = oas.get(item.$ref);
|
|
@@ -1290,7 +1291,15 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1290
1291
|
const childRef = `#/components/schemas/${name}`;
|
|
1291
1292
|
const inOneOf = parentUnion.some((oneOfItem) => isReference(oneOfItem) && oneOfItem.$ref === childRef);
|
|
1292
1293
|
const inMapping = Object.values(deref.discriminator.mapping ?? {}).some((v) => v === childRef);
|
|
1293
|
-
|
|
1294
|
+
if (inOneOf || inMapping) {
|
|
1295
|
+
const discriminatorValue = Object.entries(deref.discriminator.mapping ?? {}).find(([, v]) => v === childRef)?.[0];
|
|
1296
|
+
if (discriminatorValue) filteredDiscriminantValues.push({
|
|
1297
|
+
propertyName: deref.discriminator.propertyName,
|
|
1298
|
+
value: discriminatorValue
|
|
1299
|
+
});
|
|
1300
|
+
return false;
|
|
1301
|
+
}
|
|
1302
|
+
return true;
|
|
1294
1303
|
}).map((s) => convertSchema({ schema: s }, options));
|
|
1295
1304
|
const syntheticStart = allOfMembers.length;
|
|
1296
1305
|
if (Array.isArray(schema.required) && schema.required.length) {
|
|
@@ -1315,9 +1324,22 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1315
1324
|
const { allOf: _allOf, ...schemaWithoutAllOf } = schema;
|
|
1316
1325
|
allOfMembers.push(convertSchema({ schema: schemaWithoutAllOf }, options));
|
|
1317
1326
|
}
|
|
1327
|
+
for (const { propertyName, value } of filteredDiscriminantValues) allOfMembers.push((0, _kubb_ast.createSchema)({
|
|
1328
|
+
type: "object",
|
|
1329
|
+
primitive: "object",
|
|
1330
|
+
properties: [(0, _kubb_ast.createProperty)({
|
|
1331
|
+
name: propertyName,
|
|
1332
|
+
schema: (0, _kubb_ast.createSchema)({
|
|
1333
|
+
type: "enum",
|
|
1334
|
+
primitive: "string",
|
|
1335
|
+
enumValues: [value]
|
|
1336
|
+
}),
|
|
1337
|
+
required: true
|
|
1338
|
+
})]
|
|
1339
|
+
}));
|
|
1318
1340
|
return (0, _kubb_ast.createSchema)({
|
|
1319
1341
|
type: "intersection",
|
|
1320
|
-
members: [...allOfMembers.slice(0, syntheticStart), ...mergeAdjacentAnonymousObjects(allOfMembers.slice(syntheticStart))],
|
|
1342
|
+
members: [...mergeAdjacentAnonymousObjects(allOfMembers.slice(0, syntheticStart)), ...mergeAdjacentAnonymousObjects(allOfMembers.slice(syntheticStart))],
|
|
1321
1343
|
...renderSchemaBase(schema, name, nullable, defaultValue)
|
|
1322
1344
|
});
|
|
1323
1345
|
}
|
|
@@ -1361,6 +1383,33 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1361
1383
|
})
|
|
1362
1384
|
});
|
|
1363
1385
|
}
|
|
1386
|
+
const discriminator = isDiscriminator(schema) ? schema.discriminator : void 0;
|
|
1387
|
+
if (discriminator?.mapping) return (0, _kubb_ast.createSchema)({
|
|
1388
|
+
type: "union",
|
|
1389
|
+
...unionBase,
|
|
1390
|
+
members: unionMembers.map((s) => {
|
|
1391
|
+
const ref = isReference(s) ? s.$ref : void 0;
|
|
1392
|
+
const discriminatorValue = ref ? Object.entries(discriminator.mapping).find(([, v]) => v === ref)?.[0] : void 0;
|
|
1393
|
+
const memberNode = convertSchema({ schema: s }, options);
|
|
1394
|
+
if (!discriminatorValue) return memberNode;
|
|
1395
|
+
return (0, _kubb_ast.createSchema)({
|
|
1396
|
+
type: "intersection",
|
|
1397
|
+
members: [memberNode, (0, _kubb_ast.createSchema)({
|
|
1398
|
+
type: "object",
|
|
1399
|
+
primitive: "object",
|
|
1400
|
+
properties: [(0, _kubb_ast.createProperty)({
|
|
1401
|
+
name: discriminator.propertyName,
|
|
1402
|
+
schema: (0, _kubb_ast.createSchema)({
|
|
1403
|
+
type: "enum",
|
|
1404
|
+
primitive: "string",
|
|
1405
|
+
enumValues: [discriminatorValue]
|
|
1406
|
+
}),
|
|
1407
|
+
required: true
|
|
1408
|
+
})]
|
|
1409
|
+
})]
|
|
1410
|
+
});
|
|
1411
|
+
})
|
|
1412
|
+
});
|
|
1364
1413
|
return (0, _kubb_ast.createSchema)({
|
|
1365
1414
|
type: "union",
|
|
1366
1415
|
...unionBase,
|
|
@@ -1380,8 +1429,7 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1380
1429
|
name,
|
|
1381
1430
|
title: schema.title,
|
|
1382
1431
|
description: schema.description,
|
|
1383
|
-
deprecated: schema.deprecated
|
|
1384
|
-
nullable
|
|
1432
|
+
deprecated: schema.deprecated
|
|
1385
1433
|
});
|
|
1386
1434
|
return (0, _kubb_ast.createSchema)({
|
|
1387
1435
|
type: "enum",
|
|
@@ -1583,14 +1631,23 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1583
1631
|
const required = Array.isArray(schema.required) ? schema.required.includes(propName) : !!schema.required;
|
|
1584
1632
|
const resolvedPropSchema = propSchema;
|
|
1585
1633
|
const propNullable = isNullable(resolvedPropSchema);
|
|
1634
|
+
let schemaNode = applyEnumName(convertSchema({
|
|
1635
|
+
schema: resolvedPropSchema,
|
|
1636
|
+
name: resolveChildName(name, propName)
|
|
1637
|
+
}, options), name, propName, mergedOptions.enumSuffix);
|
|
1638
|
+
const tupleNode = (0, _kubb_ast.narrowSchema)(schemaNode, "tuple");
|
|
1639
|
+
if (tupleNode?.items) {
|
|
1640
|
+
const namedItems = tupleNode.items.map((item) => applyEnumName(item, name, propName, mergedOptions.enumSuffix));
|
|
1641
|
+
if (namedItems.some((item, i) => item !== tupleNode.items[i])) schemaNode = {
|
|
1642
|
+
...tupleNode,
|
|
1643
|
+
items: namedItems
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1586
1646
|
return (0, _kubb_ast.createProperty)({
|
|
1587
1647
|
name: propName,
|
|
1588
1648
|
schema: {
|
|
1589
|
-
...
|
|
1590
|
-
|
|
1591
|
-
name: resolveChildName(name, propName)
|
|
1592
|
-
}, options), name, propName, mergedOptions.enumSuffix),
|
|
1593
|
-
nullable: propNullable || void 0,
|
|
1649
|
+
...schemaNode,
|
|
1650
|
+
nullable: schemaNode.type === "null" ? void 0 : propNullable || void 0,
|
|
1594
1651
|
optional: !required && !propNullable ? true : void 0,
|
|
1595
1652
|
nullish: !required && propNullable ? true : void 0
|
|
1596
1653
|
},
|
|
@@ -1627,15 +1684,17 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1627
1684
|
/**
|
|
1628
1685
|
* Converts an OAS 3.1 `prefixItems` tuple into a `TupleSchemaNode`.
|
|
1629
1686
|
*
|
|
1630
|
-
* Each `prefixItems` element maps to a positional tuple slot.
|
|
1631
|
-
*
|
|
1687
|
+
* Each `prefixItems` element maps to a positional tuple slot. When an explicit `items` schema
|
|
1688
|
+
* is present alongside `prefixItems`, it becomes the rest element. When `items` is absent,
|
|
1689
|
+
* a rest element of type `any` is emitted to match JSON Schema semantics (absent `items`
|
|
1690
|
+
* means additional items are allowed).
|
|
1632
1691
|
*/
|
|
1633
1692
|
function convertTuple({ schema, name, nullable, defaultValue, options }) {
|
|
1634
1693
|
return (0, _kubb_ast.createSchema)({
|
|
1635
1694
|
type: "tuple",
|
|
1636
1695
|
primitive: "array",
|
|
1637
1696
|
items: (schema.prefixItems ?? []).map((item) => convertSchema({ schema: item }, options)),
|
|
1638
|
-
rest: schema.items ? convertSchema({ schema: schema.items }, options) :
|
|
1697
|
+
rest: schema.items ? convertSchema({ schema: schema.items }, options) : (0, _kubb_ast.createSchema)({ type: "any" }),
|
|
1639
1698
|
min: schema.minItems,
|
|
1640
1699
|
max: schema.maxItems,
|
|
1641
1700
|
...renderSchemaBase(schema, name, nullable, defaultValue)
|
|
@@ -1802,11 +1861,11 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1802
1861
|
}
|
|
1803
1862
|
/**
|
|
1804
1863
|
* Converts a single dereferenced OAS parameter object into a `ParameterNode`.
|
|
1805
|
-
* When the parameter has no `schema
|
|
1864
|
+
* When the parameter has no `schema`, falls back to `unknownType`; `$ref` schemas are resolved through `convertSchema` to produce a proper named type reference.
|
|
1806
1865
|
*/
|
|
1807
1866
|
function parseParameter(options, param) {
|
|
1808
1867
|
const required = param["required"] ?? false;
|
|
1809
|
-
const schema = param["schema"]
|
|
1868
|
+
const schema = param["schema"] ? convertSchema({ schema: param["schema"] }, options) : (0, _kubb_ast.createSchema)({ type: resolveTypeOption(options.unknownType) });
|
|
1810
1869
|
return (0, _kubb_ast.createParameter)({
|
|
1811
1870
|
name: param["name"],
|
|
1812
1871
|
in: param["in"],
|
|
@@ -1826,8 +1885,10 @@ function createOasParser(oas, { contentType, collisionDetection } = {}) {
|
|
|
1826
1885
|
const parameters = oas.getParameters(operation).map((param) => parseParameter(options, param));
|
|
1827
1886
|
const requestBodySchema = oas.getRequestSchema(operation);
|
|
1828
1887
|
const requestBodySchemaNode = requestBodySchema ? convertSchema({ schema: requestBodySchema }, options) : void 0;
|
|
1888
|
+
const requestBodyDescription = operation.schema.requestBody && !isReference(operation.schema.requestBody) ? operation.schema.requestBody.description : void 0;
|
|
1829
1889
|
const requestBodyKeysToOmit = requestBodySchema?.properties ? Object.entries(requestBodySchema.properties).filter(([, prop]) => !isReference(prop) && prop.readOnly).map(([key]) => key) : void 0;
|
|
1830
1890
|
const requestBody = requestBodySchemaNode ? {
|
|
1891
|
+
description: requestBodyDescription,
|
|
1831
1892
|
schema: requestBodySchemaNode,
|
|
1832
1893
|
keysToOmit: requestBodyKeysToOmit?.length ? requestBodyKeysToOmit : void 0
|
|
1833
1894
|
} : void 0;
|
|
@@ -1978,16 +2039,17 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
|
|
|
1978
2039
|
contentType,
|
|
1979
2040
|
collisionDetection
|
|
1980
2041
|
});
|
|
2042
|
+
const root = parser.parse({
|
|
2043
|
+
dateType,
|
|
2044
|
+
integerType,
|
|
2045
|
+
unknownType,
|
|
2046
|
+
emptySchemaType,
|
|
2047
|
+
enumSuffix
|
|
2048
|
+
});
|
|
1981
2049
|
nameMapping.clear();
|
|
1982
2050
|
for (const [key, value] of parser.nameMapping) nameMapping.set(key, value);
|
|
1983
2051
|
return (0, _kubb_ast.createRoot)({
|
|
1984
|
-
...
|
|
1985
|
-
dateType,
|
|
1986
|
-
integerType,
|
|
1987
|
-
unknownType,
|
|
1988
|
-
emptySchemaType,
|
|
1989
|
-
enumSuffix
|
|
1990
|
-
}),
|
|
2052
|
+
...root,
|
|
1991
2053
|
meta: {
|
|
1992
2054
|
title: oas.api.info?.title,
|
|
1993
2055
|
description: oas.api.info?.description,
|