Merge "Make str(json_param) return string vs python"

This commit is contained in:
Jenkins 2014-09-13 03:35:14 +00:00 committed by Gerrit Code Review
commit 38aa12a015
2 changed files with 15 additions and 0 deletions

View File

@ -391,6 +391,11 @@ class JsonParam(Parameter):
def __len__(self): def __len__(self):
return len(self.parsed) return len(self.parsed)
def __str__(self):
if self.hidden():
return super(JsonParam, self).__str__()
return strutils.safe_decode(json.dumps(self.value()))
def _validate(self, val, context): def _validate(self, val, context):
val = self.parse(val) val = self.parse(val)
self.schema.validate_value(val, context) self.schema.validate_value(val, context)

View File

@ -50,6 +50,16 @@ class ParameterTest(testtools.TestCase):
p = self.new_parameter('p', {'Type': 'Json'}, validate_value=False) p = self.new_parameter('p', {'Type': 'Json'}, validate_value=False)
self.assertIsInstance(p, parameters.JsonParam) self.assertIsInstance(p, parameters.JsonParam)
def test_json_return(self):
p = self.new_parameter('p', {'Type': 'Json'}, {"a": 1, "b": "a"})
self.assertEqual('{"a": 1, "b": "a"}', str(p))
def test_json_return_no_echo_true(self):
p = self.new_parameter(
'p', {'Type': 'Json', 'NoEcho': 'true'}, {"a": 1})
self.assertTrue(p.hidden())
self.assertEqual(str(p), '******')
def test_new_bad_type(self): def test_new_bad_type(self):
self.assertRaises(exception.InvalidSchemaError, self.new_parameter, self.assertRaises(exception.InvalidSchemaError, self.new_parameter,
'p', {'Type': 'List'}, validate_value=False) 'p', {'Type': 'List'}, validate_value=False)