Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't overwrite builtin jinja2 filters with tests #37881

Merged
merged 2 commits into from Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ansible/playbook/conditional.py
Expand Up @@ -176,7 +176,7 @@ def generic_visit(self, node, inside_call=False, inside_yield=False):
)
try:
e = templar.environment.overlay()
e.filters.update(templar._get_filters())
e.filters.update(templar._get_filters(e.filters))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could reverse the update ordering and update templar._get_filters() (or a copy...) with e.filters so that
the e.filters 'wins'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like

custom_filters = templar._get_filters()
custom_filters.update(e.filters)
e.filters = custom_filters

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to override a builtin filter with an ansible provided by filter, such as groupby. We just don't want this to happen with custom tests.

Eventually custom tests will no longer be registered as filters (2.9), but have been kept thus far for backwards compat.

e.tests.update(templar._get_tests())

res = e._parse(conditional, None, None)
Expand Down
7 changes: 5 additions & 2 deletions lib/ansible/template/__init__.py
Expand Up @@ -292,7 +292,7 @@ def __init__(self, loader, shared_loader_obj=None, variables=None):
))
self._no_type_regex = re.compile(r'.*\|\s*(?:%s)\s*(?:%s)?$' % ('|'.join(C.STRING_TYPE_FILTERS), self.environment.variable_end_string))

def _get_filters(self):
def _get_filters(self, builtin_filters):
'''
Returns filter plugins, after loading and caching them if need be
'''
Expand All @@ -304,6 +304,9 @@ def _get_filters(self):

# TODO: Remove registering tests as filters in 2.9
for name, func in self._get_tests().items():
if name in builtin_filters:
# If we have a custom test named the same as a builtin filter, don't register as a filter
continue
self._filters[name] = tests_as_filters_warning(name, func)

for fp in self._filter_loader.all():
Expand Down Expand Up @@ -678,7 +681,7 @@ def do_template(self, data, preserve_trailing_newlines=True, escape_backslashes=
setattr(myenv, key, ast.literal_eval(val.strip()))

# Adds Ansible custom filters and tests
myenv.filters.update(self._get_filters())
myenv.filters.update(self._get_filters(myenv.filters))
myenv.tests.update(self._get_tests())

if escape_backslashes:
Expand Down
7 changes: 6 additions & 1 deletion test/units/template/test_tests_as_filters_warning.py
@@ -1,13 +1,15 @@
from ansible.template import Templar, display
from units.mock.loader import DictDataLoader
from jinja2.filters import FILTERS
from os.path import isabs


def test_tests_as_filters_warning(mocker):
fake_loader = DictDataLoader({
"/path/to/my_file.txt": "foo\n",
})
templar = Templar(loader=fake_loader, variables={})
filters = templar._get_filters()
filters = templar._get_filters(templar.environment.filters)

mocker.patch.object(display, 'deprecated')

Expand All @@ -28,3 +30,6 @@ def test_tests_as_filters_warning(mocker):
display.deprecated.reset_mock()
filters['bool'](True)
assert display.deprecated.call_count == 0

# Ensure custom test does not override builtin filter
assert filters.get('abs') != isabs