Skip to content

Commit 3b8fb98

Browse files
committed
Added test for terminal characters in yaml strings
1 parent a6743f9 commit 3b8fb98

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Microsoft.OpenApi/Writers/SpecialCharacterStringExtensions.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public static class SpecialCharacterStringExtensions
4242
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
4343
private static readonly string[] _yamlPlainStringForbiddenCombinations =
4444
{
45-
//": ",
46-
":", // Even though colons can be allowed, this change prevents unquoted trailing colon which causes a problem
45+
": ",
4746
" #",
4847

4948
// These are technically forbidden only inside flow collections, but
@@ -55,6 +54,13 @@ public static class SpecialCharacterStringExtensions
5554
","
5655
};
5756

57+
// Plain style strings cannot end with these characters.
58+
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
59+
private static readonly string[] _yamlPlainStringForbiddenTerminals =
60+
{
61+
":"
62+
};
63+
5864
// Double-quoted strings are needed for these non-printable control characters.
5965
// http://www.yaml.org/spec/1.2/spec.html#style/flow/double-quoted
6066
private static readonly char[] _yamlControlCharacters =
@@ -171,6 +177,7 @@ internal static string GetYamlCompatibleString(this string input)
171177
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
172178
if (_yamlPlainStringForbiddenCombinations.Any(fc => input.Contains(fc)) ||
173179
_yamlIndicators.Any(i => input.StartsWith(i.ToString())) ||
180+
_yamlPlainStringForbiddenTerminals.Any(i => input.EndsWith(i.ToString())) ||
174181
input.Trim() != input)
175182
{
176183
// Escape single quotes with two single quotes.

test/Microsoft.OpenApi.Tests/Writers/OpenApiWriterSpecialCharacterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void WriteStringWithSpecialCharactersAsJsonWorks(string input, string exp
6060
[InlineData("true", " 'true'")]
6161
[InlineData("trailingspace ", " 'trailingspace '")]
6262
[InlineData(" trailingspace", " ' trailingspace'")]
63+
[InlineData("terminal:", " 'terminal:'")]
6364
public void WriteStringWithSpecialCharactersAsYamlWorks(string input, string expected)
6465
{
6566
// Arrange

0 commit comments

Comments
 (0)