Skip to content

Commit 042af17

Browse files
authored
fix(specmap): avoid traversal limit for non-complex huge definitions (#3399)
Refs swagger-api/swagger-ui#9645 Refs #3385
1 parent 9358552 commit 042af17

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/specmap/index.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import properties from './lib/properties.js';
66
import ContextTree from './lib/context-tree.js';
77

88
const PLUGIN_DISPATCH_LIMIT = 100;
9-
const TRAVERSE_LIMIT = 1000;
9+
const TRAVERSE_LIMIT = 3000;
1010
const noop = () => {};
1111

1212
class SpecMap {
@@ -40,7 +40,6 @@ class SpecMap {
4040
getInstance: () => this,
4141
}),
4242
allowMetaPatches: false,
43-
currentTraverseCount: 0,
4443
},
4544
opts
4645
);
@@ -72,7 +71,6 @@ class SpecMap {
7271

7372
wrapPlugin(plugin, name) {
7473
const { pathDiscriminator } = this;
75-
const that = this;
7674
let ctx = null;
7775
let fn;
7876

@@ -107,16 +105,15 @@ class SpecMap {
107105
const refCache = {};
108106

109107
// eslint-disable-next-line no-restricted-syntax
110-
for (const patch of patches.filter(lib.isAdditiveMutation)) {
111-
if (that.currentTraverseCount < TRAVERSE_LIMIT) {
108+
for (const [i, patch] of patches.filter(lib.isAdditiveMutation).entries()) {
109+
if (i < TRAVERSE_LIMIT) {
112110
yield* traverse(patch.value, patch.path, patch);
113111
} else {
114112
return;
115113
}
116114
}
117115

118116
function* traverse(obj, path, patch) {
119-
that.currentTraverseCount += 1;
120117
if (!lib.isObject(obj)) {
121118
if (pluginObj.key === path[path.length - 1]) {
122119
yield pluginObj.plugin(obj, pluginObj.key, path, specmap);
@@ -142,11 +139,7 @@ class SpecMap {
142139
if (specmap.allowMetaPatches && objRef) {
143140
refCache[objRef] = true;
144141
}
145-
if (that.currentTraverseCount < TRAVERSE_LIMIT) {
146-
yield* traverse(val, updatedPath, patch);
147-
} else {
148-
return;
149-
}
142+
yield* traverse(val, updatedPath, patch);
150143
}
151144
}
152145

@@ -325,8 +318,6 @@ class SpecMap {
325318
const that = this;
326319
const plugin = this.nextPlugin();
327320

328-
that.currentTraverseCount = 0;
329-
330321
if (!plugin) {
331322
const nextPromise = this.nextPromisedPatch();
332323
if (nextPromise) {

test/specmap/complex.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('complex', () => {
3636
const startTime = new Date();
3737
console.log('Run', spec.name); // eslint-disable-line no-console
3838

39-
return mapSpec({ spec: spec.spec, plugins: [refs, allOf] })
39+
return mapSpec({ spec: spec.spec, plugins: [refs, allOf], allowMetaPatches: true })
4040
.then((res) => {
4141
if (res.errors.length) throw res.errors[0];
4242
expect(res.errors.length).toEqual(0);
@@ -64,15 +64,17 @@ describe('complex', () => {
6464
expect(
6565
result.spec.components.schemas[
6666
'com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.StudyTreatments-create'
67-
].properties.scenario.allOf[0].$ref
67+
].properties.scenario.$ref
6868
).toEqual(
6969
'#/components/schemas/com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.Scenarios-create'
7070
);
7171

7272
expect(
7373
result.spec.components.schemas[
7474
'com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.BlindingGroups'
75-
].properties.study.properties.scenarios.items.$$ref
76-
).toEqual('#/components/schemas/com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.Scenarios');
75+
].properties.study.properties.materials.items.$$ref
76+
).toEqual(
77+
'#/components/schemas/com.sap.ctsm.backend.core.api.study.v1.StudyAPIv1.StudyMaterials'
78+
);
7779
});
7880
});

0 commit comments

Comments
 (0)