mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 18:15:54 +00:00
[PR #11959/d87a8a16 backport][stable-12] xml: fail for non-string values (#12001)
xml: fail for non-string `value`s (#11959)
* fix(xml): coerce boolean values to string with a warning
Fixes #7171
* test(xml): add integration tests for boolean value handling
* changelog: add fragment for PR 11959
* 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.
* adjustments from review
* fix(xml): correct boolean test assertions to match actual error message format
---------
(cherry picked from commit d87a8a167f)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
caeafeec1f
commit
7f688e78bf
4 changed files with 74 additions and 0 deletions
4
changelogs/fragments/11959-xml-boolean-value.yml
Normal file
4
changelogs/fragments/11959-xml-boolean-value.yml
Normal 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)."
|
||||
|
|
@ -688,6 +688,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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue