Skip to content

Commit 03cad1f

Browse files
Enforce -Depth on nested sequences by routing through ConvertTo-YamlNode; fix enum serialization for unsigned underlying types; add tests for depth truncation of nested arrays and sequence-under-mapping, and unsigned enum serialization
1 parent da93f35 commit 03cad1f

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/functions/private/ConvertTo-YamlMapping.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
$null = $Builder.Append(' []').AppendLine()
5454
} else {
5555
$null = $Builder.AppendLine()
56-
ConvertTo-YamlSequence -Value $rawVal -Builder $Builder -Level ($Level + 1) -CurrentDepth ($CurrentDepth + 1) -Options $Options
56+
ConvertTo-YamlNode -Value $val -Builder $Builder -Level ($Level + 1) -CurrentDepth ($CurrentDepth + 1) -Options $Options
5757
}
5858
continue
5959
}

src/functions/private/ConvertTo-YamlSequence.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
$null = $Builder.Append($prefix).Append($keyText).Append(': []').AppendLine()
7676
} else {
7777
$null = $Builder.Append($prefix).Append($keyText).Append(':').AppendLine()
78-
ConvertTo-YamlSequence -Value $rawVal -Builder $Builder -Level ($Level + 2) -CurrentDepth ($CurrentDepth + 1) -Options $Options
78+
ConvertTo-YamlNode -Value $val -Builder $Builder -Level ($Level + 2) -CurrentDepth ($CurrentDepth + 1) -Options $Options
7979
}
8080
continue
8181
}
@@ -92,7 +92,7 @@
9292
$null = $Builder.Append($indent).Append('- []').AppendLine()
9393
} else {
9494
$null = $Builder.Append($indent).Append('-').AppendLine()
95-
ConvertTo-YamlSequence -Value $rawItem -Builder $Builder -Level ($Level + 1) -CurrentDepth ($CurrentDepth + 1) -Options $Options
95+
ConvertTo-YamlNode -Value $item -Builder $Builder -Level ($Level + 1) -CurrentDepth ($CurrentDepth + 1) -Options $Options
9696
}
9797
continue
9898
}

src/functions/private/Format-YamlScalar.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
if ($Options.EnumsAsStrings) {
1919
return Format-YamlString -Text ($Value.ToString())
2020
}
21-
return ([int64] $Value).ToString([cultureinfo]::InvariantCulture)
21+
$underlyingType = [System.Enum]::GetUnderlyingType($Value.GetType())
22+
$numeric = [System.Convert]::ChangeType($Value, $underlyingType)
23+
return ([System.IConvertible]$numeric).ToString([cultureinfo]::InvariantCulture)
2224
}
2325

2426
if ($Value -is [byte] -or $Value -is [sbyte] -or

tests/ConvertTo-Yaml.Tests.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,16 @@ Describe 'ConvertTo-Yaml' {
286286
$yaml = $obj | ConvertTo-Yaml
287287
$yaml.Trim() | Should -Be 'day: 1'
288288
}
289+
290+
It 'Serializes enums with unsigned underlying type correctly' {
291+
# System.Security.AccessControl.FileSystemRights has UInt32 underlying type
292+
# and values that can exceed Int32.MaxValue
293+
$val = [System.Security.AccessControl.FileSystemRights]::FullControl
294+
$obj = [ordered]@{ rights = $val }
295+
$yaml = $obj | ConvertTo-Yaml
296+
$expected = [int]$val
297+
$yaml.Trim() | Should -Be "rights: $expected"
298+
}
289299
}
290300

291301
Context '-Depth' {
@@ -326,6 +336,26 @@ Describe 'ConvertTo-Yaml' {
326336
$depthLine = $lines[($lines.IndexOf($bLine) + 1)]
327337
$depthLine | Should -Match '^\s{4}'
328338
}
339+
340+
It 'Truncates deeply nested sequences beyond -Depth' {
341+
$inner = ,('innermost')
342+
$middle = ,$inner
343+
$outer = ,$middle
344+
$yaml = ConvertTo-Yaml -InputObject $outer -Depth 1 -WarningAction SilentlyContinue
345+
$yaml | Should -Not -BeNullOrEmpty
346+
$yaml | Should -Not -Match 'innermost'
347+
}
348+
349+
It 'Truncates sequences under mapping keys beyond -Depth' {
350+
$obj = [ordered]@{
351+
a = [ordered]@{
352+
b = @(1, 2, 3)
353+
}
354+
}
355+
$yaml = $obj | ConvertTo-Yaml -Depth 1 -WarningAction SilentlyContinue
356+
$yaml | Should -Not -BeNullOrEmpty
357+
$yaml | Should -Not -Match '- 1'
358+
}
329359
}
330360

331361
Context 'Empty collections' {

0 commit comments

Comments
 (0)