From 540e0d89632eaf1ab9d430d2c0560f6e16fc56b4 Mon Sep 17 00:00:00 2001 From: Emanuele Bernardi Date: Thu, 25 Jul 2024 18:50:32 +0200 Subject: [PATCH] fix from_xml: return python dictionary (#361) * removed the in between json conversion, now function returns python dict, changed unit tests to check against the dict. * remove the json import * changelog added * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * changed changelog to minor --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ruchi Pakhle <72685035+Ruchip16@users.noreply.github.com> Co-authored-by: Nilashish Chakraborty --- changelogs/fragments/fix_from_xml.yaml | 3 +++ plugins/plugin_utils/from_xml.py | 4 +--- tests/unit/plugins/filter/test_from_xml.py | 8 ++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/fix_from_xml.yaml diff --git a/changelogs/fragments/fix_from_xml.yaml b/changelogs/fragments/fix_from_xml.yaml new file mode 100644 index 0000000..9553d0a --- /dev/null +++ b/changelogs/fragments/fix_from_xml.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - The from_xml filter returns a python dictionary instead of a json string. diff --git a/plugins/plugin_utils/from_xml.py b/plugins/plugin_utils/from_xml.py index 9216a05..eb1194e 100644 --- a/plugins/plugin_utils/from_xml.py +++ b/plugins/plugin_utils/from_xml.py @@ -13,8 +13,6 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -import json - from ansible.errors import AnsibleFilterError @@ -46,7 +44,7 @@ def from_xml(data, engine): if not HAS_XMLTODICT: _raise_error("Missing required library xmltodict") try: - res = json.dumps(xmltodict.parse(data)) + res = xmltodict.parse(data) except Exception: _raise_error("Input Xml is not valid") return res diff --git a/tests/unit/plugins/filter/test_from_xml.py b/tests/unit/plugins/filter/test_from_xml.py index 4643f53..18c2b0a 100644 --- a/tests/unit/plugins/filter/test_from_xml.py +++ b/tests/unit/plugins/filter/test_from_xml.py @@ -22,8 +22,12 @@ VALID_DATA = ( "" ) -OUTPUT = """{"netconf-state": \ -{"@xmlns": "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", "schemas": {"schema": null}}}""" +OUTPUT = { + "netconf-state": { + "@xmlns": "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", + "schemas": {"schema": None}, + }, +} class TestFromXml(TestCase):