@kubb/adapter-oas 5.4.3 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1274,6 +1274,73 @@ function extractDiscriminatedAllOfMembers({ allOfMembers, name, refs }) {
1274
1274
  };
1275
1275
  }
1276
1276
  //#endregion
1277
+ //#region src/emit/converters/annotatedEnum.ts
1278
+ const ANNOTATIONS = /* @__PURE__ */ new Set([
1279
+ "title",
1280
+ "description",
1281
+ "default",
1282
+ "deprecated",
1283
+ "readOnly",
1284
+ "writeOnly",
1285
+ "example",
1286
+ "examples",
1287
+ "xml",
1288
+ "externalDocs"
1289
+ ]);
1290
+ const UNION_KEYS = /* @__PURE__ */ new Set([
1291
+ ...ANNOTATIONS,
1292
+ "type",
1293
+ "nullable",
1294
+ "oneOf",
1295
+ "anyOf"
1296
+ ]);
1297
+ const MEMBER_KEYS = /* @__PURE__ */ new Set([
1298
+ ...ANNOTATIONS,
1299
+ "type",
1300
+ "const"
1301
+ ]);
1302
+ function hasOnlyKeys(schema, keys) {
1303
+ return typeof schema === "object" && schema !== null && !Array.isArray(schema) && Object.keys(schema).every((key) => keys.has(key) || key.startsWith("x-"));
1304
+ }
1305
+ function matchesType(type, value) {
1306
+ return type === void 0 || type === typeof value || type === "integer" && typeof value === "number" && Number.isInteger(value);
1307
+ }
1308
+ /** Recognizes annotated const unions only when replacing them cannot discard validation constraints. */
1309
+ function convertAnnotatedEnum(context) {
1310
+ const { schema, document } = context;
1311
+ if (!document.openapi?.startsWith("3.1.")) return void 0;
1312
+ if (!hasOnlyKeys(schema, UNION_KEYS) || schema.oneOf !== void 0 && schema.anyOf !== void 0) return void 0;
1313
+ const members = schema.oneOf ?? schema.anyOf;
1314
+ if (!members?.length) return void 0;
1315
+ const names = /* @__PURE__ */ new Set();
1316
+ const values = /* @__PURE__ */ new Set();
1317
+ const namedEnumValues = [];
1318
+ let primitive;
1319
+ for (const member of members) {
1320
+ if (!hasOnlyKeys(member, MEMBER_KEYS)) return void 0;
1321
+ const value = member.const;
1322
+ if (typeof value !== "string" && (typeof value !== "number" || !Number.isFinite(value))) return void 0;
1323
+ if (typeof member.title !== "string" || !member.title.trim() || names.has(member.title) || values.has(value)) return void 0;
1324
+ if (!matchesType(schema.type, value) || !matchesType(member.type, value)) return void 0;
1325
+ const valueType = typeof value === "number" ? "number" : "string";
1326
+ if (primitive !== void 0 && primitive !== valueType) return void 0;
1327
+ primitive = valueType;
1328
+ names.add(member.title);
1329
+ values.add(value);
1330
+ namedEnumValues.push({
1331
+ name: member.title,
1332
+ value,
1333
+ primitive: valueType,
1334
+ description: member.description
1335
+ });
1336
+ }
1337
+ return createNode(context, {
1338
+ type: "enum",
1339
+ primitive,
1340
+ namedEnumValues
1341
+ });
1342
+ }
1343
+ //#endregion
1277
1344
  //#region src/emit/converters/composition.ts
1278
1345
  /**
1279
1346
  * Converts a `$ref` schema into a `RefSchemaNode`.
@@ -1386,7 +1453,10 @@ function convertAllOf({ schema, name, nullable, defaultValue, rawOptions, parse,
1386
1453
  /**
1387
1454
  * Converts a `oneOf` / `anyOf` schema into a `UnionSchemaNode`.
1388
1455
  */
1389
- function convertUnion({ schema, name, nullable, defaultValue, rawOptions, parse, refs }) {
1456
+ function convertUnion(context) {
1457
+ const annotatedEnum = convertAnnotatedEnum(context);
1458
+ if (annotatedEnum) return annotatedEnum;
1459
+ const { schema, name, nullable, defaultValue, rawOptions, parse, refs } = context;
1390
1460
  const ctx = {
1391
1461
  schema,
1392
1462
  name,
@@ -1539,9 +1609,7 @@ function convertFormat(context) {
1539
1609
  if (schema.format === "int64" || schema.format === "uint64") return createNode(ctx, {
1540
1610
  type: options.integerType === "bigint" ? "bigint" : "integer",
1541
1611
  primitive: "integer",
1542
- min: schema.minimum,
1543
- max: schema.maximum,
1544
- ...getExclusiveBounds(schema)
1612
+ ...getNumericConstraints(schema)
1545
1613
  });
1546
1614
  if (schema.format === "date-time" || schema.format === "date" || schema.format === "time") {
1547
1615
  const dateType = getDateType(options, schema.format);
@@ -1558,13 +1626,15 @@ function convertFormat(context) {
1558
1626
  });
1559
1627
  }
1560
1628
  const specialType = getSchemaType(schema.format);
1629
+ const isNumeric = specialType === "number" || specialType === "integer" || specialType === "bigint";
1561
1630
  return createNode(ctx, {
1562
- primitive: specialType === "number" || specialType === "integer" || specialType === "bigint" ? specialType : "string",
1631
+ primitive: isNumeric ? specialType : "string",
1563
1632
  type: specialType,
1564
1633
  ...specialType === "url" || specialType === "uuid" || specialType === "email" ? {
1565
1634
  min: schema.minLength,
1566
1635
  max: schema.maxLength
1567
- } : {}
1636
+ } : {},
1637
+ ...isNumeric ? getNumericConstraints(schema) : {}
1568
1638
  });
1569
1639
  }
1570
1640
  /**
@@ -1641,6 +1711,18 @@ function convertString({ schema, name, nullable, defaultValue }) {
1641
1711
  });
1642
1712
  }
1643
1713
  /**
1714
+ * Reads the numeric constraints shared by every `number`, `integer` and `bigint` node. A `format`
1715
+ * such as `double` or `int32` only narrows the type, so formatted numbers keep these too.
1716
+ */
1717
+ function getNumericConstraints(schema) {
1718
+ return {
1719
+ min: schema.minimum,
1720
+ max: schema.maximum,
1721
+ ...getExclusiveBounds(schema),
1722
+ multipleOf: schema.multipleOf
1723
+ };
1724
+ }
1725
+ /**
1644
1726
  * Converts a `type: 'number'` or `type: 'integer'` schema.
1645
1727
  */
1646
1728
  function convertNumeric({ schema, name, nullable, defaultValue }, type) {
@@ -1652,10 +1734,7 @@ function convertNumeric({ schema, name, nullable, defaultValue }, type) {
1652
1734
  }, {
1653
1735
  type,
1654
1736
  primitive: type,
1655
- min: schema.minimum,
1656
- max: schema.maximum,
1657
- ...getExclusiveBounds(schema),
1658
- multipleOf: schema.multipleOf
1737
+ ...getNumericConstraints(schema)
1659
1738
  });
1660
1739
  }
1661
1740
  /**