From f093774cbef03f64c90f201a19cb83aedadf202f Mon Sep 17 00:00:00 2001 From: Bernie White Date: Sun, 24 Mar 2019 20:17:32 +1000 Subject: [PATCH] Fix de-serialization of non-object arrays #111 (#112) - Fix incorrect JSON de-serialization of non-object arrays. #111 --- CHANGELOG.md | 1 + src/PSRule/Common/JsonConverters.cs | 12 ++++++++++-- tests/PSRule.Tests/FromFile.Rule.ps1 | 1 + tests/PSRule.Tests/ObjectFromFile.json | 10 ++++++++++ tests/PSRule.Tests/ObjectFromFile.yaml | 6 +++++- tests/PSRule.Tests/ObjectFromFile2.yaml | 4 ++++ 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fb2668c10..5cc0c930e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased - Fix incorrect JSON de-serialization of nested arrays. [#109](https://github.com/BernieWhite/PSRule/issues/109) +- Fix incorrect JSON de-serialization of non-object arrays. [#111](https://github.com/BernieWhite/PSRule/issues/111) ## v0.4.0-B190311 (pre-release) diff --git a/src/PSRule/Common/JsonConverters.cs b/src/PSRule/Common/JsonConverters.cs index 580d934ac1..58b62a42fc 100644 --- a/src/PSRule/Common/JsonConverters.cs +++ b/src/PSRule/Common/JsonConverters.cs @@ -183,8 +183,16 @@ private PSObject ReadObject(JsonReader reader) while (reader.TokenType != JsonToken.EndArray) { - var item = ReadObject(reader: reader); - items.Add(item); + if (reader.TokenType == JsonToken.StartObject) + { + var item = ReadObject(reader: reader); + items.Add(item); + } + else + { + items.Add(PSObject.AsPSObject(reader.Value)); + } + reader.Read(); } diff --git a/tests/PSRule.Tests/FromFile.Rule.ps1 b/tests/PSRule.Tests/FromFile.Rule.ps1 index b8a9b69e17..71f0d2623e 100644 --- a/tests/PSRule.Tests/FromFile.Rule.ps1 +++ b/tests/PSRule.Tests/FromFile.Rule.ps1 @@ -133,6 +133,7 @@ Rule 'WithConfiguration' { Rule 'WithFormat' { $TargetObject.spec.properties.kind -eq 'Test' ($TargetObject.spec.properties.array.id | Measure-Object).Count -eq 2 + ($TargetObject.spec.properties.array2 | Measure-Object).Count -eq 3 } # Description: Test for Hint keyword diff --git a/tests/PSRule.Tests/ObjectFromFile.json b/tests/PSRule.Tests/ObjectFromFile.json index 532087b137..d0aeb89b9e 100644 --- a/tests/PSRule.Tests/ObjectFromFile.json +++ b/tests/PSRule.Tests/ObjectFromFile.json @@ -12,6 +12,11 @@ { "id": "2" } + ], + "array2": [ + "1", + "2", + "3" ] } } @@ -29,6 +34,11 @@ { "id": "2" } + ], + "array2": [ + "1", + "2", + "3" ] } } diff --git a/tests/PSRule.Tests/ObjectFromFile.yaml b/tests/PSRule.Tests/ObjectFromFile.yaml index e703927c0f..f6793ad7e3 100644 --- a/tests/PSRule.Tests/ObjectFromFile.yaml +++ b/tests/PSRule.Tests/ObjectFromFile.yaml @@ -9,7 +9,10 @@ spec: array: - id: 1 - id: 2 - + array2: + - "1" + - "2" + - "3" --- targetName: TestObject2 spec: @@ -19,6 +22,7 @@ spec: array: - id: 1 - id: 2 + array2: [ "1", "2", "3" ] --- # Null object diff --git a/tests/PSRule.Tests/ObjectFromFile2.yaml b/tests/PSRule.Tests/ObjectFromFile2.yaml index ef0a0805d5..645e1c18da 100644 --- a/tests/PSRule.Tests/ObjectFromFile2.yaml +++ b/tests/PSRule.Tests/ObjectFromFile2.yaml @@ -9,3 +9,7 @@ spec: array: - id: 1 - id: 2 + array2: + - 1 + - 2 + - 3