Skip to content

Commit e1d8409

Browse files
authored
fix: expandToNestedObject method bug (inconsistent top-level keys) (#551)
1 parent 9daccff commit e1d8409

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

.changeset/good-parrots-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperdx/api": patch
3+
---
4+
5+
fix: expandToNestedObject method bug (inconsistent top-level keys)

packages/api/src/tasks/__tests__/checkAlerts.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ describe('checkAlerts', () => {
9999
expect(expandToNestedObject({ 'foo.bar.baz': 'qux' })).toEqual({
100100
foo: { bar: { baz: 'qux' } },
101101
});
102+
// inconsistent top level keys
103+
expect(
104+
expandToNestedObject({
105+
foo: 'bar',
106+
'foo.bar': 'baz',
107+
}),
108+
).toEqual({
109+
foo: 'bar',
110+
});
111+
expect(
112+
expandToNestedObject({
113+
'foo.bar': 'baz',
114+
foo: 'bar',
115+
}),
116+
).toEqual({
117+
foo: 'bar',
118+
});
102119
// mix
103120
expect(
104121
expandToNestedObject({

packages/api/src/tasks/checkAlerts.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,16 @@ export const expandToNestedObject = (
126126
break;
127127
}
128128
const nestedKey = keys[i];
129-
if (i === keys.length - 1) {
130-
nestedObj[nestedKey] = obj[key];
131-
} else {
132-
nestedObj[nestedKey] = nestedObj[nestedKey] || {};
133-
nestedObj = nestedObj[nestedKey];
129+
try {
130+
if (i === keys.length - 1) {
131+
nestedObj[nestedKey] = obj[key];
132+
} else {
133+
nestedObj[nestedKey] = nestedObj[nestedKey] || {};
134+
nestedObj = nestedObj[nestedKey];
135+
}
136+
} catch (e) {
137+
// skip the duplicate inconsistent top level keys case. ex: 'foo' and 'foo.bar'
138+
logger.warn('expandToNestedObject', e);
134139
}
135140
}
136141
}

0 commit comments

Comments
 (0)