@kubb/adapter-oas 5.0.0-beta.76 → 5.0.0-beta.78

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.js CHANGED
@@ -42,7 +42,7 @@ const SCHEMA_REF_PREFIX = "#/components/schemas/";
42
42
  * HTTP methods that count as operations on an OpenAPI path item. Other keys
43
43
  * (`parameters`, `summary`, `$ref`, vendor extensions) are skipped when iterating operations.
44
44
  */
45
- const SUPPORTED_METHODS = new Set([
45
+ const SUPPORTED_METHODS = /* @__PURE__ */ new Set([
46
46
  "get",
47
47
  "put",
48
48
  "post",
@@ -66,7 +66,7 @@ const SUPPORTED_METHODS = new Set([
66
66
  * // true when fragment has e.g. 'properties' or 'oneOf'
67
67
  * ```
68
68
  */
69
- const structuralKeys = new Set([
69
+ const structuralKeys = /* @__PURE__ */ new Set([
70
70
  "properties",
71
71
  "items",
72
72
  "additionalProperties",
@@ -81,7 +81,7 @@ const structuralKeys = new Set([
81
81
  * special-cases in `parser.ts`. `isHandledFormat` reads it so the
82
82
  * `KUBB_UNSUPPORTED_FORMAT` diagnostic and the parser agree on what is handled.
83
83
  */
84
- const specialCasedFormats = new Set([
84
+ const specialCasedFormats = /* @__PURE__ */ new Set([
85
85
  "int64",
86
86
  "date-time",
87
87
  "date",
@@ -991,8 +991,8 @@ function flattenSchema(schema) {
991
991
  const allOfFragments = schema.allOf;
992
992
  if (allOfFragments.some((item) => isReference(item))) return schema;
993
993
  if (allOfFragments.some(hasStructuralKeywords)) return schema;
994
- const merged = { ...schema };
995
- delete merged.allOf;
994
+ const { allOf: _allOf, ...rest } = schema;
995
+ const merged = rest;
996
996
  for (const fragment of allOfFragments) for (const [key, value] of Object.entries(fragment)) if (merged[key] === void 0) merged[key] = value;
997
997
  return merged;
998
998
  }
@@ -1118,7 +1118,7 @@ function getSchemas(document, { contentType }) {
1118
1118
  let hasMultipleSources = false;
1119
1119
  if (!isSingle) {
1120
1120
  const firstSource = items[0].source;
1121
- for (let i = 1; i < items.length; i++) if (items[i].source !== firstSource) {
1121
+ for (const item of items) if (item.source !== firstSource) {
1122
1122
  hasMultipleSources = true;
1123
1123
  break;
1124
1124
  }
@@ -1316,6 +1316,24 @@ function createSchemaParser(ctx, dialect = oasDialect) {
1316
1316
  */
1317
1317
  const resolvedRefCache = /* @__PURE__ */ new Map();
1318
1318
  /**
1319
+ * Memoized record of whether a `$ref` path resolves to a node the document actually defines.
1320
+ * A circular ref still resolves to an existing target, so this stays `true` for cycles and only
1321
+ * goes `false` for a `$ref` that points at a component the spec never declares.
1322
+ */
1323
+ const refExistence = /* @__PURE__ */ new Map();
1324
+ function refExists(refPath) {
1325
+ if (!refExistence.has(refPath)) {
1326
+ let exists = false;
1327
+ try {
1328
+ exists = !!dialect.schema.resolveRef(document, refPath);
1329
+ } catch {
1330
+ exists = false;
1331
+ }
1332
+ refExistence.set(refPath, exists);
1333
+ }
1334
+ return refExistence.get(refPath) ?? false;
1335
+ }
1336
+ /**
1319
1337
  * Converts a `$ref` schema into a `RefSchemaNode`.
1320
1338
  *
1321
1339
  * The resolved schema is stored in `node.schema`. Usage-site sibling fields
@@ -1340,6 +1358,10 @@ function createSchemaParser(ctx, dialect = oasDialect) {
1340
1358
  }
1341
1359
  resolvedSchema = resolvedRefCache.get(refPath) ?? null;
1342
1360
  }
1361
+ if (refPath && document.components && !refExists(refPath)) return ast.factory.createSchema({
1362
+ ...buildSchemaNode(schema, name, nullable, defaultValue),
1363
+ type: "unknown"
1364
+ });
1343
1365
  return ast.factory.createSchema({
1344
1366
  ...buildSchemaNode(schema, name, nullable, defaultValue),
1345
1367
  type: "ref",
@@ -1459,7 +1481,7 @@ function createSchemaParser(ctx, dialect = oasDialect) {
1459
1481
  const variant = resolveRefSilent(member.$ref);
1460
1482
  if (!variant) return null;
1461
1483
  const propertyName = discriminator.propertyName;
1462
- const seen = new Set([member.$ref]);
1484
+ const seen = /* @__PURE__ */ new Set([member.$ref]);
1463
1485
  function constrains(v) {
1464
1486
  const prop = v.properties?.[propertyName];
1465
1487
  const resolved = prop && dialect.schema.isReference(prop) ? resolveRefSilent(prop.$ref) : prop;
@@ -2110,7 +2132,6 @@ function collectInlineEnums(roots, topLevelNames) {
2110
2132
  const isSchemaRoot = root.kind === "Schema";
2111
2133
  for (const node of ast.collect(root, { schema: (schemaNode) => schemaNode })) {
2112
2134
  if (node.type !== "enum" || !node.name) continue;
2113
- if ((node.namedEnumValues ?? node.enumValues ?? []).length === 1) continue;
2114
2135
  if (isSchemaRoot && node === root) continue;
2115
2136
  if (topLevelNames.has(node.name)) continue;
2116
2137
  if (!promoted.has(node.name)) promoted.set(node.name, {
@@ -2260,9 +2281,7 @@ function preScan({ schemas, parseSchema, parseOperation, document, parserOptions
2260
2281
  name
2261
2282
  });
2262
2283
  if (node.type === "ref" && node.name && node.name !== name) refAliasMap.set(name, node);
2263
- const enumNode = ast.narrowSchema(node, ast.schemaTypes.enum);
2264
- const isConstEnum = (enumNode?.namedEnumValues ?? enumNode?.enumValues ?? []).length === 1;
2265
- if (enumNode && node.name && !isConstEnum) enumNames.push(node.name);
2284
+ if (ast.narrowSchema(node, ast.schemaTypes.enum) && node.name) enumNames.push(node.name);
2266
2285
  if (discriminator === "propagate" && (schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName) discriminatorParentNodes.push(node);
2267
2286
  }
2268
2287
  const circularNames = [...findCircularSchemas(allNodes)];
@@ -2496,8 +2515,7 @@ const adapterOas = createAdapter((options) => {
2496
2515
  return collect(node, { schema(schemaNode) {
2497
2516
  const schemaRef = narrowSchema(schemaNode, "ref");
2498
2517
  if (!schemaRef?.ref) return null;
2499
- const rawName = extractRefName(schemaRef.ref);
2500
- const result = resolve(nameMapping.get(rawName) ?? rawName);
2518
+ const result = resolve(nameMapping.get(schemaRef.ref) ?? extractRefName(schemaRef.ref));
2501
2519
  if (!result) return null;
2502
2520
  return ast.factory.createImport({
2503
2521
  name: [result.name],