@@ -13,7 +13,9 @@ function Get-YamlSerializationShape {
1313 [Parameter (Mandatory )]
1414 [pscustomobject ] $State ,
1515
16- [switch ] $EnumsAsStrings
16+ [switch ] $EnumsAsStrings ,
17+
18+ [switch ] $InspectOnly
1719 )
1820
1921 $scalar = New-YamlEmissionNode - Kind Scalar
@@ -189,6 +191,15 @@ function Get-YamlSerializationShape {
189191 }
190192
191193 if ($isByteArray ) {
194+ $base64Length = [long ] 4 * [long ] [System.Math ]::Ceiling($Value.LongLength / 3.0 )
195+ if ($base64Length -gt $State.MaxScalarLength ) {
196+ throw (New-YamlSerializationException - ErrorId ' YamlScalarLimitExceeded' - Message (
197+ " A scalar exceeds the configured limit of $ ( $State.MaxScalarLength ) characters."
198+ ))
199+ }
200+ if ($InspectOnly ) {
201+ return [pscustomobject ]@ { Kind = ' Binary' ; Node = $null ; Values = $null }
202+ }
192203 $scalar.Tag = ' tag:yaml.org,2002:binary'
193204 $scalar.Value = [System.Convert ]::ToBase64String($Value )
194205 $scalar.Style = ' DoubleQuoted'
@@ -197,16 +208,33 @@ function Get-YamlSerializationShape {
197208 }
198209
199210 if ($isDictionary -or $isPropertyBag ) {
211+ if ($InspectOnly ) {
212+ return [pscustomobject ]@ { Kind = ' Mapping' ; Node = $null ; Values = $null }
213+ }
200214 $entries = [System.Collections.Generic.List [object ]]::new()
201215 if ($isDictionary ) {
202- foreach ($entry in $Value.GetEnumerator ()) {
216+ $remainingNodes = [System.Math ]::Max(0 , $State.MaxNodes - $State.NodeCount )
217+ $maximumEntries = [int ] [System.Math ]::Floor($remainingNodes / 2.0 )
218+ $rawEntries = Read-YamlBoundedEnumerable - Value $Value `
219+ - MaximumItems $maximumEntries - MaxNodes $State.MaxNodes - State $State `
220+ - DictionaryEntries - EnumsAsStrings:$EnumsAsStrings
221+ foreach ($entry in $rawEntries ) {
203222 $entries.Add ([pscustomobject ]@ {
204223 Key = [object ] $entry.Key
205224 Value = [object ] $entry.Value
206225 })
207226 }
208227 } else {
228+ if (($dataProperties.Count * 2 ) -gt ($State.MaxNodes - $State.NodeCount )) {
229+ throw (New-YamlSerializationException - ErrorId ' YamlNodeLimitExceeded' - Message (
230+ " The object graph exceeds the configured limit of $ ( $State.MaxNodes ) nodes."
231+ ))
232+ }
209233 foreach ($property in $dataProperties ) {
234+ $null = Get-YamlSerializationShape - Value $property.Name - State $State `
235+ - EnumsAsStrings:$EnumsAsStrings - InspectOnly
236+ $null = Get-YamlSerializationShape - Value $property.Value - State $State `
237+ - EnumsAsStrings:$EnumsAsStrings - InspectOnly
210238 $entries.Add ([pscustomobject ]@ {
211239 Key = [object ] $property.Name
212240 Value = [object ] $property.Value
@@ -220,10 +248,13 @@ function Get-YamlSerializationShape {
220248 }
221249 }
222250 if ($isSequence ) {
223- $items = [System.Collections.Generic.List [object ]]::new()
224- foreach ($item in $Value ) {
225- $items.Add ([object ] $item )
251+ if ($InspectOnly ) {
252+ return [pscustomobject ]@ { Kind = ' Sequence' ; Node = $null ; Values = $null }
226253 }
254+ $remainingNodes = [System.Math ]::Max(0 , $State.MaxNodes - $State.NodeCount )
255+ $items = Read-YamlBoundedEnumerable - Value $Value `
256+ - MaximumItems $remainingNodes - MaxNodes $State.MaxNodes - State $State `
257+ - EnumsAsStrings:$EnumsAsStrings
227258 return [pscustomobject ]@ {
228259 Kind = ' Sequence'
229260 Node = $null
0 commit comments