Skip to content

Commit 7b9bbc1

Browse files
authored
fix(aggregations): pass object to the get schema; make sure get schema handles recursive deps (#5241)
1 parent e934571 commit 7b9bbc1

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

packages/compass-aggregations/src/components/stage-wizard/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ export default connect(
289289
return { name, type };
290290
});
291291
const previousStageFieldsWithSchema = getSchema(
292-
previousStage?.previewDocs ?? []
292+
previousStage?.previewDocs?.map((doc) => {
293+
return doc.generateObject();
294+
}) ?? []
293295
);
294296

295297
const fields =

packages/compass-aggregations/src/utils/get-schema.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,22 @@ describe('get schema', function () {
189189
expect(getSchema(input)).to.deep.equal(output);
190190
});
191191
});
192+
193+
it("doesn't break when getting schema from recursive object", function () {
194+
const a = {
195+
b: {
196+
c: {
197+
get a() {
198+
return a;
199+
},
200+
},
201+
},
202+
};
203+
204+
expect(getSchema([a])).to.deep.eq([
205+
{ name: 'b', type: 'Object' },
206+
{ name: 'b.c', type: 'Object' },
207+
{ name: 'b.c.a', type: 'Object' },
208+
]);
209+
});
192210
});

packages/compass-aggregations/src/utils/get-schema.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,28 @@ const toFieldSchemaWithPrefix = (prefix: string) => {
2222
});
2323
};
2424

25-
const getSchemaForObject = (document: Document): DocumentSchema => {
25+
const getSchemaForObject = (
26+
document: Document,
27+
seen = new WeakSet()
28+
): DocumentSchema => {
2629
const schema: DocumentSchema = [];
30+
31+
if (seen.has(document)) {
32+
return schema;
33+
}
34+
35+
seen.add(document);
36+
2737
for (const key in document) {
2838
const value = document[key];
39+
2940
schema.push({
3041
name: key,
3142
type: TypeChecker.type(value),
3243
});
3344

3445
if (Array.isArray(value)) {
35-
const valueSchema = getSchemaForArray(value).map(
46+
const valueSchema = getSchemaForArray(value, seen).map(
3647
toFieldSchemaWithPrefix(key)
3748
);
3849
schema.push(...valueSchema);
@@ -41,7 +52,7 @@ const getSchemaForObject = (document: Document): DocumentSchema => {
4152
value !== null &&
4253
!value._bsontype
4354
) {
44-
const valueSchema = getSchemaForObject(value).map(
55+
const valueSchema = getSchemaForObject(value, seen).map(
4556
toFieldSchemaWithPrefix(key)
4657
);
4758
schema.push(...valueSchema);
@@ -50,18 +61,21 @@ const getSchemaForObject = (document: Document): DocumentSchema => {
5061
return schema;
5162
};
5263

53-
const getSchemaForArray = (records: Document[]): DocumentSchema => {
64+
const getSchemaForArray = (
65+
records: Document[],
66+
seen = new WeakSet()
67+
): DocumentSchema => {
5468
const schema: DocumentSchema = [];
5569

5670
for (const record of records) {
5771
if (Array.isArray(record)) {
58-
schema.push(...getSchemaForArray(record));
72+
schema.push(...getSchemaForArray(record, seen));
5973
} else if (
6074
typeof record === 'object' &&
6175
record !== null &&
6276
!record._bsontype
6377
) {
64-
schema.push(...getSchemaForObject(record));
78+
schema.push(...getSchemaForObject(record, seen));
6579
}
6680
}
6781

0 commit comments

Comments
 (0)