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

Fix: omit nulls in params #6394

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: omit nulls when building params
  • Loading branch information
Willshaw committed May 15, 2024
commit bde5fca3b2021050156f4c05dafa90e09fbf78f5
40 changes: 26 additions & 14 deletions lib/helpers/toFormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,31 @@ function toFormData(obj, formData, options) {
function defaultVisitor(value, key, path) {
var arr = value;

if (value && !path && typeof value === 'object') {
if (utils.endsWith(key, '{}')) {
if (value && !path && typeof value === "object") {
if (utils.endsWith(key, "{}")) {
// eslint-disable-next-line no-param-reassign
key = metaTokens ? key : key.slice(0, -2);
// eslint-disable-next-line no-param-reassign
value = JSON.stringify(value);
} else if (
(utils.isArray(value) && isFlatArray(value)) ||
(utils.isFileList(value) || utils.endsWith(key, '[]') && (arr = utils.toArray(value))
)) {
utils.isFileList(value) ||
(utils.endsWith(key, "[]") && (arr = utils.toArray(value)))
) {
// eslint-disable-next-line no-param-reassign
key = removeBrackets(key);

arr.forEach(function each(el, index) {
!utils.isUndefined(el) && formData.append(
// eslint-disable-next-line no-nested-ternary
indexes === true ? renderKey([key], index, dots) : (indexes === null ? key : key + '[]'),
convertValue(el)
);
!(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)
);
});
return false;
}
Expand All @@ -142,22 +148,28 @@ function toFormData(obj, formData, options) {
var exposedHelpers = Object.assign(predicates, {
defaultVisitor: defaultVisitor,
convertValue: convertValue,
isVisitable: isVisitable
isVisitable: isVisitable,
});

function build(value, path) {
if (utils.isUndefined(value)) return;

if (stack.indexOf(value) !== -1) {
throw Error('Circular reference detected in ' + path.join('.'));
throw Error("Circular reference detected in " + path.join("."));
}

stack.push(value);

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

if (result === true) {
build(el, path ? path.concat(key) : [key]);
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