Skip to content

Commit

Permalink
[BUGFIX] serialize null array items to string (#8083)
Browse files Browse the repository at this point in the history
  • Loading branch information
camerondubas authored and runspired committed Sep 12, 2022
1 parent 2878466 commit ecd10b5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { module, test } from 'qunit';

import { serializeQueryParams } from '@ember-data/adapter/-private';

module('Unit | serializeQueryParams', function () {
test('it works', function (assert) {
assert.expect(1);

const qp = {
filter: '%foo',
};
const result = serializeQueryParams(qp);

assert.deepEqual(result, 'filter=%25foo');
});

test('it works with nested', function (assert) {
assert.expect(1);

const qp = {
filter: {
children: [1, 2],
},
};
const result = serializeQueryParams(qp);

assert.deepEqual(result, 'filter%5Bchildren%5D%5B%5D=1&filter%5Bchildren%5D%5B%5D=2');
});

test('it treats null values in arrays as empty strings', function (assert) {
assert.expect(1);

const qp = {
filter: {
children: [null, 2],
},
};
const result = serializeQueryParams(qp);

assert.deepEqual(result, 'filter%5Bchildren%5D%5B%5D=&filter%5Bchildren%5D%5B%5D=2');
});

test('it works with + sign', function (assert) {
assert.expect(1);

const qp = {
term: 'search me',
};
const result = serializeQueryParams(qp);

assert.deepEqual(result, 'term=search%20me');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function serializeQueryParams(queryParamsObject: object | string): string
if (RBRACKET.test(prefix)) {
add(s, prefix, obj[i]);
} else {
buildParams(prefix + '[' + (typeof obj[i] === 'object' ? i : '') + ']', obj[i]);
buildParams(prefix + '[' + (typeof obj[i] === 'object' && obj[i] !== null ? i : '') + ']', obj[i]);
}
}
} else if (isPlainObject(obj)) {
Expand Down

0 comments on commit ecd10b5

Please sign in to comment.