Skip to content

Commit be07f52

Browse files
Improve YAML boolean and null value detection per YAML spec
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
1 parent 31f1cc9 commit be07f52

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

DevProxy.Abstractions/Utils/ProxyYaml.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,23 +192,20 @@ public static bool TryConvertYamlToJson(string yamlContent, out string? jsonCont
192192
return null;
193193
}
194194

195-
// Try to infer the type from the value
196-
// Check for boolean values
197-
if (value.Equals("true", StringComparison.OrdinalIgnoreCase))
195+
// Check for null values (YAML 1.1 and 1.2 spec)
196+
if (IsNullValue(value))
198197
{
199-
return true;
198+
return null;
200199
}
201-
if (value.Equals("false", StringComparison.OrdinalIgnoreCase))
200+
201+
// Check for boolean values (YAML 1.1 spec - commonly used)
202+
if (IsTrueValue(value))
202203
{
203-
return false;
204+
return true;
204205
}
205-
206-
// Check for null values
207-
if (value.Equals("null", StringComparison.OrdinalIgnoreCase) ||
208-
value.Equals("~", StringComparison.Ordinal) ||
209-
value.Length == 0)
206+
if (IsFalseValue(value))
210207
{
211-
return null;
208+
return false;
212209
}
213210

214211
// Check for integer values
@@ -231,4 +228,22 @@ public static bool TryConvertYamlToJson(string yamlContent, out string? jsonCont
231228
// Return as string
232229
return value;
233230
}
231+
232+
// YAML 1.1 boolean true values
233+
private static bool IsTrueValue(string value) =>
234+
value.Equals("true", StringComparison.OrdinalIgnoreCase) ||
235+
value.Equals("yes", StringComparison.OrdinalIgnoreCase) ||
236+
value.Equals("on", StringComparison.OrdinalIgnoreCase);
237+
238+
// YAML 1.1 boolean false values
239+
private static bool IsFalseValue(string value) =>
240+
value.Equals("false", StringComparison.OrdinalIgnoreCase) ||
241+
value.Equals("no", StringComparison.OrdinalIgnoreCase) ||
242+
value.Equals("off", StringComparison.OrdinalIgnoreCase);
243+
244+
// YAML 1.1 and 1.2 null values
245+
private static bool IsNullValue(string value) =>
246+
value.Length == 0 ||
247+
value.Equals("~", StringComparison.Ordinal) ||
248+
value.Equals("null", StringComparison.OrdinalIgnoreCase);
234249
}

0 commit comments

Comments
 (0)