Browse code

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

(cherry picked from commit 5228133d742fc35ce9f3ada2d22729b525b2f417)

Jordan Borean authored on 2019/05/01 10:08:23
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+bugfixes:
1
+- 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
334 334
             LogEvent(String.Format("[WARNING] {0}", message), EventLogEntryType.Warning);
335 335
         }
336 336
 
337
-        public static Dictionary<string, object> FromJson(string json) { return FromJson<Dictionary<string, object>>(json); }
337
+        public static object FromJson(string json) { return FromJson<object>(json); }
338 338
         public static T FromJson<T>(string json)
339 339
         {
340 340
 #if CORECLR
... ...
@@ -375,7 +375,7 @@ namespace Ansible.Basic
375 375
             if (args.Length > 0)
376 376
             {
377 377
                 string inputJson = File.ReadAllText(args[0]);
378
-                Dictionary<string, object> rawParams = FromJson(inputJson);
378
+                Dictionary<string, object> rawParams = FromJson<Dictionary<string, object>>(inputJson);
379 379
                 if (!rawParams.ContainsKey("ANSIBLE_MODULE_ARGS"))
380 380
                     throw new ArgumentException("Module was unable to get ANSIBLE_MODULE_ARGS value from the argument path json");
381 381
                 return (IDictionary)rawParams["ANSIBLE_MODULE_ARGS"];
... ...
@@ -58,7 +58,7 @@ Function Assert-DictionaryEquals {
58 58
 
59 59
         if ($actual_value -is [System.Collections.IDictionary]) {
60 60
             $actual_value | Assert-DictionaryEquals -Expected $expected_value
61
-        } elseif ($actual_value -is [System.Collections.ArrayList]) {
61
+        } elseif ($actual_value -is [System.Collections.ArrayList] -or $actual_value -is [Array]) {
62 62
             for ($i = 0; $i -lt $actual_value.Count; $i++) {
63 63
                 $actual_entry = $actual_value[$i]
64 64
                 $expected_entry = $expected_value[$i]
... ...
@@ -2347,6 +2347,23 @@ test_no_log - Invoked with:
2347 2347
         $actual.invocation | Assert-DictionaryEquals -Expected @{module_args = @{}}
2348 2348
         $actual.output | Assert-DictionaryEquals -Expected @{a = "a"; b = "b"}
2349 2349
     }
2350
+
2351
+    "String json array to object" = {
2352
+        $input_json = '["abc", "def"]'
2353
+        $actual = [Ansible.Basic.AnsibleModule]::FromJson($input_json)
2354
+        $actual -is [Array] | Assert-Equals -Expected $true
2355
+        $actual.Length | Assert-Equals -Expected 2
2356
+        $actual[0] | Assert-Equals -Expected "abc"
2357
+        $actual[1] | Assert-Equals -Expected "def"
2358
+    }
2359
+
2360
+    "String json array of dictionaries to object" = {
2361
+        $input_json = '[{"abc":"def"}]'
2362
+        $actual = [Ansible.Basic.AnsibleModule]::FromJson($input_json)
2363
+        $actual -is [Array] | Assert-Equals -Expected $true
2364
+        $actual.Length | Assert-Equals -Expected 1
2365
+        $actual[0] | Assert-DictionaryEquals -Expected @{"abc" = "def"}
2366
+    }
2350 2367
 }
2351 2368
 
2352 2369
 try {
... ...
@@ -429,3 +429,17 @@
429 429
     that:
430 430
     - not request_status_code_comma.changed
431 431
     - request_status_code_comma.status_code == 202
432
+
433
+# https://github.com/ansible/ansible/issues/55294
434
+- name: get json content that is an array
435
+  win_uri:
436
+    url: https://{{httpbin_host}}/base64/{{ '[{"abc":"def"}]' | b64encode }}
437
+    return_content: yes
438
+  register: content_array
439
+
440
+- name: assert content of json array
441
+  assert:
442
+    that:
443
+    - not content_array is changed
444
+    - content_array.content == '[{"abc":"def"}]'
445
+    - content_array.json == [{"abc":"def"}]