Always convert string type params to string

Schema.to_schema_type() should convert values passed to it to the
appropriate type.  So if the user passed a version number of 9.4 and
they defined it in the template with a string type, we should get the
string '9.4' back, instead of the error "The value '9.4' is invalid for
type 'string'".

Co-Authored-By: Richard Lee <rblee88@gmail.com>
Closes-Bug: #1353701
Change-Id: If7abd129dc7819c6ac01f9bf7dfdbe397ec09c65
This commit is contained in:
Anderson Mesquita 2014-08-06 17:48:56 -04:00
parent 58e141b2ac
commit af21176f1a
2 changed files with 6 additions and 5 deletions

View File

@ -173,8 +173,6 @@ class Schema(collections.Mapping):
elif self.type == self.NUMBER: elif self.type == self.NUMBER:
return Schema.str_to_num(value) return Schema.str_to_num(value)
elif self.type == self.STRING: elif self.type == self.STRING:
if value and not isinstance(value, basestring):
raise ValueError()
return str(value) return str(value)
elif self.type == self.BOOLEAN: elif self.type == self.BOOLEAN:
return strutils.bool_from_string(str(value), strict=True) return strutils.bool_from_string(str(value), strict=True)

View File

@ -390,9 +390,12 @@ class SchemaTest(testtools.TestCase):
self.assertIsInstance(res, basestring) self.assertIsInstance(res, basestring)
res = schema.to_schema_type('1') res = schema.to_schema_type('1')
self.assertIsInstance(res, basestring) self.assertIsInstance(res, basestring)
err = self.assertRaises(ValueError, schema.to_schema_type, 1) res = schema.to_schema_type(1)
self.assertEqual('Value "1" is invalid for data type "String".', self.assertIsInstance(res, basestring)
six.text_type(err)) res = schema.to_schema_type(True)
self.assertIsInstance(res, basestring)
res = schema.to_schema_type(None)
self.assertIsInstance(res, basestring)
def test_to_schema_type_boolean(self): def test_to_schema_type_boolean(self):
'''Test Schema.to_schema_type method for type Boolean.''' '''Test Schema.to_schema_type method for type Boolean.'''