Skip to content

Commit

Permalink
fix(specmap): avoid traversal limit for non-complex huge definitions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud authored Mar 1, 2024
1 parent 9358552 commit 042af17
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
17 changes: 4 additions & 13 deletions src/specmap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import properties from './lib/properties.js';
import ContextTree from './lib/context-tree.js';

const PLUGIN_DISPATCH_LIMIT = 100;
const TRAVERSE_LIMIT = 1000;
const TRAVERSE_LIMIT = 3000;
const noop = () => {};

class SpecMap {
Expand Down Expand Up @@ -40,7 +40,6 @@ class SpecMap {
getInstance: () => this,
}),
allowMetaPatches: false,
currentTraverseCount: 0,
},
opts
);
Expand Down Expand Up @@ -72,7 +71,6 @@ class SpecMap {

wrapPlugin(plugin, name) {
const { pathDiscriminator } = this;
const that = this;
let ctx = null;
let fn;

Expand Down Expand Up @@ -107,16 +105,15 @@ class SpecMap {
const refCache = {};

// eslint-disable-next-line no-restricted-syntax
for (const patch of patches.filter(lib.isAdditiveMutation)) {
if (that.currentTraverseCount < TRAVERSE_LIMIT) {
for (const [i, patch] of patches.filter(lib.isAdditiveMutation).entries()) {
if (i < TRAVERSE_LIMIT) {
yield* traverse(patch.value, patch.path, patch);
} else {
return;
}
}

function* traverse(obj, path, patch) {
that.currentTraverseCount += 1;
if (!lib.isObject(obj)) {
if (pluginObj.key === path[path.length - 1]) {
yield pluginObj.plugin(obj, pluginObj.key, path, specmap);
Expand All @@ -142,11 +139,7 @@ class SpecMap {
if (specmap.allowMetaPatches && objRef) {
refCache[objRef] = true;
}
if (that.currentTraverseCount < TRAVERSE_LIMIT) {
yield* traverse(val, updatedPath, patch);
} else {
return;
}
yield* traverse(val, updatedPath, patch);
}
}

Expand Down Expand Up @@ -325,8 +318,6 @@ class SpecMap {
const that = this;
const plugin = this.nextPlugin();

that.currentTraverseCount = 0;

if (!plugin) {
const nextPromise = this.nextPromisedPatch();
if (nextPromise) {
Expand Down
10 changes: 6 additions & 4 deletions test/specmap/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('complex', () => {
const startTime = new Date();
console.log('Run', spec.name); // eslint-disable-line no-console

return mapSpec({ spec: spec.spec, plugins: [refs, allOf] })
return mapSpec({ spec: spec.spec, plugins: [refs, allOf], allowMetaPatches: true })
.then((res) => {
if (res.errors.length) throw res.errors[0];
expect(res.errors.length).toEqual(0);
Expand Down Expand Up @@ -64,15 +64,17 @@ describe('complex', () => {
expect(
result.spec.components.schemas[
'com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.StudyTreatments-create'
].properties.scenario.allOf[0].$ref
].properties.scenario.$ref
).toEqual(
'#/components/schemas/com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.Scenarios-create'
);

expect(
result.spec.components.schemas[
'com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.BlindingGroups'
].properties.study.properties.scenarios.items.$$ref
).toEqual('#/components/schemas/com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.Scenarios');
].properties.study.properties.materials.items.$$ref
).toEqual(
'#/components/schemas/com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.StudyMaterials'
);
});
});

0 comments on commit 042af17

Please sign in to comment.