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

Create a value promise right after seeing an anchor #598

Merged
merged 2 commits into from
Mar 23, 2021
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
79 changes: 79 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Xunit;
using YamlDotNet.Core;
Expand Down Expand Up @@ -1968,6 +1969,84 @@ public void ShouldIndentSequences()
Assert.Equal(expected.NormalizeNewLines(), yaml.NormalizeNewLines().TrimNewLines());
}

public class CycleTestEntity
{
public CycleTestEntity Cycle { get; set; }
}

[Fact]
public void SerializeCycleWithAlias()
{
var sut = new SerializerBuilder()
.WithTagMapping("!CycleTag", typeof(CycleTestEntity))
.Build();

var entity = new CycleTestEntity();
entity.Cycle = entity;
var yaml = sut.Serialize(entity);
var expected = Yaml.Text(@"&o0 !CycleTag
Cycle: *o0");

Assert.Equal(expected.NormalizeNewLines(), yaml.NormalizeNewLines().TrimNewLines());
}

[Fact]
public void DeserializeCycleWithAlias()
{
var sut = new DeserializerBuilder()
.WithTagMapping("!CycleTag", typeof(CycleTestEntity))
.Build();

var yaml = Yaml.Text(@"&o0 !CycleTag
Cycle: *o0");
var obj = sut.Deserialize<CycleTestEntity>(yaml);

Assert.Same(obj, obj.Cycle);
}

[Fact]
public void DeserializeCycleWithoutAlias()
{
var sut = new DeserializerBuilder()
.Build();

var yaml = Yaml.Text(@"&o0
Cycle: *o0");
var obj = sut.Deserialize<CycleTestEntity>(yaml);

Assert.Same(obj, obj.Cycle);
}

public static IEnumerable<object[]> Depths => Enumerable.Range(1, 10).Select(i => new[] { (object)i });

[Theory]
[MemberData(nameof(Depths))]
public void DeserializeCycleWithAnchorsWithDepth(int? depth)
{
var sut = new DeserializerBuilder()
.WithTagMapping("!CycleTag", typeof(CycleTestEntity))
.Build();

StringBuilder builder = new StringBuilder(@"&o0 !CycleTag");
builder.AppendLine();
string indentation;
for (int i = 0; i < depth - 1; ++i)
{
indentation = string.Concat(Enumerable.Repeat(" ", i));
builder.AppendLine($"{indentation}Cycle: !CycleTag");
}
indentation = string.Concat(Enumerable.Repeat(" ", depth.Value - 1));
builder.AppendLine($"{indentation}Cycle: *o0");
var yaml = Yaml.Text(builder.ToString());
var obj = sut.Deserialize<CycleTestEntity>(yaml);
CycleTestEntity iterator = obj;
for (int i = 0; i < depth; ++i)
{
iterator = iterator.Cycle;
}
Assert.Same(obj, iterator);
}

[TypeConverter(typeof(DoublyConvertedTypeConverter))]
public class DoublyConverted
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public object? Value
if (parser.Accept<NodeEvent>(out var nodeEvent) && !nodeEvent.Anchor.IsEmpty)
{
anchor = nodeEvent.Anchor;
var aliasState = state.Get<AliasState>();
if (!aliasState.ContainsKey(anchor))
{
aliasState[anchor] = new ValuePromise(new AnchorAlias(anchor));
}
}

value = innerDeserializer.DeserializeValue(parser, expectedType, state, nestedObjectDeserializer);
Expand Down