1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-05-06 18:15:54 +00:00

xml: fail for non-string values (#11959)

* fix(xml): coerce boolean values to string with a warning

Fixes #7171

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test(xml): add integration tests for boolean value handling

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* changelog: add fragment for PR 11959

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* adjustments from review

* test(xml): update boolean-value integration tests to expect failure

Now that xml fails on non-string values, replace the old success-path
tests with failure assertions and add a positive test for quoted strings.
Remove the no-longer-needed result XML fixtures.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* adjustments from review

* fix(xml): correct boolean test assertions to match actual error message format

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Alexei Znamensky 2026-05-07 05:23:58 +12:00 committed by GitHub
parent 60cb5cd679
commit d87a8a167f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 74 additions and 0 deletions

View file

@ -0,0 +1,4 @@
bugfixes:
- "xml - emit an error when ``value`` is not a string, pointing to the offending xpath
(https://github.com/ansible-collections/community.general/issues/7171,
https://github.com/ansible-collections/community.general/pull/11959)."

View file

@ -695,6 +695,16 @@ def set_target_inner(module, tree, xpath, namespaces, attribute, value):
msg=f"Xpath {xpath} does not reference a node! tree is {etree.tostring(tree, pretty_print=True)}"
)
if not isinstance(value, str):
target = f"attribute '{attribute}' at xpath '{xpath}'" if attribute else f"element text at xpath '{xpath}'"
module.fail_json(
msg=(
f"A non-string value {value!r} was parsed for {target}. "
"YAML values for booleans, octals, floats may not yield the string you intended. "
"""Quote the value to be explicit, like `value: "yes"`."""
)
)
for element in tree.xpath(xpath, namespaces=namespaces):
if not attribute:
changed = changed or (element.text != value)

View file

@ -60,6 +60,7 @@
- include_tasks: test-remove-element.yml
- include_tasks: test-remove-element-nochange.yml
- include_tasks: test-set-attribute-value.yml
- include_tasks: test-set-attribute-value-boolean.yml
- include_tasks: test-set-children-elements.yml
- include_tasks: test-set-children-elements-level.yml
- include_tasks: test-set-children-elements-value.yml

View file

@ -0,0 +1,59 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Regression test for https://github.com/ansible-collections/community.general/issues/7171
# Passing an unquoted YAML boolean as `value` must now fail with an explicit error.
- name: Setup test fixture
copy:
src: fixtures/ansible-xml-beers.xml
dest: /tmp/ansible-xml-beers.xml
- name: "Set '/business/rating/@subjective' to boolean false (unquoted YAML boolean) - must fail"
xml:
path: /tmp/ansible-xml-beers.xml
xpath: /business/rating
attribute: subjective
value: false
register: set_attribute_boolean_false
ignore_errors: true
- name: Assert that passing a boolean attribute value raises an error
assert:
that:
- set_attribute_boolean_false is failed
- "'was parsed for' in set_attribute_boolean_false.msg"
- "'attribute' in set_attribute_boolean_false.msg"
- name: "Set '/business/rating' text content to boolean true (unquoted YAML boolean) - must fail"
xml:
path: /tmp/ansible-xml-beers.xml
xpath: /business/rating
value: true
register: set_element_boolean_true
ignore_errors: true
- name: Assert that passing a boolean element value raises an error
assert:
that:
- set_element_boolean_true is failed
- "'was parsed for' in set_element_boolean_true.msg"
- "'element text' in set_element_boolean_true.msg"
- name: "Set '/business/rating/@subjective' to quoted string 'false' - must succeed"
xml:
path: /tmp/ansible-xml-beers.xml
xpath: /business/rating
attribute: subjective
value: "false"
register: set_attribute_string_false
- name: Assert that passing a quoted string attribute value succeeds
assert:
that:
- set_attribute_string_false is changed