Escape name__regex and name__iregex (#11964)

Escape name__regex and name__iregex. Escaping the value for those
keys when creating a smart inventory is a work around for the
pyparsing code on the API side for special characters. This will just
display an extra escape when showing the host_filter on details page.
This commit is contained in:
Kersom 2022-04-08 13:08:32 -04:00 committed by GitHub
parent b646aa03f8
commit 0712affa9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -68,11 +68,12 @@ export function toQueryString(config, searchParams = {}) {
/**
* Escape a string with double quote in case there was a white space
* @param {string} key The key of the value to be parsed
* @param {string} value A string to be parsed
* @return {string} string
*/
const escapeString = (value) => {
if (verifySpace(value)) {
const escapeString = (key, value) => {
if (verifySpace(value) || key.includes('regex')) {
return `"${value}"`;
}
return value;
@ -95,9 +96,11 @@ export function toHostFilter(searchParams = {}) {
.sort()
.flatMap((key) => {
if (Array.isArray(searchParams[key])) {
return searchParams[key].map((val) => `${key}=${escapeString(val)}`);
return searchParams[key].map(
(val) => `${key}=${escapeString(key, val)}`
);
}
return `${key}=${escapeString(searchParams[key])}`;
return `${key}=${escapeString(key, searchParams[key])}`;
});
const filteredSearchParams = flattenSearchParams.filter(

View File

@ -136,6 +136,17 @@ describe('toHostFilter', () => {
);
});
test('should escape name__regex and name__iregex', () => {
const object = {
or__name__regex: '(t|e)st',
or__name__iregex: '(f|o)',
or__name: 'foo',
};
expect(toHostFilter(object)).toEqual(
'name=foo or name__iregex="(f|o)" or name__regex="(t|e)st"'
);
});
test('should return a host filter with or conditional when value is array', () => {
const object = {
or__groups__id: ['1', '2'],