-
Notifications
You must be signed in to change notification settings - Fork 35
feat(oas-to-har): retain null values in array #998
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
Merged
erunion
merged 4 commits into
readmeio:main
from
eaglethrost:dimas/RM-9906-not-displaying-null-in-array
Oct 6, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3740d74
feat: modify remove undefined objects & encode array to not remove nu…
eaglethrost acb3674
clean: delete cloned removed-undefined-objects
eaglethrost e4e9028
feat: update remove undefined package, update code & tests
eaglethrost 330766c
Merge branch 'main' into dimas/RM-9906-not-displaying-null-in-array
erunion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/oas-to-har/src/lib/remove-undefined-objects.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| function isObject(obj: unknown) { | ||
| return typeof obj === 'object' && obj !== null && !Array.isArray(obj); | ||
| } | ||
|
|
||
| function isEmptyObject(obj: unknown) { | ||
| return typeof obj === 'object' && obj !== null && !Object.keys(obj).length; | ||
| } | ||
|
|
||
| interface RemovalOptions { | ||
| removeAllFalsy?: boolean; | ||
| } | ||
|
|
||
| // Modified from here: https://stackoverflow.com/a/43781499 | ||
| function stripEmptyObjects(obj: any, options: RemovalOptions = {}) { | ||
| const cleanObj = obj; | ||
|
|
||
| if (obj === null && options.removeAllFalsy) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!isObject(obj) && !Array.isArray(cleanObj)) { | ||
| return cleanObj; | ||
| } | ||
|
|
||
| if (!Array.isArray(cleanObj)) { | ||
|
|
||
| Object.keys(cleanObj).forEach(key => { | ||
| let value = cleanObj[key]; | ||
|
|
||
| if (typeof value !== 'object') { | ||
| return; | ||
| } | ||
|
|
||
| if (value === null) { | ||
| if (options.removeAllFalsy) { | ||
| delete cleanObj[key]; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| value = stripEmptyObjects(value, options); | ||
|
|
||
| if (isEmptyObject(value)) { | ||
| delete cleanObj[key]; | ||
| } else { | ||
| cleanObj[key] = value; | ||
| } | ||
| }); | ||
|
|
||
| return cleanObj; | ||
| } | ||
|
|
||
| cleanObj.forEach((o, idx) => { | ||
| let value = o; | ||
| if (typeof value === 'object' && value !== null) { | ||
| value = stripEmptyObjects(value, options); | ||
|
|
||
| if (isEmptyObject(value)) { | ||
| delete cleanObj[idx]; | ||
| } else { | ||
| cleanObj[idx] = value; | ||
| } | ||
| } else if (value === null && options.removeAllFalsy) { | ||
| delete cleanObj[idx]; | ||
| } | ||
| }); | ||
|
|
||
| // Since deleting a key from an array will retain an undefined value in that array, we need to | ||
| // filter them out. | ||
| return cleanObj.filter(el => el !== undefined); | ||
| } | ||
|
|
||
| export default function removeUndefinedObjects<T>(obj?: T, options?: RemovalOptions): T | undefined { | ||
| if (obj === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // Remove objects that recursively contain undefined values | ||
| // E.g. { a: { b: undefined } } -> { a: {} } | ||
| let withoutUndefined = removeUndefined(obj); | ||
|
|
||
| // Then we recursively remove all empty objects and nullish arrays | ||
| withoutUndefined = stripEmptyObjects(withoutUndefined, options); | ||
|
|
||
| // If the only thing that's leftover is an empty object then return nothing. | ||
| if (isEmptyObject(withoutUndefined)) return undefined; | ||
|
|
||
| return withoutUndefined; | ||
| } | ||
|
|
||
| function removeUndefined(obj: any): any { | ||
| if (obj === undefined) { | ||
| return undefined; | ||
| } | ||
| // Preserve null | ||
| if (obj === null) { | ||
| return null; | ||
| } | ||
| // Remove undefined in arrays | ||
| if (Array.isArray(obj)) { | ||
| return obj | ||
| .map(removeUndefined) | ||
| .filter(item => item !== undefined); | ||
| } | ||
| if (typeof obj === 'object') { | ||
| const cleaned: any = {}; | ||
| for (const [key, value] of Object.entries(obj)) { | ||
| const cleanedValue = removeUndefined(value); | ||
| if (cleanedValue !== undefined) { | ||
| cleaned[key] = cleanedValue; | ||
| } | ||
| } | ||
| return cleaned; | ||
| } | ||
| return obj; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/oas-to-har/test/lib/remove-undefined-objects.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { describe, test, it, expect } from 'vitest'; | ||
|
|
||
| import removeUndefinedObjects from '../../src/lib/remove-undefined-objects.js'; | ||
|
|
||
| describe('typings', () => { | ||
| it('should not blow away typings from supplied objects', () => { | ||
| const obj = removeUndefinedObjects({ | ||
| key: 'buster', | ||
| }); | ||
|
|
||
| expect(obj).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| test('should leave primitives alone', () => { | ||
| expect(removeUndefinedObjects(1234)).toBe(1234); | ||
| expect(removeUndefinedObjects('1234')).toBe('1234'); | ||
| expect(removeUndefinedObjects(null)).toBeNull(); | ||
| expect(removeUndefinedObjects()).toBeUndefined(); | ||
| expect(removeUndefinedObjects(undefined)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should leave only truthy primitives alone when removeAllFalsy is true', () => { | ||
| expect(removeUndefinedObjects(1234, { removeAllFalsy: true })).toBe(1234); | ||
| expect(removeUndefinedObjects('1234', { removeAllFalsy: true })).toBe('1234'); | ||
| expect(removeUndefinedObjects(null, { removeAllFalsy: true })).toBeUndefined(); | ||
| expect(removeUndefinedObjects(undefined, { removeAllFalsy: true })).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("should also remove '' and null values when removeAllFalsy is true", () => { | ||
| expect(removeUndefinedObjects({ value: 1234 }, { removeAllFalsy: true })).toStrictEqual({ value: 1234 }); | ||
| expect(removeUndefinedObjects({ value: '1234' }, { removeAllFalsy: true })).toStrictEqual({ value: '1234' }); | ||
| expect(removeUndefinedObjects({ value: null }, { removeAllFalsy: true })).toBeUndefined(); | ||
| expect(removeUndefinedObjects({ value: undefined }, { removeAllFalsy: true })).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should remove empty objects with only empty properties', () => { | ||
| const obj = { | ||
| a: { | ||
| b: {}, | ||
| c: { | ||
| d: {}, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| expect(removeUndefinedObjects(obj)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should remove empty objects with only undefined properties', () => { | ||
| const obj = { | ||
| a: { | ||
| b: undefined, | ||
| c: { | ||
| d: undefined, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| expect(removeUndefinedObjects(obj)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should remove empty arrays from within object', () => { | ||
| const obj = { | ||
| a: { | ||
| b: undefined, | ||
| c: { | ||
| d: undefined, | ||
| }, | ||
| }, | ||
| d: [1234, undefined], | ||
| e: [], | ||
| f: null, | ||
| g: [null, undefined, null], | ||
| }; | ||
|
|
||
| expect(removeUndefinedObjects(obj)).toStrictEqual({ | ||
| d: [1234], | ||
| f: null, | ||
| g: [null, null], | ||
| }); | ||
| }); | ||
|
|
||
| test('should remove empty arrays and falsy values from within object when removeAllFalsy is true', () => { | ||
| const obj = { | ||
| a: { | ||
| b: undefined, | ||
| c: { | ||
| d: undefined, | ||
| }, | ||
| }, | ||
| d: [1234, undefined], | ||
| e: [], | ||
| f: null, | ||
| g: [null, undefined, null], | ||
| }; | ||
|
|
||
| expect(removeUndefinedObjects(obj, { removeAllFalsy: true })).toStrictEqual({ | ||
| d: [1234], | ||
| }); | ||
| }); | ||
|
|
||
| test('should remove undefined values from arrays & not null values', () => { | ||
| expect(removeUndefinedObjects([undefined, undefined])).toBeUndefined(); | ||
| expect(removeUndefinedObjects([null])).toStrictEqual([null]); | ||
| expect(removeUndefinedObjects(['1234', null, undefined, { a: null, b: undefined }, ' ', ''])).toStrictEqual([ | ||
| '1234', | ||
| null, | ||
| { | ||
| a: null, | ||
| }, | ||
| ' ', | ||
| '', | ||
| ]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we vendoring and forking remove-undefined-objects in here instead of adding a new option to that library for preserving empty arrays? thats what @mjcuva and i talked about out what i thought needed to happen for this ticket
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry this PR is not for that ticket, but a different one, which tries to solve a different issue of null values in arrays getting dropped in the request snippet.
The PR's related to the ticket you were referring to are these: