Skip to content

Commit

Permalink
Merge pull request #814 from YihaoOct/yihao/fix-comments
Browse files Browse the repository at this point in the history
Fix Emitter to handle comments between a mapping key and a mapping/sequence value

+semver:fix
  • Loading branch information
EdwardCooke authored Jun 17, 2023
2 parents 9a5900f + dd02557 commit 6f1e8dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
22 changes: 22 additions & 0 deletions YamlDotNet.Test/Core/EmitterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,28 @@ public void CommentsAreEmittedCorrectly()
.And.Contain("# Bottom comment");
}

[Fact]
public void CommentsBetweenMappingKeyAndValueAreEmittedCorrectly()
{
var events = MappingWith(
Scalar("key").ImplicitPlain,
InlineComment("inline comment"),
StandaloneComment("standalone comment"),
BlockSequenceStart,
Scalar("value").ImplicitPlain,
SequenceEnd
);

var yaml = EmittedTextFrom(StreamedDocumentWith(events));

yaml.Should()
.Contain(Lines(
"key: # inline comment",
"# standalone comment",
" - value"
));
}

[Fact]
public void ACommentAsTheFirstEventAddsANewLine()
{
Expand Down
7 changes: 2 additions & 5 deletions YamlDotNet/Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,7 @@ private void EmitBlockMappingKey(ParsingEvent evt, bool isFirst)
{
states.Push(EmitterState.BlockMappingSimpleValue);
EmitNode(evt, true, true);
WriteIndicator(":", false, false, false);
}
else
{
Expand All @@ -1652,11 +1653,7 @@ private void EmitBlockMappingKey(ParsingEvent evt, bool isFirst)
/// </summary>
private void EmitBlockMappingValue(ParsingEvent evt, bool isSimple)
{
if (isSimple)
{
WriteIndicator(":", false, false, false);
}
else
if (!isSimple)
{
WriteIndent();
WriteIndicator(":", true, false, true);
Expand Down

0 comments on commit 6f1e8dc

Please sign in to comment.