Skip to content

Commit 5290d0a

Browse files
committed
Make sure generating sample values does not go into infinite recursion
1 parent 3f64fd0 commit 5290d0a

3 files changed

Lines changed: 37 additions & 16 deletions

File tree

packages/flowtest-electron/src/utils/collection.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ const parseOpenAPISpec = (collection) => {
4242
if (request['parameters']) {
4343
let firstQueryParam = true;
4444
request['parameters'].map((value, _) => {
45-
// path parameters are included in url
46-
// handle multiple parameters
47-
// allow different type of variables in request node like string, int, array etc...
4845
if (value['in'] === 'query') {
4946
if (firstQueryParam) {
5047
url = url.concat(`?${value['name']}={{${value['name']}}}`);

packages/flowtest-electron/src/utils/collection.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ const userSchema = {
5454
pattern: '^[a-zA-Z0-9]{3,}$',
5555
example: 'user123',
5656
},
57+
interests: {
58+
type: 'array',
59+
items: {
60+
type: 'string',
61+
},
62+
example: ['coding', 'reading'],
63+
},
5764
},
5865
};
5966

packages/flowtest-electron/src/utils/generate-request-body.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
const generateRequestBodyExample = (schema) => {
1+
const generateRequestBodyExample = (schema, level = 0, context = { processedSchemas: new Set() }) => {
22
if (!schema) return {};
33

4+
if (schema.example !== undefined) {
5+
return schema.example;
6+
}
7+
48
if (schema.enum) {
59
return schema.example || schema.enum[0];
610
}
711

812
if (schema.oneOf) {
9-
return generateRequestBodyExample(schema.oneOf[0]);
13+
return generateRequestBodyExample(schema.oneOf[0], level + 1, context);
1014
}
1115

1216
if (schema.anyOf) {
13-
return generateRequestBodyExample(schema.anyOf[0]);
17+
return generateRequestBodyExample(schema.anyOf[0], level + 1, context);
1418
}
1519

1620
if (schema.allOf) {
17-
return generateAllOfExample(schema.allOf);
21+
return generateAllOfExample(schema.allOf, level + 1, context);
1822
}
1923

2024
switch (schema.type) {
2125
case 'object':
22-
return generateObjectExample(schema);
26+
return generateObjectExample(schema, level + 1, context);
2327
case 'array':
24-
return generateArrayExample(schema);
28+
return generateArrayExample(schema, level + 1, context);
2529
case 'string':
2630
return generateStringExample(schema);
2731
case 'integer':
@@ -35,33 +39,46 @@ const generateRequestBodyExample = (schema) => {
3539
}
3640
};
3741

38-
const generateAllOfExample = (schemas) => {
42+
const generateAllOfExample = (schemas, level, context) => {
3943
const example = {};
4044
schemas.forEach((subSchema) => {
41-
const subExample = generateRequestBodyExample(subSchema);
45+
const subExample = generateRequestBodyExample(subSchema, level, context);
4246
Object.assign(example, subExample);
4347
});
4448
return example;
4549
};
4650

47-
const generateObjectExample = (schema) => {
51+
const generateObjectExample = (schema, level, context) => {
52+
if (schema.example !== undefined) {
53+
return schema.example;
54+
}
55+
56+
if (context.processedSchemas.has(schema) && level > 1) {
57+
return {};
58+
}
59+
context.processedSchemas.add(schema);
60+
4861
const example = {};
4962
const properties = schema.properties || {};
5063

5164
for (const [key, propertySchema] of Object.entries(properties)) {
52-
example[key] = generateRequestBodyExample(propertySchema);
65+
example[key] = generateRequestBodyExample(propertySchema, level, context);
5366
}
5467

68+
context.processedSchemas.delete(schema);
5569
return example;
5670
};
5771

58-
const generateArrayExample = (schema) => {
72+
const generateArrayExample = (schema, level, context) => {
73+
if (schema.example !== undefined) {
74+
return schema.example;
75+
}
5976
const itemsSchema = schema.items || {};
60-
return [generateRequestBodyExample(itemsSchema)];
77+
return [generateRequestBodyExample(itemsSchema, level, context)];
6178
};
6279

6380
const generateStringExample = (schema) => {
64-
let example = schema.example || 'string';
81+
let example = String(schema.example || 'string');
6582

6683
if (schema.minLength || schema.maxLength) {
6784
example = generateStringWithLengthConstraints(example, schema.minLength, schema.maxLength);

0 commit comments

Comments
 (0)