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

MergingParser deserializer can now handle nested NodeSequence with aliases (try2) #782

Merged
merged 5 commits into from
Aug 10, 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
8 changes: 1 addition & 7 deletions YamlDotNet.Test/Core/EmitterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ private IList<ParsingEvent> ParsingEventsOf(string text)
return EnumerationOf(parser).ToList();
}

private IEnumerable<ParsingEvent> EnumerationOf(IParser parser)
{
while (parser.MoveNext())
{
yield return parser.Current;
}
}


[Fact]
public void PlainScalarCanBeFollowedByImplicitDocument()
Expand Down
8 changes: 8 additions & 0 deletions YamlDotNet.Test/Core/EmitterTestsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ protected string Emit(IEnumerable<ParsingEvent> events, Func<TextWriter, Emitter
return writer.ToString();
}

protected static IEnumerable<ParsingEvent> EnumerationOf(IParser parser)
{
while (parser.MoveNext())
{
yield return parser.Current;
}
}

protected IEnumerable<ParsingEvent> StreamedDocumentWith(IEnumerable<ParsingEvent> events)
{
return StreamOf(DocumentWith(events.ToArray()));
Expand Down
37 changes: 0 additions & 37 deletions YamlDotNet.Test/Serialization/DeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -333,42 +332,6 @@ public void DeserializeWithoutDuplicateKeyChecking_YamlWithDuplicateKeys_DoesNot
act.ShouldNotThrow<YamlException>("Because duplicate key checking is not enabled");
}

[Fact]
public void MergingParserWithMergeObjectWithSequence_ShouldNotThrowException()
{
var yaml = @"
base_level: &base
tenant:
- a1
Level1: &Level1
<<: [*base]
Level2: &Level2
<<: *Level1
";
var mergingParserFailed = new MergingParser(new Parser(new StringReader(yaml)));
var deserializer = new DeserializerBuilder().Build();
Action act = () => deserializer.Deserialize(mergingParserFailed);
act.ShouldNotThrow<Exception>();
}

[Fact]
public void MergingParserWithNestedSequence_ShouldNotThrowException()
{
var yaml = @"
base_level: &base {}
Level1: &Level1
<<: [*base]
Level2: &Level2
<<: [*Level1]
Level3:
<<: *Level2
";
var mergingParserFailed = new MergingParser(new Parser(new StringReader(yaml)));
var deserializer = new DeserializerBuilder().Build();
Action act = () => deserializer.Deserialize(mergingParserFailed);
act.ShouldNotThrow<Exception>();
}

public class Test
{
public string Value { get; set; }
Expand Down
155 changes: 155 additions & 0 deletions YamlDotNet.Test/Serialization/MergingParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// This file is part of YamlDotNet - A .NET library for YAML.
hemnstill marked this conversation as resolved.
Show resolved Hide resolved
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Xunit;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Test.Core;

namespace YamlDotNet.Test.Serialization
{
public class MergingParserTests : EmitterTestsHelper
{
[Fact]
public void MergingParserWithMergeObjectWithSequence_EachLevelsShouldEquals()
{
var yaml = @"base_level: &base
tenant:
- a1
- a2
Level1: &Level1
<<: [*base]
Level2: &Level2
<<: *Level1
";

var etalonLevel = @"tenant:
- a1
- a2
".NormalizeNewLines();

var mergingParser = new MergingParser(new Parser(new StringReader(yaml)));
var yamlObject = new DeserializerBuilder().Build().Deserialize<Dictionary<string, object>>(mergingParser);
yamlObject.Should().NotBeNull();

var serializer = new SerializerBuilder().Build();
serializer.Serialize(yamlObject["base_level"]).NormalizeNewLines().Should().Be(etalonLevel);
serializer.Serialize(yamlObject["Level1"]).NormalizeNewLines().Should().Be(etalonLevel);
serializer.Serialize(yamlObject["Level2"]).NormalizeNewLines().Should().Be(etalonLevel);
}

[Fact]
public void MergingParserWithMergeObjectWithSequence_EmittedTextShouldNotContainsDeletedEvents()
{
var yaml = @"base_level: &base
tenant:
- a1
- a2
Level1: &Level1
<<: [*base]
Level2:
<<: *Level1
";

var etalonEmittedText = @"base_level: &base
tenant:
- a1
- a2
Level1: &Level1
tenant:
- a1
- a2
Level2:
tenant:
- a1
- a2
".NormalizeNewLines();

var mergingParser = new MergingParser(new Parser(new StringReader(yaml)));
var events = EnumerationOf(mergingParser);
EmittedTextFrom(events).NormalizeNewLines().Should().Be(etalonEmittedText);
}

[Fact]
public void MergingParserWithMergeObjectWithSequenceAndScalarItems_EmittedTextShouldNotContainsDeletedEvents()
{
var yaml = @"base_level: &base
tenant:
- a1
- a2
Level1: &Level1
<<: [*base]
item1:
Level2:
<<: *Level1
item2:
";

var etalonEmittedText = @"base_level: &base
tenant:
- a1
- a2
Level1: &Level1
tenant:
- a1
- a2
item1: ''
Level2:
tenant:
- a1
- a2
item1: ''
item2: ''
".NormalizeNewLines();

var mergingParser = new MergingParser(new Parser(new StringReader(yaml)));
var events = EnumerationOf(mergingParser);
EmittedTextFrom(events).NormalizeNewLines().Should().Be(etalonEmittedText);
}

[Fact]
public void MergingParserWithNestedSequence_ShouldNotThrowException()
{
var yaml = @"
base_level: &base {}
Level1: &Level1
<<: [*base]
Level2: &Level2
<<: [*Level1]
Level3:
<<: *Level2
";
var etalonMergedYaml = @"base_level: {}
Level1: {}
Level2: {}
Level3: {}
".NormalizeNewLines();

var mergingParserFailed = new MergingParser(new Parser(new StringReader(yaml)));
var yamlObject = new DeserializerBuilder().Build().Deserialize(mergingParserFailed);

new SerializerBuilder().Build().Serialize(yamlObject!).NormalizeNewLines().Should().Be(etalonMergedYaml);
}
}
}
10 changes: 7 additions & 3 deletions YamlDotNet/Core/MergingParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ private bool HandleSequence(LinkedListNode<ParsingEvent> node)
var current = node;
while (current != null)
{
if (current.Value is SequenceEnd &&
current.Value.Start.Line >= sequenceStart.Value.Start.Line)
if (current.Value is SequenceEnd)
{
events.MarkDeleted(current);
return true;
Expand All @@ -160,7 +159,7 @@ private IEnumerable<ParsingEvent> GetMappingEvents(AnchorName anchor)
var cloner = new ParsingEventCloner();
var nesting = 0;

return events.FromAnchor(anchor)
return events.FromAnchor(anchor).Where(e => !this.events.IsDeleted(e))
.Select(e => e.Value)
.TakeWhile(e => (nesting += e.NestingIncrease) >= 0)
.Select(e => cloner.Clone(e));
Expand Down Expand Up @@ -198,6 +197,11 @@ public void MarkDeleted(LinkedListNode<ParsingEvent> node)
deleted.Add(node);
}

public bool IsDeleted(LinkedListNode<ParsingEvent> node)
{
return deleted.Contains(node);
}

public void CleanMarked()
{
foreach (var node in deleted)
Expand Down