Ansible.Basic - fix when deserialising a json string of an array (#55691)
* Ansible.Basic - fix when deserialising a json string of an array * Added changelog fragment
This commit is contained in:
parent
b9af6847c2
commit
5228133d74
4 changed files with 36 additions and 3 deletions
2
changelogs/fragments/ps-basic-json.yaml
Normal file
2
changelogs/fragments/ps-basic-json.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- Ansible.Basic - Fix issue when deserilizing a JSON string that is not a dictionary - https://github.com/ansible/ansible/pull/55691
|
|
@ -334,7 +334,7 @@ namespace Ansible.Basic
|
||||||
LogEvent(String.Format("[WARNING] {0}", message), EventLogEntryType.Warning);
|
LogEvent(String.Format("[WARNING] {0}", message), EventLogEntryType.Warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<string, object> FromJson(string json) { return FromJson<Dictionary<string, object>>(json); }
|
public static object FromJson(string json) { return FromJson<object>(json); }
|
||||||
public static T FromJson<T>(string json)
|
public static T FromJson<T>(string json)
|
||||||
{
|
{
|
||||||
#if CORECLR
|
#if CORECLR
|
||||||
|
@ -375,7 +375,7 @@ namespace Ansible.Basic
|
||||||
if (args.Length > 0)
|
if (args.Length > 0)
|
||||||
{
|
{
|
||||||
string inputJson = File.ReadAllText(args[0]);
|
string inputJson = File.ReadAllText(args[0]);
|
||||||
Dictionary<string, object> rawParams = FromJson(inputJson);
|
Dictionary<string, object> rawParams = FromJson<Dictionary<string, object>>(inputJson);
|
||||||
if (!rawParams.ContainsKey("ANSIBLE_MODULE_ARGS"))
|
if (!rawParams.ContainsKey("ANSIBLE_MODULE_ARGS"))
|
||||||
throw new ArgumentException("Module was unable to get ANSIBLE_MODULE_ARGS value from the argument path json");
|
throw new ArgumentException("Module was unable to get ANSIBLE_MODULE_ARGS value from the argument path json");
|
||||||
return (IDictionary)rawParams["ANSIBLE_MODULE_ARGS"];
|
return (IDictionary)rawParams["ANSIBLE_MODULE_ARGS"];
|
||||||
|
|
|
@ -58,7 +58,7 @@ Function Assert-DictionaryEquals {
|
||||||
|
|
||||||
if ($actual_value -is [System.Collections.IDictionary]) {
|
if ($actual_value -is [System.Collections.IDictionary]) {
|
||||||
$actual_value | Assert-DictionaryEquals -Expected $expected_value
|
$actual_value | Assert-DictionaryEquals -Expected $expected_value
|
||||||
} elseif ($actual_value -is [System.Collections.ArrayList]) {
|
} elseif ($actual_value -is [System.Collections.ArrayList] -or $actual_value -is [Array]) {
|
||||||
for ($i = 0; $i -lt $actual_value.Count; $i++) {
|
for ($i = 0; $i -lt $actual_value.Count; $i++) {
|
||||||
$actual_entry = $actual_value[$i]
|
$actual_entry = $actual_value[$i]
|
||||||
$expected_entry = $expected_value[$i]
|
$expected_entry = $expected_value[$i]
|
||||||
|
@ -2347,6 +2347,23 @@ test_no_log - Invoked with:
|
||||||
$actual.invocation | Assert-DictionaryEquals -Expected @{module_args = @{}}
|
$actual.invocation | Assert-DictionaryEquals -Expected @{module_args = @{}}
|
||||||
$actual.output | Assert-DictionaryEquals -Expected @{a = "a"; b = "b"}
|
$actual.output | Assert-DictionaryEquals -Expected @{a = "a"; b = "b"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"String json array to object" = {
|
||||||
|
$input_json = '["abc", "def"]'
|
||||||
|
$actual = [Ansible.Basic.AnsibleModule]::FromJson($input_json)
|
||||||
|
$actual -is [Array] | Assert-Equals -Expected $true
|
||||||
|
$actual.Length | Assert-Equals -Expected 2
|
||||||
|
$actual[0] | Assert-Equals -Expected "abc"
|
||||||
|
$actual[1] | Assert-Equals -Expected "def"
|
||||||
|
}
|
||||||
|
|
||||||
|
"String json array of dictionaries to object" = {
|
||||||
|
$input_json = '[{"abc":"def"}]'
|
||||||
|
$actual = [Ansible.Basic.AnsibleModule]::FromJson($input_json)
|
||||||
|
$actual -is [Array] | Assert-Equals -Expected $true
|
||||||
|
$actual.Length | Assert-Equals -Expected 1
|
||||||
|
$actual[0] | Assert-DictionaryEquals -Expected @{"abc" = "def"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -429,3 +429,17 @@
|
||||||
that:
|
that:
|
||||||
- not request_status_code_comma.changed
|
- not request_status_code_comma.changed
|
||||||
- request_status_code_comma.status_code == 202
|
- request_status_code_comma.status_code == 202
|
||||||
|
|
||||||
|
# https://github.com/ansible/ansible/issues/55294
|
||||||
|
- name: get json content that is an array
|
||||||
|
win_uri:
|
||||||
|
url: https://{{httpbin_host}}/base64/{{ '[{"abc":"def"}]' | b64encode }}
|
||||||
|
return_content: yes
|
||||||
|
register: content_array
|
||||||
|
|
||||||
|
- name: assert content of json array
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- not content_array is changed
|
||||||
|
- content_array.content == '[{"abc":"def"}]'
|
||||||
|
- content_array.json == [{"abc":"def"}]
|
||||||
|
|
Loading…
Reference in a new issue