Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argspec validation: fix apply_defaults #74029

Merged
merged 4 commits into from Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions 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)."
4 changes: 2 additions & 2 deletions lib/ansible/module_utils/common/parameters.py
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions test/integration/targets/argspec/library/argspec.py
Expand Up @@ -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),
Expand Down
29 changes: 29 additions & 0 deletions test/integration/targets/argspec/tasks/main.yml
Expand Up @@ -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
Expand Down Expand Up @@ -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'}"