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:
Matt Davis 2025-05-29 09:50:01 -07:00 committed by GitHub
parent f05b1d1ccf
commit 356bf336bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 0 deletions

View 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.

View File

@ -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

View File

@ -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 |
---