From 940b7af7d8242d40c59fc05a6ef4db3f3629552c Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Thu, 25 Mar 2021 00:17:07 +0100 Subject: [PATCH] argspec validation - fix apply_defaults (#74029) * Add test --- .../74029-argspec-apply_defaults.yml | 2 ++ lib/ansible/module_utils/common/parameters.py | 4 +-- .../targets/argspec/library/argspec.py | 13 +++++++++ .../targets/argspec/tasks/main.yml | 29 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/74029-argspec-apply_defaults.yml diff --git a/changelogs/fragments/74029-argspec-apply_defaults.yml b/changelogs/fragments/74029-argspec-apply_defaults.yml new file mode 100644 index 00000000000..ec7c56e09c8 --- /dev/null +++ b/changelogs/fragments/74029-argspec-apply_defaults.yml @@ -0,0 +1,2 @@ +bugfixes: +- "argument spec validation - fix behavior of ``apply_defaults=True`` when an empty dictionary is specified for such an option (https://github.com/ansible/ansible/pull/74029)." diff --git a/lib/ansible/module_utils/common/parameters.py b/lib/ansible/module_utils/common/parameters.py index e297573410a..972db2d3faf 100644 --- a/lib/ansible/module_utils/common/parameters.py +++ b/lib/ansible/module_utils/common/parameters.py @@ -702,8 +702,8 @@ def _validate_sub_spec(argument_spec, parameters, prefix='', options_context=Non if sub_spec is not None: if parameters.get(param) is None: parameters[param] = {} - else: - continue + else: + continue elif sub_spec is None or param not in parameters or parameters[param] is None: continue diff --git a/test/integration/targets/argspec/library/argspec.py b/test/integration/targets/argspec/library/argspec.py index 08dad1a0494..1a1d288d238 100644 --- a/test/integration/targets/argspec/library/argspec.py +++ b/test/integration/targets/argspec/library/argspec.py @@ -126,6 +126,19 @@ def main(): 'int': { 'type': 'int', }, + 'apply_defaults': { + 'type': 'dict', + 'apply_defaults': True, + 'options': { + 'foo': { + 'type': 'str', + }, + 'bar': { + 'type': 'str', + 'default': 'baz', + }, + }, + }, }, required_if=( ('state', 'present', ('path', 'content'), True), diff --git a/test/integration/targets/argspec/tasks/main.yml b/test/integration/targets/argspec/tasks/main.yml index d90bdf02454..283c922d789 100644 --- a/test/integration/targets/argspec/tasks/main.yml +++ b/test/integration/targets/argspec/tasks/main.yml @@ -342,6 +342,30 @@ register: argspec_int_invalid ignore_errors: true +- argspec: + required: value + required_one_of_one: value + register: argspec_apply_defaults_not_specified + +- argspec: + required: value + required_one_of_one: value + apply_defaults: ~ + register: argspec_apply_defaults_none + +- argspec: + required: value + required_one_of_one: value + apply_defaults: {} + register: argspec_apply_defaults_empty + +- argspec: + required: value + required_one_of_one: value + apply_defaults: + foo: bar + register: argspec_apply_defaults_one + - assert: that: - argspec_required_fail is failed @@ -417,3 +441,8 @@ - argspec_password_no_log.stdout|regex_findall('VALUE_SPECIFIED_IN_NO_LOG_PARAMETER')|length == 1 - argspec_int_invalid is failed + + - "argspec_apply_defaults_not_specified.apply_defaults == {'foo': none, 'bar': 'baz'}" + - "argspec_apply_defaults_none.apply_defaults == {'foo': none, 'bar': 'baz'}" + - "argspec_apply_defaults_empty.apply_defaults == {'foo': none, 'bar': 'baz'}" + - "argspec_apply_defaults_one.apply_defaults == {'foo': 'bar', 'bar': 'baz'}"