Skip to content

Commit 7795f14

Browse files
Preserve array records in YAML exports
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 60583eb commit 7795f14

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

src/functions/public/Export-Yaml.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,10 @@ function Export-Yaml {
412412
}
413413
}
414414

415-
$value = if ($values.Count -eq 1) {
416-
[object] $values[0]
415+
if ($values.Count -eq 1) {
416+
$value = [object] $values[0]
417417
} else {
418-
[object[]] $values.ToArray()
418+
$value = [object[]] $values.ToArray()
419419
}
420420
$serializerParameters = @{
421421
InputObject = $value

tests/Export-Yaml.Tests.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ Describe 'Export-Yaml' {
6767
$result | Should -Be @('one', 'two')
6868
}
6969

70+
It 'preserves an explicit one-element array as a sequence' {
71+
$path = Join-Path $TestDrive 'one-element-array.yaml'
72+
73+
Export-Yaml -InputObject @(42) -Path $path
74+
75+
[System.IO.File]::ReadAllText($path) | Should -Be "- 42`n"
76+
}
77+
78+
It 'preserves an explicit empty array as an empty sequence' {
79+
$path = Join-Path $TestDrive 'empty-array.yaml'
80+
81+
Export-Yaml -InputObject @() -Path $path
82+
83+
[System.IO.File]::ReadAllText($path) | Should -Be "[]`n"
84+
}
85+
7086
It 'collects multiple pipeline records into one sequence' {
7187
$path = Join-Path $TestDrive 'pipeline.yaml'
7288

@@ -76,6 +92,30 @@ Describe 'Export-Yaml' {
7692
$result | Should -Be @('one', 'two', 'three')
7793
}
7894

95+
It 'distinguishes one nested pipeline record from multiple records' {
96+
$singlePath = Join-Path $TestDrive 'single-nested-record.yaml'
97+
$multiplePath = Join-Path $TestDrive 'multiple-nested-records.yaml'
98+
$firstRecord = [object[]]::new(1)
99+
$firstRecord[0] = [object[]] @(1, 2)
100+
$secondRecord = [object[]]::new(1)
101+
$secondRecord[0] = [object[]] @(3, 4)
102+
103+
Write-Output -InputObject $firstRecord -NoEnumerate |
104+
Export-Yaml -Path $singlePath
105+
& {
106+
Write-Output -InputObject $firstRecord -NoEnumerate
107+
Write-Output -InputObject $secondRecord -NoEnumerate
108+
} | Export-Yaml -Path $multiplePath
109+
110+
$singleExpected = ConvertTo-Yaml -InputObject $firstRecord
111+
$multipleExpected = & {
112+
Write-Output -InputObject $firstRecord -NoEnumerate
113+
Write-Output -InputObject $secondRecord -NoEnumerate
114+
} | ConvertTo-Yaml
115+
[System.IO.File]::ReadAllText($singlePath) | Should -Be $singleExpected
116+
[System.IO.File]::ReadAllText($multiplePath) | Should -Be $multipleExpected
117+
}
118+
79119
It 'serializes an explicit null input' {
80120
$path = Join-Path $TestDrive 'null.yaml'
81121

0 commit comments

Comments
 (0)