[config] coerce more to string when 'type: str' (#72172)

Change:
- When a plugin defines `type: str` on a parameter, treat more kinds of
  input as a string instead of whatever it is parsed as.

Test Plan:
- New unit tests
- CI

Signed-off-by: Rick Elrod <rick@elrod.me>
This commit is contained in:
Rick Elrod 2020-10-14 11:53:08 -05:00 committed by GitHub
parent 6e7a40dd94
commit 3b40c6f3b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 1 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- "config - more types are now automatically coerced to string when ``type: str`` is used and the value is parsed as a different type"

View file

@ -149,7 +149,7 @@ def ensure_type(value, value_type, origin=None):
errmsg = 'dictionary'
elif value_type in ('str', 'string'):
if isinstance(value, (string_types, AnsibleVaultEncryptedUnicode)):
if isinstance(value, (string_types, AnsibleVaultEncryptedUnicode, bool, int, float, complex)):
value = unquote(to_text(value, errors='surrogate_or_strict'))
else:
errmsg = 'string'

View file

View file

@ -60,6 +60,18 @@ ensure_test_data = [
('a', 'string', string_types),
('Café', 'string', string_types),
('', 'string', string_types),
('29', 'str', string_types),
('13.37', 'str', string_types),
('123j', 'string', string_types),
('0x123', 'string', string_types),
('true', 'string', string_types),
('True', 'string', string_types),
(0, 'str', string_types),
(29, 'str', string_types),
(13.37, 'str', string_types),
(123j, 'string', string_types),
(0x123, 'string', string_types),
(True, 'string', string_types),
('None', 'none', type(None))
]