Skip to content

Commit b7f22b1

Browse files
authored
Fix 'str' object has no attribute 'pop' error when parsing query (#6941)
1 parent 897c683 commit b7f22b1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

redash/query_runner/elasticsearch2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
from typing import Optional, Tuple
34

@@ -64,6 +65,7 @@ def run_query(self, query, user):
6465
return data, error
6566

6667
def _build_query(self, query: str) -> Tuple[dict, str, Optional[list]]:
68+
query = json.loads(query)
6769
index_name = query.pop("index", "")
6870
result_fields = query.pop("result_fields", None)
6971
url = "/{}/_search".format(index_name)

tests/query_runner/test_elasticsearch2.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from unittest import TestCase
1+
from unittest import TestCase, mock
22

33
from redash.query_runner.elasticsearch2 import (
44
ElasticSearch2,
@@ -137,3 +137,14 @@ def test_parse_results(self):
137137
],
138138
}
139139
self.assertDictEqual(XPackSQLElasticSearch._parse_results(None, response), expected)
140+
141+
142+
class TestElasticSearch2(TestCase):
143+
@mock.patch("redash.query_runner.elasticsearch2.ElasticSearch2.__init__", return_value=None)
144+
def test_build_query(self, mock_init):
145+
query_runner = ElasticSearch2()
146+
query_str = '{"index": "test_index", "result_fields": ["field1", "field2"]}'
147+
query_dict, url, result_fields = query_runner._build_query(query_str)
148+
self.assertEqual(query_dict, {})
149+
self.assertEqual(url, "/test_index/_search")
150+
self.assertEqual(result_fields, ["field1", "field2"])

0 commit comments

Comments
 (0)