Skip to content

Commit 2ffb0de

Browse files
authored
Throw warning when applies_to is empty (#1245)
* Return null if applies to is empty and emit warning * Update docs * Add test * Fix error * Fix tests
1 parent 9c1e5be commit 2ffb0de

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

docs/syntax/applies.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ discontinued 9.2.0
4747
all
4848
```
4949

50-
`all` and empty string mean generally available for all active versions
50+
`all` means generally available for all active versions
5151

5252
```yaml
5353
applies_to:
5454
serverless: all
5555
```
5656
57-
`all` and empty string can also be specified at a version level
57+
`all` can also be specified at a version level
5858

5959
```yaml
6060
applies_to:
6161
stack: beta all
6262
serverless: beta
6363
```
6464

65-
Both are equivalent, note `all` just means we won't be rendering the version portion in the html.
65+
Note `all` just means we won't be rendering the version portion in the HTML.
6666

6767

6868
## Structured model

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,17 @@ public class ApplicableToConverter : IYamlTypeConverter
122122

123123
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
124124
{
125+
var warnings = new List<string>();
126+
var applicableTo = new ApplicableTo();
127+
125128
if (parser.TryConsume<Scalar>(out var value))
126129
{
127130
if (string.IsNullOrWhiteSpace(value.Value))
128-
return ApplicableTo.All;
131+
{
132+
warnings.Add("The 'applies_to' field is present but empty. No applicability will be assumed.");
133+
return null;
134+
}
135+
129136
if (string.Equals(value.Value, "all", StringComparison.InvariantCultureIgnoreCase))
130137
return ApplicableTo.All;
131138
}
@@ -134,10 +141,6 @@ public class ApplicableToConverter : IYamlTypeConverter
134141
if (deserialized is not Dictionary<object, object?> { Count: > 0 } dictionary)
135142
return null;
136143

137-
138-
var applicableTo = new ApplicableTo();
139-
var warnings = new List<string>();
140-
141144
var keys = dictionary.Keys.OfType<string>().ToArray();
142145
var oldStyleKeys = keys.Where(k => k.StartsWith(':')).ToList();
143146
if (oldStyleKeys.Count > 0)

tests/authoring/Applicability/AppliesToFrontMatter.fs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ applies_to:
2323
"""
2424
[<Fact>]
2525
let ``apply matches expected`` () =
26-
markdown |> appliesTo ApplicableTo.All
26+
markdown |> appliesTo (Unchecked.defaultof<ApplicableTo>)
2727

2828
type ``apply default to top level arguments`` () =
2929
static let markdown = frontMatter """
@@ -186,3 +186,11 @@ applies_to:
186186
Stack=AppliesCollection.op_Explicit "ga 9.1.0",
187187
Product=AppliesCollection.op_Explicit "coming 9.5, discontinued 9.7"
188188
))
189+
190+
type ``parses empty applies_to as null`` () =
191+
static let markdown = frontMatter """
192+
applies_to:
193+
"""
194+
[<Fact>]
195+
let ``does not render label`` () =
196+
markdown |> appliesTo (Unchecked.defaultof<ApplicableTo>)

0 commit comments

Comments
 (0)