@kubb/adapter-oas 5.0.0-beta.10 → 5.0.0-beta.12

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
@@ -1211,6 +1211,19 @@ function createSchemaParser(ctx) {
1211
1211
  */
1212
1212
  const resolvingRefs = /* @__PURE__ */ new Set();
1213
1213
  /**
1214
+ * Cache of already-resolved `$ref` schemas within this parser instance.
1215
+ *
1216
+ * Without this, the same referenced schema (e.g. `customer`) is fully re-expanded
1217
+ * every time it appears as a `$ref` in a different parent schema. In heavily
1218
+ * cross-referenced specs like Stripe (~1 400 schemas), this causes exponential
1219
+ * blowup — `customer` alone may be referenced from dozens of top-level schemas,
1220
+ * each triggering a fresh recursive expansion of its entire sub-tree.
1221
+ *
1222
+ * Memoising by `$ref` path reduces the overall work from O(2^depth) to O(N)
1223
+ * where N is the number of unique schema names.
1224
+ */
1225
+ const resolvedRefCache = /* @__PURE__ */ new Map();
1226
+ /**
1214
1227
  * Converts a `$ref` schema into a `RefSchemaNode`.
1215
1228
  *
1216
1229
  * The resolved schema is stored in `node.schema`. Usage-site sibling fields
@@ -1221,14 +1234,18 @@ function createSchemaParser(ctx) {
1221
1234
  function convertRef({ schema, name, nullable, defaultValue, rawOptions }) {
1222
1235
  let resolvedSchema;
1223
1236
  const refPath = schema.$ref;
1224
- if (refPath && !resolvingRefs.has(refPath)) try {
1225
- const referenced = resolveRef(document, refPath);
1226
- if (referenced) {
1227
- resolvingRefs.add(refPath);
1228
- resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1229
- resolvingRefs.delete(refPath);
1230
- }
1231
- } catch {}
1237
+ if (refPath && !resolvingRefs.has(refPath)) if (resolvedRefCache.has(refPath)) resolvedSchema = resolvedRefCache.get(refPath);
1238
+ else {
1239
+ try {
1240
+ const referenced = resolveRef(document, refPath);
1241
+ if (referenced) {
1242
+ resolvingRefs.add(refPath);
1243
+ resolvedSchema = parseSchema({ schema: referenced }, rawOptions);
1244
+ resolvingRefs.delete(refPath);
1245
+ }
1246
+ } catch {}
1247
+ resolvedRefCache.set(refPath, resolvedSchema);
1248
+ }
1232
1249
  return _kubb_core.ast.createSchema({
1233
1250
  ...buildSchemaNode(schema, name, nullable, defaultValue),
1234
1251
  type: "ref",
@@ -1929,7 +1946,6 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
1929
1946
  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;
1930
1947
  let nameMapping = /* @__PURE__ */ new Map();
1931
1948
  let parsedDocument;
1932
- let inputNode;
1933
1949
  return {
1934
1950
  name: "oas",
1935
1951
  get options() {
@@ -1950,9 +1966,6 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
1950
1966
  get document() {
1951
1967
  return parsedDocument;
1952
1968
  },
1953
- get inputNode() {
1954
- return inputNode;
1955
- },
1956
1969
  async validate(input, options) {
1957
1970
  await validateDocument(await parseDocument(input), options);
1958
1971
  },
@@ -1986,7 +1999,7 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
1986
1999
  const node = discriminator === "inherit" ? applyDiscriminatorInheritance(parsedRoot) : parsedRoot;
1987
2000
  nameMapping = parsedNameMapping;
1988
2001
  parsedDocument = document;
1989
- inputNode = _kubb_core.ast.createInput({
2002
+ return _kubb_core.ast.createInput({
1990
2003
  ...node,
1991
2004
  meta: {
1992
2005
  title: document.info?.title,
@@ -1995,7 +2008,6 @@ const adapterOas = (0, _kubb_core.createAdapter)((options) => {
1995
2008
  baseURL
1996
2009
  }
1997
2010
  });
1998
- return inputNode;
1999
2011
  }
2000
2012
  };
2001
2013
  });