Make str(json_param) return string vs python

Closes-Bug: #1366798

Change-Id: I3f435932787662cb1bfa91bd5a84c21cf6ddf9d0
This commit is contained in:
Endre Karlson 2014-09-09 15:18:38 +02:00 committed by Endre Karlson
parent 35d7465c11
commit 3913658527
2 changed files with 15 additions and 0 deletions

View File

@ -386,6 +386,11 @@ class JsonParam(Parameter):
def __len__(self):
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):
val = self.parse(val)
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)
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):
self.assertRaises(exception.InvalidSchemaError, self.new_parameter,
'p', {'Type': 'List'}, validate_value=False)