mirror of
https://github.com/ansible/ansible.git
synced 2026-08-03 00:22:10 +02:00
Fix filter plugin result processing (#85653)
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
bugfixes:
|
||||
- templating - Ensure filter plugin result processing occurs under the correct call context.
|
||||
(https://github.com/ansible/ansible/issues/85585)
|
||||
@@ -114,7 +114,13 @@ class JinjaPluginIntercept(c.MutableMapping):
|
||||
|
||||
try:
|
||||
with JinjaCallContext(accept_lazy_markers=instance.accept_lazy_markers):
|
||||
return instance.j2_function(*lazify_container_args(args), **lazify_container_kwargs(kwargs))
|
||||
result = instance.j2_function(*lazify_container_args(args), **lazify_container_kwargs(kwargs))
|
||||
|
||||
if instance.plugin_type == 'filter':
|
||||
# ensure list conversion occurs under the call context
|
||||
result = _wrap_plugin_output(result)
|
||||
|
||||
return result
|
||||
except MarkerError as ex:
|
||||
return ex.source
|
||||
except Exception as ex:
|
||||
@@ -155,7 +161,6 @@ class JinjaPluginIntercept(c.MutableMapping):
|
||||
@functools.wraps(instance.j2_function)
|
||||
def wrapper(*args, **kwargs) -> t.Any:
|
||||
result = self._invoke_plugin(instance, *args, **kwargs)
|
||||
result = _wrap_plugin_output(result)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -1078,3 +1078,18 @@ def test_marker_from_test_plugin() -> None:
|
||||
"""Verify test plugins can raise MarkerError to return a Marker, and that no warnings or deprecations are emitted."""
|
||||
with emits_warnings(deprecation_pattern=[], warning_pattern=[]):
|
||||
assert TemplateEngine(variables=dict(something=TRUST.tag("{{ nope }}"))).template(TRUST.tag("{{ (something is eq {}) is undefined }}"))
|
||||
|
||||
|
||||
def test_filter_generator() -> None:
|
||||
"""Verify that filters which return a generator are converted to a list while under the filter's JinjaCallContext."""
|
||||
variables = dict(
|
||||
foo=[
|
||||
dict(x=1, optional_var=0),
|
||||
dict(x=2),
|
||||
],
|
||||
bar=TRUST.tag("{{ foo | selectattr('optional_var', 'defined') }}"),
|
||||
)
|
||||
|
||||
te = TemplateEngine(variables=variables)
|
||||
te.template(TRUST.tag("{{ bar }}"))
|
||||
te.template(TRUST.tag("{{ lookup('vars', 'bar') }}"))
|
||||
|
||||
Reference in New Issue
Block a user