@kubb/adapter-oas 5.4.5 → 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,