Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 0d88e53

Browse files
committed
fix(oas2): recursive generateBody
1 parent eafd062 commit 0d88e53

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

packages/openapi2-parser/lib/generator.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ faker.option({
1717
random: () => 0,
1818
});
1919

20-
const schemaIsArrayAndHasItems = schema => schema.type && schema.type === 'array' && schema.items;
20+
const schemaIsArrayAndHasItems = schema => schema.type === 'array' && typeof schema.items === 'object';
2121
const isEmptyArray = value => value && Array.isArray(value) && value.length === 0;
2222

2323
function generateBody(schema) {
24-
if (schema.allOf && schema.allOf.length === 1 && schema.allOf[0].examples && schema.allOf[0].examples.length > 0) {
25-
return schema.allOf[0].examples[0];
26-
}
27-
2824
if (schema.examples && schema.examples.length > 0) {
2925
return schema.examples[0];
3026
}
3127

28+
if (schemaIsArrayAndHasItems(schema)) {
29+
return [generateBody(schema.items)];
30+
}
31+
32+
if (schema.allOf && schema.allOf.length === 1) {
33+
return generateBody(schema.allOf[0]);
34+
}
35+
3236
const body = faker.generate(schema);
3337

3438
if (isEmptyArray(body) && schemaIsArrayAndHasItems(schema)) {

0 commit comments

Comments
 (0)