Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [RUMF-1470] Sanitize - Fix size computation #2116

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/core/src/tools/sanitize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ describe('sanitize', () => {

expect(sanitize(cannotSerialize)).toEqual('[Unserializable]')
})

it('should handle objects with properties including null or undefined', () => {
const obj = { a: null, b: undefined }
expect(sanitize(obj)).toEqual({ a: null, b: undefined })
})
})

describe('arrays handling', () => {
Expand All @@ -122,6 +127,11 @@ describe('sanitize', () => {

expect(sanitize(arr)).toEqual([1, 2, 3, 4])
})

it('should handle arrays containing null or undefined', () => {
const arr = [null, undefined]
expect(sanitize(arr)).toEqual([null, undefined])
})
})

describe('circular references handling', () => {
Expand Down Expand Up @@ -240,5 +250,22 @@ describe('sanitize', () => {
expect(sanitized).toEqual([1, 2]) // Length of 5 after JSON.stringify
expect(displaySpy).toHaveBeenCalled()
})

it('should count size properly when array contains undefined values', () => {
// This is a special case: JSON.stringify([undefined]) => '[null]'
const displaySpy = spyOn(display, 'warn')
const arr = [undefined, undefined] // Length of 11 after JSON.stringify
const sanitized = sanitize(arr, 10)
expect(sanitized).toEqual([undefined])
expect(displaySpy).toHaveBeenCalled()
})

it('should count size properly when an object contains properties with undefined values', () => {
const displaySpy = spyOn(display, 'warn')
const obj = { a: undefined, b: 42 } // Length of 8 after JSON.stringify
const sanitized = sanitize(obj, 8)
expect(sanitized).toEqual({ a: undefined, b: 42 })
expect(displaySpy).not.toHaveBeenCalled()
})
})
})
22 changes: 17 additions & 5 deletions packages/core/src/tools/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,20 @@ export function sanitize(source: unknown, maxCharacterCount = SANITIZE_DEFAULT_M
containerQueue,
visitedObjectsWithPath
)
accumulatedCharacterCount += JSON.stringify(targetData).length + separatorLength

if (targetData !== undefined) {
accumulatedCharacterCount += JSON.stringify(targetData).length
} else {
// When an element of an Array (targetData) is undefined, it is serialized as null:
// JSON.stringify([undefined]) => '[null]' - This accounts for 4 characters
accumulatedCharacterCount += 4
}
accumulatedCharacterCount += separatorLength
separatorLength = 1
if (accumulatedCharacterCount > maxCharacterCount) {
warnOverCharacterLimit(maxCharacterCount, 'truncated', source)
break
}
separatorLength = 1
;(containerToProcess.target as ContextArray)[key] = targetData
}
} else {
Expand All @@ -97,13 +105,17 @@ export function sanitize(source: unknown, maxCharacterCount = SANITIZE_DEFAULT_M
containerQueue,
visitedObjectsWithPath
)
accumulatedCharacterCount +=
JSON.stringify(targetData).length + separatorLength + key.length + KEY_DECORATION_LENGTH
// When a property of an object has an undefined value, it will be dropped during serialization:
// JSON.stringify({a:undefined}) => '{}'
if (targetData !== undefined) {
accumulatedCharacterCount +=
JSON.stringify(targetData).length + separatorLength + key.length + KEY_DECORATION_LENGTH
separatorLength = 1
}
if (accumulatedCharacterCount > maxCharacterCount) {
warnOverCharacterLimit(maxCharacterCount, 'truncated', source)
break
}
separatorLength = 1
;(containerToProcess.target as Context)[key] = targetData
}
}
Expand Down