mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
InfluxDB: Fix escaping template variable when it was used in parentheses (#83400)
escape properly regardless of the parentheses
This commit is contained in:
parent
e564024883
commit
6fab62739d
@ -443,6 +443,19 @@ describe('InfluxDataSource Frontend Mode', () => {
|
||||
expect(result).toBe(expectation);
|
||||
});
|
||||
|
||||
it('should return the escaped value if the value wrapped in regex 3', () => {
|
||||
const value = ['env', 'env2', 'env3'];
|
||||
const variableMock = queryBuilder()
|
||||
.withId('tempVar')
|
||||
.withName('tempVar')
|
||||
.withMulti(false)
|
||||
.withIncludeAll(true)
|
||||
.build();
|
||||
const result = ds.interpolateQueryExpr(value, variableMock, 'select from /^($tempVar)$/');
|
||||
const expectation = `env|env2|env3`;
|
||||
expect(result).toBe(expectation);
|
||||
});
|
||||
|
||||
it('should **not** return the escaped value if the value **is not** wrapped in regex', () => {
|
||||
const value = '/special/path';
|
||||
const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build();
|
||||
|
@ -316,8 +316,8 @@ export default class InfluxDatasource extends DataSourceWithBackend<InfluxQuery,
|
||||
// we want to see how it's been used. If it is used in a regex expression
|
||||
// we escape it. Otherwise, we return it directly.
|
||||
// regex below checks if the variable inside /^...$/ (^ and $ is optional)
|
||||
// i.e. /^$myVar$/ or /$myVar/
|
||||
const regex = new RegExp(`\\/(?:\\^)?\\$${variable.name}(?:\\$)?\\/`, 'gm');
|
||||
// i.e. /^$myVar$/ or /$myVar/ or /^($myVar)$/
|
||||
const regex = new RegExp(`\\/(?:\\^)?(.*)(\\$${variable.name})(.*)(?:\\$)?\\/`, 'gm');
|
||||
if (regex.test(query)) {
|
||||
if (typeof value === 'string') {
|
||||
return escapeRegex(value);
|
||||
|
Loading…
Reference in New Issue
Block a user