Skip to content

Commit 6ba113c

Browse files
committed
Fix
1 parent 8324af9 commit 6ba113c

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

src/Elastic.Markdown/Myst/FrontMatter/Products.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,24 +321,13 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria
321321
return enumMemberAttr?.Value?.Equals(value.Value, StringComparison.OrdinalIgnoreCase) ?? false;
322322
});
323323

324-
if (product != default)
324+
if (Enum.IsDefined(product) && product.GetEnumMemberValue()?.Equals(value.Value) == true)
325325
return product;
326326

327327
throw new InvalidProductException(value.Value);
328328
}
329329

330-
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
331-
{
332-
if (value == null)
333-
return;
334-
var product = (Product)value;
335-
var enumMemberAttr = typeof(Product)
336-
.GetField(product.ToString())
337-
?.GetCustomAttributes(typeof(EnumMemberAttribute), false)
338-
.FirstOrDefault() as EnumMemberAttribute;
339-
340-
emitter.Emit(new Scalar(enumMemberAttr?.Value ?? product.ToString().ToLowerInvariant()));
341-
}
330+
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer) => throw new NotImplementedException();
342331
}
343332

344333
public class InvalidProductException(string invalidValue)
@@ -356,6 +345,15 @@ public static string GetProductDisplayName(this Product product)
356345
return displayAttr?.Name ?? product.ToString();
357346
}
358347

348+
public static string? GetEnumMemberValue(this Product product)
349+
{
350+
var enumMemberAttr = typeof(Product)
351+
.GetField(product.ToString())
352+
?.GetCustomAttributes(typeof(EnumMemberAttribute), false)
353+
.FirstOrDefault() as EnumMemberAttribute;
354+
return enumMemberAttr?.Value;
355+
}
356+
359357

360358
private static List<string> GetEnumMemberValues() => Enum.GetValues<Product>()
361359
.Select(p =>

tests/Elastic.Markdown.Tests/FrontMatter/YamlFrontMatterTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using Elastic.Markdown.Myst.FrontMatter;
56
using Elastic.Markdown.Tests.Directives;
67
using FluentAssertions;
78

@@ -63,3 +64,81 @@ public class NavigationTitleSupportReplacements(ITestOutputHelper output) : Dire
6364
[Fact]
6465
public void ReadsNavigationTitle() => File.NavigationTitle.Should().Be("Documentation Guide: value");
6566
}
67+
68+
public class Products(ITestOutputHelper output) : DirectiveTest(output,
69+
"""
70+
---
71+
products:
72+
- "apm"
73+
---
74+
75+
# APM
76+
"""
77+
)
78+
{
79+
[Fact]
80+
public void ReadsProducts()
81+
{
82+
File.YamlFrontMatter.Should().NotBeNull();
83+
File.YamlFrontMatter!.Products.Should().NotBeNull()
84+
.And.HaveCount(1)
85+
.And.Contain(Product.Apm);
86+
}
87+
}
88+
89+
public class ProductsSuggestionWhenMispelled(ITestOutputHelper output) : DirectiveTest(output,
90+
"""
91+
---
92+
products:
93+
- aapm
94+
---
95+
96+
# APM
97+
"""
98+
)
99+
{
100+
[Fact]
101+
public void HasErrors()
102+
{
103+
Collector.Diagnostics.Should().HaveCount(1);
104+
Collector.Diagnostics.Should().Contain(d => d.Message.Contains("Invalid products frontmatter value: \"aapm\". Did you mean \"apm\"?"));
105+
}
106+
}
107+
108+
public class ProductsSuggestionWhenMispelled2(ITestOutputHelper output) : DirectiveTest(output,
109+
"""
110+
---
111+
products:
112+
- apm-javaagent
113+
---
114+
115+
# APM
116+
"""
117+
)
118+
{
119+
[Fact]
120+
public void HasErrors()
121+
{
122+
Collector.Diagnostics.Should().HaveCount(1);
123+
Collector.Diagnostics.Should().Contain(d => d.Message.Contains("Invalid products frontmatter value: \"apm-javaagent\". Did you mean \"apm-java-agent\"?"));
124+
}
125+
}
126+
127+
public class ProductsSuggestionWhenCasingError(ITestOutputHelper output) : DirectiveTest(output,
128+
"""
129+
---
130+
products:
131+
- Apm
132+
---
133+
134+
# APM
135+
"""
136+
)
137+
{
138+
[Fact]
139+
public void HasErrors()
140+
{
141+
Collector.Diagnostics.Should().HaveCount(1);
142+
Collector.Diagnostics.Should().Contain(d => d.Message.Contains("Invalid products frontmatter value: \"Apm\". Did you mean \"apm\"?"));
143+
}
144+
}

0 commit comments

Comments
 (0)