Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Better handling of the block chomping indicator
The Block chomping indicators affects how the final line break is
interpreted. So use "strip" when the value doesn't ends with a line
break, "clip" when the value ends with a line break, and "keep"
when the value ends with multiple line breaks.
  • Loading branch information
foriequal0 committed Jul 2, 2020
commit bdf10c48ae690f0ae14588501b8eb9775ce74b0f
56 changes: 50 additions & 6 deletions src/Microsoft.OpenApi/Writers/OpenApiYamlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,8 @@ public override void WriteValue(string value)

Writer.Write("|");

// Write chomping indicator when it ends with line break.
if (value.LastIndexOfAny(new []{'\n', '\r'}) == value.Length - 1)
{
Writer.Write("+");
}

WriteChompingIndicator(value);

// Write indentation indicator when it starts with spaces
if (value.StartsWith(" "))
{
Expand Down Expand Up @@ -222,6 +218,54 @@ public override void WriteValue(string value)
}
}

private void WriteChompingIndicator(string value)
{
var trailingNewlines = 0;
var end = value.Length - 1;
// We only need to know whether there are 0, 1, or more trailing newlines
while (end >= 0 && trailingNewlines < 2)
{
var found = value.LastIndexOfAny(new[] { '\n', '\r' }, end, 2);
if (found == -1 || found != end)
{
// does not ends with newline
break;
}

if (value[end] == '\r')
{
// ends with \r
end--;
}
else if (end > 0 && value[end - 1] == '\r')
{
// ends with \r\n
end -= 2;
}
else
{
// ends with \n
end -= 1;
}
trailingNewlines++;
}

switch (trailingNewlines)
{
case 0:
// "strip" chomping indicator
Writer.Write("-");
break;
case 1:
// "clip"
break;
default:
// "keep" chomping indicator
Writer.Write("+");
break;
}
}

/// <summary>
/// Write null value.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ public void WriteStringWithSpecialCharactersAsYamlWorks(string input, string exp
}

[Theory]
[InlineData("multiline\r\nstring", "test: |\n multiline\n string")]
[InlineData("ends with\r\nline break\r\n", "test: |+\n ends with\n line break")]
[InlineData(" starts with\nspaces", "test: |2\n starts with\n spaces")]
[InlineData(" starts with\nspaces, and ends with line break\n", "test: |+2\n starts with\n spaces, and ends with line break")]
[InlineData("contains\n\n\nempty lines", "test: |\n contains\n\n\n empty lines")]
[InlineData("multiline\r\nstring", "test: |-\n multiline\n string")]
[InlineData("ends with\r\nline break\r\n", "test: |\n ends with\n line break")]
[InlineData("ends with\r\n2 line breaks\r\n\r\n", "test: |+\n ends with\n 2 line breaks\n")]
[InlineData("ends with\r\n3 line breaks\r\n\r\n\r\n", "test: |+\n ends with\n 3 line breaks\n\n")]
[InlineData(" starts with\nspaces", "test: |-2\n starts with\n spaces")]
[InlineData(" starts with\nspaces, and ends with line break\n", "test: |2\n starts with\n spaces, and ends with line break")]
[InlineData("contains\n\n\nempty lines", "test: |-\n contains\n\n\n empty lines")]
[InlineData("no line breaks fallback ", "test: 'no line breaks fallback '")]
public void WriteStringWithNewlineCharactersInObjectAsYamlWorks(string input, string expected)
{
Expand All @@ -103,11 +105,13 @@ public void WriteStringWithNewlineCharactersInObjectAsYamlWorks(string input, st
}

[Theory]
[InlineData("multiline\r\nstring", "- |\n multiline\n string")]
[InlineData("ends with\r\nline break\r\n", "- |+\n ends with\n line break")]
[InlineData(" starts with\nspaces", "- |2\n starts with\n spaces")]
[InlineData(" starts with\nspaces, and ends with line break\n", "- |+2\n starts with\n spaces, and ends with line break")]
[InlineData("contains\n\n\nempty lines", "- |\n contains\n\n\n empty lines")]
[InlineData("multiline\r\nstring", "- |-\n multiline\n string")]
[InlineData("ends with\r\nline break\r\n", "- |\n ends with\n line break")]
[InlineData("ends with\r\n2 line breaks\r\n\r\n", "- |+\n ends with\n 2 line breaks\n")]
[InlineData("ends with\r\n3 line breaks\r\n\r\n\r\n", "- |+\n ends with\n 3 line breaks\n\n")]
[InlineData(" starts with\nspaces", "- |-2\n starts with\n spaces")]
[InlineData(" starts with\nspaces, and ends with line break\n", "- |2\n starts with\n spaces, and ends with line break")]
[InlineData("contains\n\n\nempty lines", "- |-\n contains\n\n\n empty lines")]
[InlineData("no line breaks fallback ", "- 'no line breaks fallback '")]
public void WriteStringWithNewlineCharactersInArrayAsYamlWorks(string input, string expected)
{
Expand Down