Skip to content

Commit 134a3f9

Browse files
committed
fix: prevent infinite recursion for circular tuple array schemas
1 parent 52f4aae commit 134a3f9

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

src/processors/AsyncAPIInputProcessor.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,20 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
195195
// eslint-disable-next-line sonarjs/cognitive-complexity
196196
static convertToInternalSchema(
197197
schema: AsyncAPISchemaInterface | boolean,
198-
alreadyIteratedSchemas: Map<string, AsyncapiV2Schema> = new Map()
198+
alreadyIteratedSchemas: Map<string, AsyncapiV2Schema> = new Map(),
199+
visitedSchemas: WeakSet<object> = new WeakSet()
199200
): AsyncapiV2Schema | boolean {
200201
if (typeof schema === 'boolean') {
201202
return schema;
202203
}
203204

205+
if (typeof schema === 'object') {
206+
if (visitedSchemas.has(schema)) {
207+
return {};
208+
}
209+
visitedSchemas.add(schema);
210+
}
211+
204212
let schemaUid = schema.id();
205213
//Because the constraint functionality of generators cannot handle -, <, >, we remove them from the id if it's an anonymous schema.
206214
if (
@@ -225,27 +233,28 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
225233
convertedSchema.allOf = schema
226234
.allOf()!
227235
.map((item: any) =>
228-
this.convertToInternalSchema(item, alreadyIteratedSchemas)
236+
this.convertToInternalSchema(item, alreadyIteratedSchemas, visitedSchemas)
229237
);
230238
}
231239
if (schema.oneOf()) {
232240
convertedSchema.oneOf = schema
233241
.oneOf()!
234242
.map((item: any) =>
235-
this.convertToInternalSchema(item, alreadyIteratedSchemas)
243+
this.convertToInternalSchema(item, alreadyIteratedSchemas, visitedSchemas)
236244
);
237245
}
238246
if (schema.anyOf()) {
239247
convertedSchema.anyOf = schema
240248
.anyOf()!
241249
.map((item: any) =>
242-
this.convertToInternalSchema(item, alreadyIteratedSchemas)
250+
this.convertToInternalSchema(item, alreadyIteratedSchemas, visitedSchemas)
243251
);
244252
}
245253
if (schema.not()) {
246254
convertedSchema.not = this.convertToInternalSchema(
247255
schema.not()!,
248-
alreadyIteratedSchemas
256+
alreadyIteratedSchemas,
257+
visitedSchemas
249258
);
250259
}
251260
if (
@@ -254,37 +263,43 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
254263
) {
255264
convertedSchema.additionalItems = this.convertToInternalSchema(
256265
schema.additionalItems(),
257-
alreadyIteratedSchemas
266+
alreadyIteratedSchemas,
267+
visitedSchemas
258268
);
259269
}
260270
if (schema.contains()) {
261271
convertedSchema.contains = this.convertToInternalSchema(
262272
schema.contains()!,
263-
alreadyIteratedSchemas
273+
alreadyIteratedSchemas,
274+
visitedSchemas
264275
);
265276
}
266277
if (schema.propertyNames()) {
267278
convertedSchema.propertyNames = this.convertToInternalSchema(
268279
schema.propertyNames()!,
269-
alreadyIteratedSchemas
280+
alreadyIteratedSchemas,
281+
visitedSchemas
270282
);
271283
}
272284
if (schema.if()) {
273285
convertedSchema.if = this.convertToInternalSchema(
274286
schema.if()!,
275-
alreadyIteratedSchemas
287+
alreadyIteratedSchemas,
288+
visitedSchemas
276289
);
277290
}
278291
if (schema.then()) {
279292
convertedSchema.then = this.convertToInternalSchema(
280293
schema.then()!,
281-
alreadyIteratedSchemas
294+
alreadyIteratedSchemas,
295+
visitedSchemas
282296
);
283297
}
284298
if (schema.else()) {
285299
convertedSchema.else = this.convertToInternalSchema(
286300
schema.else()!,
287-
alreadyIteratedSchemas
301+
alreadyIteratedSchemas,
302+
visitedSchemas
288303
);
289304
}
290305
if (
@@ -293,21 +308,23 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
293308
) {
294309
convertedSchema.additionalProperties = this.convertToInternalSchema(
295310
schema.additionalProperties(),
296-
alreadyIteratedSchemas
311+
alreadyIteratedSchemas,
312+
visitedSchemas
297313
);
298314
}
299315
if (schema.items()) {
300316
if (Array.isArray(schema.items())) {
301317
convertedSchema.items = (
302318
schema.items() as AsyncAPISchemaInterface[]
303319
).map(
304-
(item) => this.convertToInternalSchema(item),
320+
(item) => this.convertToInternalSchema(item, alreadyIteratedSchemas, visitedSchemas),
305321
alreadyIteratedSchemas
306322
);
307323
} else {
308324
convertedSchema.items = this.convertToInternalSchema(
309325
schema.items() as AsyncAPISchemaInterface,
310-
alreadyIteratedSchemas
326+
alreadyIteratedSchemas,
327+
visitedSchemas
311328
);
312329
}
313330
}
@@ -320,7 +337,8 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
320337
)) {
321338
properties[String(propertyName)] = this.convertToInternalSchema(
322339
propertySchema,
323-
alreadyIteratedSchemas
340+
alreadyIteratedSchemas,
341+
visitedSchemas
324342
);
325343
}
326344
convertedSchema.properties = properties;
@@ -337,7 +355,8 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
337355
if (typeof dependency === 'object' && !Array.isArray(dependency)) {
338356
dependencies[String(dependencyName)] = this.convertToInternalSchema(
339357
dependency,
340-
alreadyIteratedSchemas
358+
alreadyIteratedSchemas,
359+
visitedSchemas
341360
);
342361
} else {
343362
dependencies[String(dependencyName)] = dependency;
@@ -357,7 +376,7 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
357376
schemaPatternProperties
358377
)) {
359378
patternProperties[String(patternPropertyName)] =
360-
this.convertToInternalSchema(patternProperty, alreadyIteratedSchemas);
379+
this.convertToInternalSchema(patternProperty, alreadyIteratedSchemas, visitedSchemas);
361380
}
362381
convertedSchema.patternProperties = patternProperties;
363382
}
@@ -370,7 +389,8 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
370389
)) {
371390
definitions[String(definitionName)] = this.convertToInternalSchema(
372391
definition,
373-
alreadyIteratedSchemas
392+
alreadyIteratedSchemas,
393+
visitedSchemas
374394
);
375395
}
376396
convertedSchema.definitions = definitions;

0 commit comments

Comments
 (0)