Skip to content

Commit b5ad1a4

Browse files
author
Paul Korzhyk
authored
Merge pull request #50 from dgraph-io/martinmr/non-string-var
Reject non-string keys or values in variable map.
2 parents 99b9b8a + d7faf8a commit b5ad1a4

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pydgraph/txn.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ def _common_query(self, query, variables=None):
7878
for key, value in variables.items():
7979
if util.is_string(key) and util.is_string(value):
8080
req.vars[key] = value
81+
else:
82+
raise Exception(
83+
'Values and keys in variable map must be strings')
8184

8285
return req
8386

tests/test_txn.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,25 @@ def test_read_index_key_same_txn(self):
413413
self.assertEqual([], json.loads(resp.json).get('me'),
414414
"Expected 0 nodes read from index")
415415

416+
def test_non_string_variable(self):
417+
"""Tests sending a variable map with non-string values or keys results
418+
in an Exception."""
419+
helper.drop_all(self.client)
420+
helper.set_schema(self.client, 'name: string @index(exact) .')
421+
422+
txn = self.client.txn()
423+
query = """
424+
query node($a: string) {
425+
node(func: eq(name, $a))
426+
{
427+
expand(_all_)
428+
}
429+
}
430+
"""
431+
variables = {"$a": 1234}
432+
with self.assertRaises(Exception):
433+
_ = txn.query(query, variables=variables)
434+
416435

417436
class TestSPStar(helper.ClientIntegrationTestCase):
418437
def setUp(self):
@@ -503,7 +522,7 @@ def test_sp_star2(self):
503522
'uid': uid1,
504523
'friend': [{'name': 'Jan2', 'uid': uid2}]
505524
}], json.loads(resp.json).get('me'))
506-
525+
507526
deleted2 = txn.mutate(del_obj={'uid': uid1, 'friend': None})
508527
self.assertEqual(0, len(deleted2.uids))
509528
resp = txn.query(query)

0 commit comments

Comments
 (0)