mirror of
https://github.com/zebrajr/ansible.git
synced 2025-12-06 12:19:53 +01:00
fix from_yaml_all filter inconsistent None handling (#85223)
* fix from_yaml_all filter inconsistent None handling * always returns empty list for None or empty string input * deprecate non-string inputs for from_yaml and from_yaml_all
This commit is contained in:
parent
f05b1d1ccf
commit
356bf336bd
2
changelogs/fragments/from_yaml_all.yml
Normal file
2
changelogs/fragments/from_yaml_all.yml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
bugfixes:
|
||||
- from_yaml_all filter - `None` and empty string inputs now always return an empty list. Previously, `None` was returned in Jinja native mode and empty list in classic mode.
|
||||
|
|
@ -247,20 +247,30 @@ def regex_escape(string, re_type='python'):
|
|||
|
||||
|
||||
def from_yaml(data):
|
||||
if data is None:
|
||||
return None
|
||||
|
||||
if isinstance(data, string_types):
|
||||
# The ``text_type`` call here strips any custom
|
||||
# string wrapper class, so that CSafeLoader can
|
||||
# read the data
|
||||
return yaml_load(text_type(to_text(data, errors='surrogate_or_strict')))
|
||||
|
||||
display.deprecated(f"The from_yaml filter ignored non-string input of type {native_type_name(data)!r}.", version='2.23', obj=data)
|
||||
return data
|
||||
|
||||
|
||||
def from_yaml_all(data):
|
||||
if data is None:
|
||||
return [] # backward compatibility; ensure consistent result between classic/native Jinja for None/empty string input
|
||||
|
||||
if isinstance(data, string_types):
|
||||
# The ``text_type`` call here strips any custom
|
||||
# string wrapper class, so that CSafeLoader can
|
||||
# read the data
|
||||
return yaml_load_all(text_type(to_text(data, errors='surrogate_or_strict')))
|
||||
|
||||
display.deprecated(f"The from_yaml_all filter ignored non-string input of type {native_type_name(data)!r}.", version='2.23', obj=data)
|
||||
return data
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -471,6 +471,8 @@
|
|||
- "2|from_yaml_all == 2"
|
||||
- "unsafe_fruit|from_yaml == {'bananas': 'yellow', 'apples': 'red'}"
|
||||
- "unsafe_fruit_all|from_yaml_all|list == [{'bananas': 'yellow'}, {'apples': 'red'}]"
|
||||
- None | from_yaml is none
|
||||
- None | from_yaml_all == []
|
||||
vars:
|
||||
unsafe_fruit: !unsafe |
|
||||
---
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user