Skip to content

Commit

Permalink
Fix de-serialization of non-object arrays #111 (#112)
Browse files Browse the repository at this point in the history
- Fix incorrect JSON de-serialization of non-object arrays. #111
  • Loading branch information
BernieWhite authored Mar 24, 2019
1 parent d0f81ea commit f093774
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 10 additions & 2 deletions src/PSRule/Common/JsonConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions tests/PSRule.Tests/FromFile.Rule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/PSRule.Tests/ObjectFromFile.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
{
"id": "2"
}
],
"array2": [
"1",
"2",
"3"
]
}
}
Expand All @@ -29,6 +34,11 @@
{
"id": "2"
}
],
"array2": [
"1",
"2",
"3"
]
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/PSRule.Tests/ObjectFromFile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ spec:
array:
- id: 1
- id: 2

array2:
- "1"
- "2"
- "3"
---
targetName: TestObject2
spec:
Expand All @@ -19,6 +22,7 @@ spec:
array:
- id: 1
- id: 2
array2: [ "1", "2", "3" ]

---
# Null object
Expand Down
4 changes: 4 additions & 0 deletions tests/PSRule.Tests/ObjectFromFile2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ spec:
array:
- id: 1
- id: 2
array2:
- 1
- 2
- 3

0 comments on commit f093774

Please sign in to comment.