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: serialization using IndentedTextWriter causes missing indentation #1017

Merged
merged 1 commit into from
Dec 23, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// SOFTWARE.

using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -329,6 +330,35 @@ public void SerializeWithCRNewLine()
result.Should().Be(expectedResult);
}


/// <summary>
/// Tests the serialization of a dictionary containing a list of strings using IndentedTextWriter.
/// </summary>
[Fact]
public void SerializeWithTabs()
{
var tabString = " ";
using var writer = new StringWriter();
using var indentedTextWriter = new IndentedTextWriter(writer, tabString) { Indent = 2 };

//create a dictionary with a list of strings to test the tabbed serialization
var items = new List<string> { "item 1", "item 2" };
var list = new Dictionary<string, List<string>> { { "key", items } };

SerializerBuilder
.Build()
.Serialize(indentedTextWriter, list);

//split serialized output into lines
var lines = indentedTextWriter.InnerWriter.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);

//expected indentation
var indent = string.Join(string.Empty, Enumerable.Repeat(tabString, indentedTextWriter.Indent).ToList());

//check that the serialized lines (excluding the first and last) start with the expected indentation
lines.Skip(1).Take(lines.Length - 2).Where(element => element.StartsWith(indent)).Should().HaveCount(items.Count);
}

[Fact]
public void DeserializeExplicitType()
{
Expand Down
5 changes: 2 additions & 3 deletions YamlDotNet/Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public class Emitter : IEmitter
private bool isIndentation;
private readonly bool forceIndentLess;
private readonly bool useUtf16SurrogatePair;
private readonly string newLine;

private bool isDocumentEndWritten;

Expand Down Expand Up @@ -150,9 +149,9 @@ public Emitter(TextWriter output, EmitterSettings settings)
this.skipAnchorName = settings.SkipAnchorName;
this.forceIndentLess = !settings.IndentSequences;
this.useUtf16SurrogatePair = settings.UseUtf16SurrogatePairs;
this.newLine = settings.NewLine;

this.output = output;
this.output.NewLine = settings.NewLine;
this.outputUsesUnicodeEncoding = IsUnicode(output.Encoding);
}

Expand Down Expand Up @@ -1937,7 +1936,7 @@ private void WriteBreak(char breakCharacter = '\n')
{
if (breakCharacter == '\n')
{
output.Write(newLine);
output.WriteLine();
}
else
{
Expand Down