@kubb/adapter-oas 5.0.0-beta.11 → 5.0.0-beta.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.js CHANGED
@@ -1185,6 +1185,19 @@ function createSchemaParser(ctx) {
1185
1185
  */
1186
1186
  const resolvingRefs = /* @__PURE__ */ new Set();
1187
1187
  /**
1188
+ * Cache of already-resolved `$ref` schemas within this parser instance.
1189
+ *
1190
+ * Without this, the same referenced schema (e.g. `customer`) is fully re-expanded
1191
+ * every time it appears as a `$ref` in a different parent schema. In heavily
1192
+ * cross-referenced specs like Stripe (~1 400 schemas), this causes exponential
1193
+ * blowup — `customer` alone may be referenced from dozens of top-level schemas,
1194
+ * each triggering a fresh recursive expansion of its entire sub-tree.
1195
+ *
1196
+ * Memoising by `$ref` path reduces the overall work from O(2^depth) to O(N)
1197
+ * where N is the number of unique schema names.
1198
+ */
1199
+ const resolvedRefCache = /* @__PURE__ */ new Map();
1200
+ /**
1188
1201
  * Converts a `$ref` schema into a `RefSchemaNode`.
1189
1202
  *
1190
1203
  * The resolved schema is stored in `node.schema`. Usage-site sibling fields
@@ -1195,14 +1208,18 @@ function createSchemaParser(ctx) {
1195
1208
  function convertRef({ schema, name, nullable, defaultValue, rawOptions }) {
1196
1209
  let resolvedSchema;
1197
1210
  const refPath = schema.$ref;
1198
- if (refPath && !resolvingRefs.has(refPath)) try {
1199
- const referenced = resolveRef(document, refPath);
1200
- if (referenced) {
1201
- resolvingRefs.add(refPath);
1202
- resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1203
- resolvingRefs.delete(refPath);
1204
- }
1205
- } catch {}
1211
+ if (refPath && !resolvingRefs.has(refPath)) if (resolvedRefCache.has(refPath)) resolvedSchema = resolvedRefCache.get(refPath);
1212
+ else {
1213
+ try {
1214
+ const referenced = resolveRef(document, refPath);
1215
+ if (referenced) {
1216
+ resolvingRefs.add(refPath);
1217
+ resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1218
+ resolvingRefs.delete(refPath);
1219
+ }
1220
+ } catch {}
1221
+ resolvedRefCache.set(refPath, resolvedSchema);
1222
+ }
1206
1223
  return ast.createSchema({
1207
1224
  ...buildSchemaNode(schema, name, nullable, defaultValue),
1208
1225
  type: "ref",
@@ -1903,7 +1920,6 @@ const adapterOas = createAdapter((options) => {
1903
1920
  const { validate = true, contentType, serverIndex, serverVariables, discriminator = "strict", dateType = DEFAULT_PARSER_OPTIONS.dateType, integerType = DEFAULT_PARSER_OPTIONS.integerType, unknownType = DEFAULT_PARSER_OPTIONS.unknownType, enumSuffix = DEFAULT_PARSER_OPTIONS.enumSuffix, emptySchemaType = unknownType || DEFAULT_PARSER_OPTIONS.emptySchemaType } = options;
1904
1921
  let nameMapping = /* @__PURE__ */ new Map();
1905
1922
  let parsedDocument;
1906
- let inputNode;
1907
1923
  return {
1908
1924
  name: "oas",
1909
1925
  get options() {
@@ -1924,9 +1940,6 @@ const adapterOas = createAdapter((options) => {
1924
1940
  get document() {
1925
1941
  return parsedDocument;
1926
1942
  },
1927
- get inputNode() {
1928
- return inputNode;
1929
- },
1930
1943
  async validate(input, options) {
1931
1944
  await validateDocument(await parseDocument(input), options);
1932
1945
  },
@@ -1960,7 +1973,7 @@ const adapterOas = createAdapter((options) => {
1960
1973
  const node = discriminator === "inherit" ? applyDiscriminatorInheritance(parsedRoot) : parsedRoot;
1961
1974
  nameMapping = parsedNameMapping;
1962
1975
  parsedDocument = document;
1963
- inputNode = ast.createInput({
1976
+ return ast.createInput({
1964
1977
  ...node,
1965
1978
  meta: {
1966
1979
  title: document.info?.title,
@@ -1969,7 +1982,6 @@ const adapterOas = createAdapter((options) => {
1969
1982
  baseURL
1970
1983
  }
1971
1984
  });
1972
- return inputNode;
1973
1985
  }
1974
1986
  };
1975
1987
  });