fix: serialize nested hashtables in New-ModuleManifest -PrivateData correctly - #27891
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
Samran Asif (@webdevsamran) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Pull request overview
This PR updates New-ModuleManifest -PrivateData generation to correctly emit PowerShell data-file literals for nested structures (notably nested hashtables) instead of string-casting complex values to type names like System.Collections.Hashtable.
Changes:
- Added a recursive
SerializeValue()helper to format nested hashtables, arrays, booleans, numbers, and strings as PowerShell literals. - Switched
-PrivateDatavalue emission to use the new serializer (instead ofConvertTo(..., string)+ quoting).
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| if (value is int || value is long || value is double || value is float || value is decimal) | ||
| { | ||
| return Convert.ToString(value, CultureInfo.InvariantCulture); | ||
| } |
| foreach (DictionaryEntry entry in ht) | ||
| { | ||
| sb.Append(innerIndent); | ||
| sb.Append(entry.Key); |
| if (value is Hashtable ht) | ||
| { | ||
| var sb = new StringBuilder(); | ||
| string innerIndent = currentIndent + " "; | ||
| sb.Append("@{"); | ||
| sb.Append(streamWriter.NewLine); | ||
| foreach (DictionaryEntry entry in ht) | ||
| { |
| foreach (DictionaryEntry entry in privateDataHashTable) | ||
| { | ||
| result.Append(ManifestFragment(entry.Key.ToString(), entry.Key.ToString(), QuoteName((string)LanguagePrimitives.ConvertTo(entry.Value, typeof(string), CultureInfo.InvariantCulture)), streamWriter)); | ||
| result.Append(ManifestFragment(entry.Key.ToString(), entry.Key.ToString(), SerializeValue(entry.Value, _indent, streamWriter), streamWriter)); |
PR Summary
Serializes nested hashtables and complex objects correctly when generating manifest files in
New-ModuleManifest -PrivateData. Previously, values were converted via string casting which emitted"System.Collections.Hashtable"for nested hashtables. The new recursive serializer formats nested hashtables, arrays, and primitive values as valid PowerShell data file literals.Fixes #5922