Commit 5411c14b authored by Brandon Labuschagne's avatar Brandon Labuschagne

Merge branch 'mrincon-remove-empty-arrays-from-params' into 'master'

setUrlParams removes array parameters from URL

See merge request gitlab-org/gitlab!62203
parents a10cff85 91d0184f
...@@ -484,13 +484,17 @@ export const setUrlParams = ( ...@@ -484,13 +484,17 @@ export const setUrlParams = (
searchParams.delete(key); searchParams.delete(key);
} else if (Array.isArray(params[key])) { } else if (Array.isArray(params[key])) {
const keyName = railsArraySyntax ? `${key}[]` : key; const keyName = railsArraySyntax ? `${key}[]` : key;
params[key].forEach((val, idx) => { if (params[key].length === 0) {
if (idx === 0) { searchParams.delete(keyName);
searchParams.set(keyName, val); } else {
} else { params[key].forEach((val, idx) => {
searchParams.append(keyName, val); if (idx === 0) {
} searchParams.set(keyName, val);
}); } else {
searchParams.append(keyName, val);
}
});
}
} else { } else {
searchParams.set(key, params[key]); searchParams.set(key, params[key]);
} }
......
...@@ -798,15 +798,29 @@ describe('URL utility', () => { ...@@ -798,15 +798,29 @@ describe('URL utility', () => {
); );
}); });
it('handles arrays properly', () => { it('adds parameters from arrays', () => {
const url = 'https://gitlab.com/test'; const url = 'https://gitlab.com/test';
expect(urlUtils.setUrlParams({ label_name: ['foo', 'bar'] }, url)).toEqual( expect(urlUtils.setUrlParams({ labels: ['foo', 'bar'] }, url)).toEqual(
'https://gitlab.com/test?label_name=foo&label_name=bar', 'https://gitlab.com/test?labels=foo&labels=bar',
); );
}); });
it('handles arrays properly when railsArraySyntax=true', () => { it('removes parameters from empty arrays', () => {
const url = 'https://gitlab.com/test?labels=foo&labels=bar';
expect(urlUtils.setUrlParams({ labels: [] }, url)).toEqual('https://gitlab.com/test');
});
it('removes parameters from empty arrays while keeping other parameters', () => {
const url = 'https://gitlab.com/test?labels=foo&labels=bar&unrelated=unrelated';
expect(urlUtils.setUrlParams({ labels: [] }, url)).toEqual(
'https://gitlab.com/test?unrelated=unrelated',
);
});
it('adds parameters from arrays when railsArraySyntax=true', () => {
const url = 'https://gitlab.com/test'; const url = 'https://gitlab.com/test';
expect(urlUtils.setUrlParams({ labels: ['foo', 'bar'] }, url, false, true)).toEqual( expect(urlUtils.setUrlParams({ labels: ['foo', 'bar'] }, url, false, true)).toEqual(
...@@ -814,6 +828,14 @@ describe('URL utility', () => { ...@@ -814,6 +828,14 @@ describe('URL utility', () => {
); );
}); });
it('removes parameters from empty arrays when railsArraySyntax=true', () => {
const url = 'https://gitlab.com/test?labels%5B%5D=foo&labels%5B%5D=bar';
expect(urlUtils.setUrlParams({ labels: [] }, url, false, true)).toEqual(
'https://gitlab.com/test',
);
});
it('decodes URI when decodeURI=true', () => { it('decodes URI when decodeURI=true', () => {
const url = 'https://gitlab.com/test'; const url = 'https://gitlab.com/test';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment