Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Emitter to handle comments between a mapping key and a mapping/sequence value #814

Merged
merged 1 commit into from Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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