Skip to content

Ensure NULL string value is written as quoted string in YAML #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Common/YamlUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ internal class YamlUtils
{
private static Deserializer deserializer = (Deserializer)new DeserializerBuilder().Build();
private static Deserializer camelCaseDeserializer = (Deserializer)new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
private static Serializer serializer = (Serializer)new SerializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build();
private static Serializer serializer = (Serializer)new SerializerBuilder()
.WithEventEmitter(nextEmitter => new QuotedNullStringEvenEmitter(nextEmitter))
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();

// we have a specific serializer for metadata as it is not a simple string/string dictionary
// and we want it to look like: no-loc: [Cmdlet, -Parameter]
Expand Down
28 changes: 28 additions & 0 deletions src/YamlWriter/QuotedNullStringEvenEmitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Runtime.Remoting.Metadata.W3cXsd2001;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.EventEmitters;

public class QuotedNullStringEvenEmitter : ChainedEventEmitter
{
public QuotedNullStringEvenEmitter(IEventEmitter nextEmitter) : base(nextEmitter)
{
}

public override void Emit(ScalarEventInfo eventInfo, YamlDotNet.Core.IEmitter emitter)
{
if (eventInfo.Source.Type == typeof(string) && string.Equals(eventInfo.Source.Value?.ToString() , "NULL", System.StringComparison.OrdinalIgnoreCase))
{
emitter.Emit(new Scalar(@"'NULL'"));
}
else
{
// For all other values, use the default emitter
base.Emit(eventInfo, emitter);
}
}
}
6 changes: 6 additions & 0 deletions test/Pester/ExportYamlCommandHelp.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ Describe "Export-YamlCommandHelp tests" {
}
}

It 'Should be able to handle accepted values with NULL' {
$cmd = Import-MarkdownCommandHelp -Path "$PSScriptRoot/assets/Resolve-DNSName.md"
$yamlFile = $cmd | Export-YamlCommandHelp -outputfolder $TestDrive -Force
$yamlDict = Import-YamlCommandHelp $yamlFile.FullName
$yamlDict.Parameters[-1].AcceptedValues | Should -Contain "'NULL'"
}
}

Context "Input" {
Expand Down
4 changes: 2 additions & 2 deletions test/Pester/MeasurePlatyPSMarkdown.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Describe "Export-MarkdownModuleFile" {

It "Should identify all the '<fileType>' assets" -TestCases @(
@{ fileType = "unknown"; expectedCount = 2 }
@{ fileType = "CommandHelp"; expectedCount = 40 }
@{ fileType = "CommandHelp"; expectedCount = 41 }
@{ fileType = "ModuleFile"; expectedCount = 15 }
@{ fileType = "V1Schema"; expectedCount = 50 }
@{ fileType = "V1Schema"; expectedCount = 51 }
@{ fileType = "V2Schema"; expectedCount = 5 }
) {
param ($fileType, $expectedCount)
Expand Down
Loading