Skip to content

Commit

Permalink
json_query: Handle AnsibleUnicode, AnsibleUnsafeText (#1434)
Browse files Browse the repository at this point in the history
jmespath library does not undestand custom string types
such as AnsibleUnicode, and AnsibleUnsafeText.
So user need to use ``to_json | from_json`` filter while using
functions like ``starts_with`` and ``contains`` etc.
This hack will allow user to get rid of this filter.

Fixes: #320

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Dec 4, 2020
1 parent 4737104 commit 5319437
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/320_unsafe_text.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- json_query - handle ``AnsibleUnicode`` and ``AnsibleUnsafeText`` (https://github.com/ansible-collections/community.general/issues/320).
3 changes: 3 additions & 0 deletions plugins/filter/json_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def json_query(data, expr):
raise AnsibleError('You need to install "jmespath" prior to running '
'json_query filter')

# Hack to handle Ansible String Types
# See issue: https://github.com/ansible-collections/community.general/issues/320
jmespath.functions.REVERSE_TYPES_MAP['string'] = jmespath.functions.REVERSE_TYPES_MAP['string'] + ('AnsibleUnicode', 'AnsibleUnsafeText', )
try:
return jmespath.search(expr, data)
except jmespath.exceptions.JMESPathError as e:
Expand Down

0 comments on commit 5319437

Please sign in to comment.