Skip to content

Commit

Permalink
Fix: omit nulls in params (#6394)
Browse files Browse the repository at this point in the history
* fix: omit nulls when building params

* chore: fix prettier formatting

* fix: omit nulls when building params pt 2

---------

Co-authored-by: Jay <[email protected]>
  • Loading branch information
Willshaw and jasonsaayman committed May 24, 2024
1 parent 146848f commit 3936f44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/helpers/toFormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function toFormData(obj, formData, options) {
key = removeBrackets(key);

arr.forEach(function each(el, index) {
!utils.isUndefined(el) && formData.append(
!(utils.isUndefined(el) || el === null) && formData.append(
// eslint-disable-next-line no-nested-ternary
indexes === true ? renderKey([key], index, dots) : (indexes === null ? key : key + '[]'),
convertValue(el)
Expand Down Expand Up @@ -155,7 +155,7 @@ function toFormData(obj, formData, options) {
stack.push(value);

utils.forEach(value, function each(el, key) {
var result = !utils.isUndefined(el) && visitor.call(
var result = !(utils.isUndefined(el) || el === null) && visitor.call(
formData, el, utils.isString(key) ? key.trim() : key, path, exposedHelpers
);

Expand Down
18 changes: 12 additions & 6 deletions test/specs/helpers/buildURL.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ describe('helpers::buildURL', function () {
});

it('should support params', function () {
expect(buildURL('/foo', {
foo: 'bar'
})).toEqual('/foo?foo=bar');
expect(
buildURL("/foo", {
foo: "bar",
isUndefined: undefined,
isNull: null,
})
).toEqual("/foo?foo=bar");
});

it('should support object params', function () {
Expand All @@ -29,9 +33,11 @@ describe('helpers::buildURL', function () {
});

it('should support array params', function () {
expect(buildURL('/foo', {
foo: ['bar', 'baz']
})).toEqual('/foo?foo[]=bar&foo[]=baz');
expect(
buildURL("/foo", {
foo: ["bar", "baz", null, undefined],
})
).toEqual("/foo?foo[]=bar&foo[]=baz");
});

it('should support special char params', function () {
Expand Down

0 comments on commit 3936f44

Please sign in to comment.