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

(cherry picked from commit 356bf336bd)
pull/85255/head
Matt Davis 6 months ago committed by Matt Davis
parent fa2f1e4750
commit 40a675543f

@ -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): def from_yaml(data):
if data is None:
return None
if isinstance(data, string_types): if isinstance(data, string_types):
# The ``text_type`` call here strips any custom # The ``text_type`` call here strips any custom
# string wrapper class, so that CSafeLoader can # string wrapper class, so that CSafeLoader can
# read the data # read the data
return yaml_load(text_type(to_text(data, errors='surrogate_or_strict'))) 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 return data
def from_yaml_all(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): if isinstance(data, string_types):
# The ``text_type`` call here strips any custom # The ``text_type`` call here strips any custom
# string wrapper class, so that CSafeLoader can # string wrapper class, so that CSafeLoader can
# read the data # read the data
return yaml_load_all(text_type(to_text(data, errors='surrogate_or_strict'))) 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 return data

@ -471,6 +471,8 @@
- "2|from_yaml_all == 2" - "2|from_yaml_all == 2"
- "unsafe_fruit|from_yaml == {'bananas': 'yellow', 'apples': 'red'}" - "unsafe_fruit|from_yaml == {'bananas': 'yellow', 'apples': 'red'}"
- "unsafe_fruit_all|from_yaml_all|list == [{'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: vars:
unsafe_fruit: !unsafe | unsafe_fruit: !unsafe |
--- ---

Loading…
Cancel
Save