Skip to content

Commit da4ae61

Browse files
committed
Preserver original order in traverse
1 parent 0aff6bc commit da4ae61

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

packages/openapi-parser/src/traverse.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ export async function traverse<T extends AnyObject | AnyObject[]>(
2929
}
3030

3131
const keys = Object.keys(specification);
32-
await Promise.all(
33-
keys.map(async (key) => {
32+
const promises = await Promise.all(
33+
keys.map(async (key, index) => {
3434
const value = specification[key];
35-
result[key] = await traverse(value, transform, [...path, key], seen);
35+
const processed = await traverse(value, transform, [...path, key], seen);
36+
return { key, value: processed, index };
3637
})
3738
);
3839

40+
// Promise.all does not guarantee the order of the results
41+
// So we need to sort them to preserve the original order
42+
promises.sort((a, b) => a.index - b.index);
43+
for (const { key, value } of promises) {
44+
result[key] = value;
45+
}
46+
3947
return transform(result, path) as Promise<T>;
4048
}

0 commit comments

Comments
 (0)