Skip to content

Commit

Permalink
fix(aggregations): pass object to the get schema; make sure get schem…
Browse files Browse the repository at this point in the history
…a handles recursive deps (#5241)
  • Loading branch information
gribnoysup authored Dec 15, 2023
1 parent e934571 commit 7b9bbc1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ export default connect(
return { name, type };
});
const previousStageFieldsWithSchema = getSchema(
previousStage?.previewDocs ?? []
previousStage?.previewDocs?.map((doc) => {
return doc.generateObject();
}) ?? []
);

const fields =
Expand Down
18 changes: 18 additions & 0 deletions packages/compass-aggregations/src/utils/get-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,22 @@ describe('get schema', function () {
expect(getSchema(input)).to.deep.equal(output);
});
});

it("doesn't break when getting schema from recursive object", function () {
const a = {
b: {
c: {
get a() {
return a;
},
},
},
};

expect(getSchema([a])).to.deep.eq([
{ name: 'b', type: 'Object' },
{ name: 'b.c', type: 'Object' },
{ name: 'b.c.a', type: 'Object' },
]);
});
});
26 changes: 20 additions & 6 deletions packages/compass-aggregations/src/utils/get-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,28 @@ const toFieldSchemaWithPrefix = (prefix: string) => {
});
};

const getSchemaForObject = (document: Document): DocumentSchema => {
const getSchemaForObject = (
document: Document,
seen = new WeakSet()
): DocumentSchema => {
const schema: DocumentSchema = [];

if (seen.has(document)) {
return schema;
}

seen.add(document);

for (const key in document) {
const value = document[key];

schema.push({
name: key,
type: TypeChecker.type(value),
});

if (Array.isArray(value)) {
const valueSchema = getSchemaForArray(value).map(
const valueSchema = getSchemaForArray(value, seen).map(
toFieldSchemaWithPrefix(key)
);
schema.push(...valueSchema);
Expand All @@ -41,7 +52,7 @@ const getSchemaForObject = (document: Document): DocumentSchema => {
value !== null &&
!value._bsontype
) {
const valueSchema = getSchemaForObject(value).map(
const valueSchema = getSchemaForObject(value, seen).map(
toFieldSchemaWithPrefix(key)
);
schema.push(...valueSchema);
Expand All @@ -50,18 +61,21 @@ const getSchemaForObject = (document: Document): DocumentSchema => {
return schema;
};

const getSchemaForArray = (records: Document[]): DocumentSchema => {
const getSchemaForArray = (
records: Document[],
seen = new WeakSet()
): DocumentSchema => {
const schema: DocumentSchema = [];

for (const record of records) {
if (Array.isArray(record)) {
schema.push(...getSchemaForArray(record));
schema.push(...getSchemaForArray(record, seen));
} else if (
typeof record === 'object' &&
record !== null &&
!record._bsontype
) {
schema.push(...getSchemaForObject(record));
schema.push(...getSchemaForObject(record, seen));
}
}

Expand Down

0 comments on commit 7b9bbc1

Please sign in to comment.