mirror of
https://github.com/strapi/strapi.git
synced 2025-11-16 18:19:34 +00:00
Merge pull request #14463 from strapi/fix/search-encoding
Fix search query encoding
This commit is contained in:
commit
11a193c8dc
@ -1,4 +1,5 @@
|
|||||||
import { stringify } from 'qs';
|
import { stringify } from 'qs';
|
||||||
|
import set from 'lodash/set';
|
||||||
import createPluginsFilter from './createPluginsFilter';
|
import createPluginsFilter from './createPluginsFilter';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,12 +13,23 @@ const buildQueryString = (queryParams = {}) => {
|
|||||||
* Extracting pluginOptions from the query since we don't want them to be part
|
* Extracting pluginOptions from the query since we don't want them to be part
|
||||||
* of the url
|
* of the url
|
||||||
*/
|
*/
|
||||||
const { plugins: _, ...otherQueryParams } = {
|
const {
|
||||||
|
plugins: _,
|
||||||
|
_q: query,
|
||||||
|
...otherQueryParams
|
||||||
|
} = {
|
||||||
...queryParams,
|
...queryParams,
|
||||||
...createPluginsFilter(queryParams.plugins),
|
...createPluginsFilter(queryParams.plugins),
|
||||||
};
|
};
|
||||||
|
|
||||||
return `?${stringify(otherQueryParams, { encode: false })}`;
|
if (query) {
|
||||||
|
set(otherQueryParams, `_q`, encodeURIComponent(query));
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${stringify(otherQueryParams, {
|
||||||
|
encode: false,
|
||||||
|
addQueryPrefix: true,
|
||||||
|
})}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default buildQueryString;
|
export default buildQueryString;
|
||||||
|
|||||||
@ -58,4 +58,18 @@ describe('buildQueryString', () => {
|
|||||||
'?page=1&pageSize=10&sort=name:ASC&filters[0][name]=hello world&locale=en'
|
'?page=1&pageSize=10&sort=name:ASC&filters[0][name]=hello world&locale=en'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('creates a valid query string with a search query', () => {
|
||||||
|
const _q = `test&query`;
|
||||||
|
const queryParams = {
|
||||||
|
page: '1',
|
||||||
|
pageSize: '10',
|
||||||
|
sort: 'name:ASC',
|
||||||
|
_q,
|
||||||
|
};
|
||||||
|
|
||||||
|
const queryString = buildQueryString(queryParams);
|
||||||
|
|
||||||
|
expect(queryString).toBe(`?page=1&pageSize=10&sort=name:ASC&_q=${encodeURIComponent(_q)}`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -40,7 +40,7 @@ const SearchURLQuery = ({ label, placeholder, trackedEvent, trackedEventDetails
|
|||||||
if (trackedEvent) {
|
if (trackedEvent) {
|
||||||
trackUsage(trackedEvent, trackedEventDetails);
|
trackUsage(trackedEvent, trackedEventDetails);
|
||||||
}
|
}
|
||||||
setQuery({ _q: value, page: 1 });
|
setQuery({ _q: encodeURIComponent(value), page: 1 });
|
||||||
} else {
|
} else {
|
||||||
handleToggle();
|
handleToggle();
|
||||||
setQuery({ _q: '' }, 'remove');
|
setQuery({ _q: '' }, 'remove');
|
||||||
|
|||||||
@ -97,7 +97,7 @@ describe('useAssets', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
expect(axiosInstance.get).toBeCalledWith(
|
expect(axiosInstance.get).toBeCalledWith(
|
||||||
`/upload/files?${stringify(expected, { encode: false })}`
|
`/upload/files${stringify(expected, { encode: false, addQueryPrefix: true })}`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ describe('useAssets', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
expect(axiosInstance.get).toBeCalledWith(
|
expect(axiosInstance.get).toBeCalledWith(
|
||||||
`/upload/files?${stringify(expected, { encode: false })}`
|
`/upload/files${stringify(expected, { encode: false, addQueryPrefix: true })}`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -148,7 +148,7 @@ describe('useAssets', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
expect(axiosInstance.get).toBeCalledWith(
|
expect(axiosInstance.get).toBeCalledWith(
|
||||||
`/upload/files?${stringify(expected, { encode: false })}`
|
`/upload/files${stringify(expected, { encode: false, addQueryPrefix: true })}`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -172,7 +172,32 @@ describe('useAssets', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
expect(axiosInstance.get).toBeCalledWith(
|
expect(axiosInstance.get).toBeCalledWith(
|
||||||
`/upload/files?${stringify(expected, { encode: false })}`
|
`/upload/files${stringify(expected, { encode: false, addQueryPrefix: true })}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('correctly encodes the search query _q', async () => {
|
||||||
|
const _q = 'something&else';
|
||||||
|
const { result, waitFor, waitForNextUpdate } = await setup({
|
||||||
|
query: { folder: 5, _q, filters: { $and: [{ something: 'true' }] } },
|
||||||
|
});
|
||||||
|
|
||||||
|
await waitFor(() => result.current.isSuccess);
|
||||||
|
await waitForNextUpdate();
|
||||||
|
|
||||||
|
const expected = {
|
||||||
|
filters: {
|
||||||
|
$and: [
|
||||||
|
{
|
||||||
|
something: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
_q: encodeURIComponent(_q),
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(axiosInstance.get).toBeCalledWith(
|
||||||
|
`/upload/files${stringify(expected, { encode: false, addQueryPrefix: true })}`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,7 @@ export const useAssets = ({ skipWhen = false, query = {} } = {}) => {
|
|||||||
if (_q) {
|
if (_q) {
|
||||||
params = {
|
params = {
|
||||||
...paramsExceptFolderAndQ,
|
...paramsExceptFolderAndQ,
|
||||||
_q,
|
_q: encodeURIComponent(_q),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
params = {
|
params = {
|
||||||
@ -42,7 +42,10 @@ export const useAssets = ({ skipWhen = false, query = {} } = {}) => {
|
|||||||
const getAssets = async () => {
|
const getAssets = async () => {
|
||||||
try {
|
try {
|
||||||
const { data } = await axiosInstance.get(
|
const { data } = await axiosInstance.get(
|
||||||
`${dataRequestURL}?${stringify(params, { encode: false })}`
|
`${dataRequestURL}${stringify(params, {
|
||||||
|
encode: false,
|
||||||
|
addQueryPrefix: true,
|
||||||
|
})}`
|
||||||
);
|
);
|
||||||
|
|
||||||
notifyStatus(
|
notifyStatus(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user