diff --git a/src/Elastic.Documentation.Configuration/Converters/ApplicableToYamlConverter.cs b/src/Elastic.Documentation.Configuration/Converters/ApplicableToYamlConverter.cs index a572c3ecab..37159c460a 100644 --- a/src/Elastic.Documentation.Configuration/Converters/ApplicableToYamlConverter.cs +++ b/src/Elastic.Documentation.Configuration/Converters/ApplicableToYamlConverter.cs @@ -140,8 +140,23 @@ private static void MergeAppliesToListScalarLine( dictionary[key] = string.IsNullOrEmpty(val) ? null : val; } - private ApplicableTo FinalizeApplicableTo(Dictionary dictionary, List<(Severity, string)> diagnostics) + // TODO: remove once SharpYaml ships a release after 3.17.0 (https://github.com/elastic/docs-eng-team/issues/1236). + // SharpYaml 3.17.0 reads `{ self:, ece: ga }` as a key named `self:` (colon kept) when a comma follows the + // empty value. YAML defines it as key `self` with no value, so strip the colon or the key is dropped as unknown. + private static Dictionary NormalizeEmptyValueKeys(Dictionary dictionary) { + var normalized = new Dictionary(dictionary.Comparer); + foreach (var (key, value) in dictionary) + { + var name = value is null && key.Length > 1 && key.EndsWith(':') ? key[..^1] : key; + normalized[name] = value is Dictionary nested ? NormalizeEmptyValueKeys(nested) : value; + } + return normalized; + } + + private ApplicableTo FinalizeApplicableTo(Dictionary rawDictionary, List<(Severity, string)> diagnostics) + { + var dictionary = NormalizeEmptyValueKeys(rawDictionary); var applicableTo = new ApplicableTo(); var keys = dictionary.Keys.Select(x => x.Replace('_', '-')).ToArray(); diff --git a/tests/Elastic.Markdown.Tests/.snapshots/AppliesTo/ApplicableToYamlSnapshotTests/Read_FlowMappings_KeysWithoutAValue.verified.txt b/tests/Elastic.Markdown.Tests/.snapshots/AppliesTo/ApplicableToYamlSnapshotTests/Read_FlowMappings_KeysWithoutAValue.verified.txt new file mode 100644 index 0000000000..5951e2141d --- /dev/null +++ b/tests/Elastic.Markdown.Tests/.snapshots/AppliesTo/ApplicableToYamlSnapshotTests/Read_FlowMappings_KeysWithoutAValue.verified.txt @@ -0,0 +1,31 @@ +yaml: { self:, ece: ga 4.2 } +json: [{"type":"deployment","sub_type":"self","lifecycle":"ga","version":"all"},{"type":"deployment","sub_type":"ece","lifecycle":"ga","version":"4.2\u002B"}] +text: deployment: self=all, ece=ga 4.2+, + +yaml: { ess:, ece: } +json: [{"type":"deployment","sub_type":"ece","lifecycle":"ga","version":"all"},{"type":"deployment","sub_type":"ess","lifecycle":"ga","version":"all"}] +text: deployment: ece=all, ess=all + +yaml: { self:, eck: } +json: [{"type":"deployment","sub_type":"self","lifecycle":"ga","version":"all"},{"type":"deployment","sub_type":"eck","lifecycle":"ga","version":"all"}] +text: deployment: self=all, eck=all + +yaml: { eck:, self: } +json: [{"type":"deployment","sub_type":"self","lifecycle":"ga","version":"all"},{"type":"deployment","sub_type":"eck","lifecycle":"ga","version":"all"}] +text: deployment: self=all, eck=all + +yaml: { self:, ece:, eck: } +json: [{"type":"deployment","sub_type":"self","lifecycle":"ga","version":"all"},{"type":"deployment","sub_type":"ece","lifecycle":"ga","version":"all"},{"type":"deployment","sub_type":"eck","lifecycle":"ga","version":"all"}] +text: deployment: self=all, ece=all, eck=all + +yaml: { stack:, serverless: } +json: [{"type":"stack","sub_type":"stack","lifecycle":"ga","version":"all"},{"type":"serverless","sub_type":"elasticsearch","lifecycle":"ga","version":"all"},{"type":"serverless","sub_type":"observability","lifecycle":"ga","version":"all"},{"type":"serverless","sub_type":"security","lifecycle":"ga","version":"all"},{"type":"serverless","sub_type":"vectordb","lifecycle":"ga","version":"all"}] +text: stack: all, serverless: elasticsearch=all, observability=all, security=all, vectordb=all + +yaml: { deployment: { self:, ece: ga 4.2 } } +json: [{"type":"deployment","sub_type":"self","lifecycle":"ga","version":"all"},{"type":"deployment","sub_type":"ece","lifecycle":"ga","version":"4.2\u002B"}] +text: deployment: self=all, ece=ga 4.2+, + +yaml: { serverless: { elasticsearch:, security: ga } } +json: [{"type":"serverless","sub_type":"elasticsearch","lifecycle":"ga","version":"all"},{"type":"serverless","sub_type":"security","lifecycle":"ga"}] +text: serverless: elasticsearch=all, security=ga, diff --git a/tests/Elastic.Markdown.Tests/.snapshots/Directives/ApplicabilitySwitchEmptyValueKeysTests/RendersEveryBadgeInTheTabLabels.verified.html b/tests/Elastic.Markdown.Tests/.snapshots/Directives/ApplicabilitySwitchEmptyValueKeysTests/RendersEveryBadgeInTheTabLabels.verified.html new file mode 100644 index 0000000000..9a6f243894 --- /dev/null +++ b/tests/Elastic.Markdown.Tests/.snapshots/Directives/ApplicabilitySwitchEmptyValueKeysTests/RendersEveryBadgeInTheTabLabels.verified.html @@ -0,0 +1,22 @@ +
+ + +
+

Content A

+
+ + +
+

Content B

+
+
diff --git a/tests/Elastic.Markdown.Tests/AppliesTo/ApplicableToYamlSnapshotTests.cs b/tests/Elastic.Markdown.Tests/AppliesTo/ApplicableToYamlSnapshotTests.cs index 7c7d85dd80..82ca22945a 100644 --- a/tests/Elastic.Markdown.Tests/AppliesTo/ApplicableToYamlSnapshotTests.cs +++ b/tests/Elastic.Markdown.Tests/AppliesTo/ApplicableToYamlSnapshotTests.cs @@ -305,6 +305,19 @@ public void Read_Sequences_MergeMappingsAndKeyValueLines() => "- 'deployment: ga'\n- 'deployment: beta'" ]).Should().MatchSnapshot(); + [Test] + public void Read_FlowMappings_KeysWithoutAValue() => + Table([ + "{ self:, ece: ga 4.2 }", + "{ ess:, ece: }", + "{ self:, eck: }", + "{ eck:, self: }", + "{ self:, ece:, eck: }", + "{ stack:, serverless: }", + "{ deployment: { self:, ece: ga 4.2 } }", + "{ serverless: { elasticsearch:, security: ga } }" + ]).Should().MatchSnapshot(); + [Test] public void Read_Stack_ValueShapes() => Table([ diff --git a/tests/Elastic.Markdown.Tests/Directives/ApplicabilitySwitchTests.cs b/tests/Elastic.Markdown.Tests/Directives/ApplicabilitySwitchTests.cs index b886a5f642..9da47dcfb6 100644 --- a/tests/Elastic.Markdown.Tests/Directives/ApplicabilitySwitchTests.cs +++ b/tests/Elastic.Markdown.Tests/Directives/ApplicabilitySwitchTests.cs @@ -104,6 +104,28 @@ public void ItemIndicesAreZeroBased() public void RendersHtml() => Html.Should().MatchHtmlSnapshot(DocsHtml.Body()); } +// Reproduces elastic/docs-builder#5007: a key with no value followed by a comma lost its badge in the tab label. +[InheritsTests] +public class ApplicabilitySwitchEmptyValueKeysTests() : DirectiveTest( + """ +:::::{applies-switch} + +::::{applies-item} { self:, ece: ga 4.2 } +Content A +:::: + +::::{applies-item} { ess:, ece: } +Content B +:::: + +::::: +""" +) +{ + [Test] + public void RendersEveryBadgeInTheTabLabels() => Html.Should().MatchHtmlSnapshot(DocsHtml.Body()); +} + [InheritsTests] public class MultipleApplicabilitySwitchTests() : DirectiveTest( """