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

Serialization using IndentedTextWriter causes missing indentation #1015

Open
ogretmenb opened this issue Dec 2, 2024 · 2 comments
Open

Serialization using IndentedTextWriter causes missing indentation #1015

ogretmenb opened this issue Dec 2, 2024 · 2 comments

Comments

@ogretmenb
Copy link

ogretmenb commented Dec 2, 2024

Describe the bug

my project was using 12.0.2 version of YamlDotNet package. While upgrading to a newer version I realized an issue introduced in release 12.1.0 via this change https://github.com/aaubry/YamlDotNet/pull/747/files#diff-7df2919371fb7e178ae78e207ac6010f930dae71acf2e99769c373c472ce575bR1922

I am using Serializer.Serialize(TextWriter writer, object graph) and passing an IndentedTextWriter to that method for custom indentation.
That single line seems removing configured indentation in IndentedTextWriter object.

setting EmitterSettings.NewLine to TextWriter.NewLine instead of replacing WriteLine with Write seems a better solution and it works fine

I have a PR for the fix of the issue, It also contains a unit test to explain my issue. But I dont have permissions to push my branch. May I ask for push access as well?

adding the test code which creates the issue below

[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);
}
@EdwardCooke
Copy link
Collaborator

To create a pr you fork this repository to your own use. Push your branch up to your fork. Then create a pull request from there. You don’t actually create a branch in this repository. I’ll take a closer look at your issue in a little bit and if you can create that or with above mentioned directions I’ll review it.

@ogretmenb
Copy link
Author

Hi @EdwardCooke
thanks for the guidance,
here is the PR #1017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants