From 43e6fc479b6a490d7220a09b84fff5e76de56641 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Wed, 24 Apr 2019 16:14:07 +1000 Subject: [PATCH 1/2] Ansible.Basic - fix when deserialising a json string of an array --- .../module_utils/csharp/Ansible.Basic.cs | 4 ++-- .../library/ansible_basic_tests.ps1 | 19 ++++++++++++++++++- .../targets/win_uri/tasks/main.yml | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/ansible/module_utils/csharp/Ansible.Basic.cs b/lib/ansible/module_utils/csharp/Ansible.Basic.cs index a67f67fb36927b..6a7d03fdcb502d 100644 --- a/lib/ansible/module_utils/csharp/Ansible.Basic.cs +++ b/lib/ansible/module_utils/csharp/Ansible.Basic.cs @@ -334,7 +334,7 @@ public void Warn(string message) LogEvent(String.Format("[WARNING] {0}", message), EventLogEntryType.Warning); } - public static Dictionary FromJson(string json) { return FromJson>(json); } + public static object FromJson(string json) { return FromJson(json); } public static T FromJson(string json) { #if CORECLR @@ -375,7 +375,7 @@ public static IDictionary GetParams(string[] args) if (args.Length > 0) { string inputJson = File.ReadAllText(args[0]); - Dictionary rawParams = FromJson(inputJson); + Dictionary rawParams = FromJson>(inputJson); if (!rawParams.ContainsKey("ANSIBLE_MODULE_ARGS")) throw new ArgumentException("Module was unable to get ANSIBLE_MODULE_ARGS value from the argument path json"); return (IDictionary)rawParams["ANSIBLE_MODULE_ARGS"]; diff --git a/test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1 b/test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1 index 81a0156dd0cbe5..b5ef52cc46780b 100644 --- a/test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1 +++ b/test/integration/targets/win_csharp_utils/library/ansible_basic_tests.ps1 @@ -58,7 +58,7 @@ Function Assert-DictionaryEquals { if ($actual_value -is [System.Collections.IDictionary]) { $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++) { $actual_entry = $actual_value[$i] $expected_entry = $expected_value[$i] @@ -2347,6 +2347,23 @@ test_no_log - Invoked with: $actual.invocation | Assert-DictionaryEquals -Expected @{module_args = @{}} $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 { diff --git a/test/integration/targets/win_uri/tasks/main.yml b/test/integration/targets/win_uri/tasks/main.yml index ca6e4a41b4df3d..3243f1eb211fa0 100644 --- a/test/integration/targets/win_uri/tasks/main.yml +++ b/test/integration/targets/win_uri/tasks/main.yml @@ -429,3 +429,17 @@ that: - not request_status_code_comma.changed - 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"}] From 554e4693e0d56b5d0626bbb7b368b9abf9d38952 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Wed, 1 May 2019 10:38:49 +1000 Subject: [PATCH 2/2] Added changelog fragment --- changelogs/fragments/ps-basic-json.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/ps-basic-json.yaml diff --git a/changelogs/fragments/ps-basic-json.yaml b/changelogs/fragments/ps-basic-json.yaml new file mode 100644 index 00000000000000..d8a6f2835963f2 --- /dev/null +++ b/changelogs/fragments/ps-basic-json.yaml @@ -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