From 09a649849529d0ae42147cf84b16776f8e402833 Mon Sep 17 00:00:00 2001 From: Mike-E Date: Wed, 19 May 2021 03:31:10 -0400 Subject: [PATCH] Fixed `WithUnknownContent().Continue` to account for complex content --- .../Content/ContinueOnUnknownContent.cs | 13 +++ .../Issue521Tests.cs | 95 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue521Tests.cs diff --git a/src/ExtendedXmlSerializer/ExtensionModel/Content/ContinueOnUnknownContent.cs b/src/ExtendedXmlSerializer/ExtensionModel/Content/ContinueOnUnknownContent.cs index fb8f4d03c..26497250f 100644 --- a/src/ExtendedXmlSerializer/ExtensionModel/Content/ContinueOnUnknownContent.cs +++ b/src/ExtendedXmlSerializer/ExtensionModel/Content/ContinueOnUnknownContent.cs @@ -1,5 +1,6 @@ using ExtendedXmlSerializer.ContentModel.Format; using ExtendedXmlSerializer.Core; +using ExtendedXmlSerializer.ExtensionModel.Xml; namespace ExtendedXmlSerializer.ExtensionModel.Content { @@ -11,7 +12,19 @@ sealed class ContinueOnUnknownContent : ICommand public void Execute(IFormatReader parameter) { + var reader = parameter.Get().To(); + var depth = XmlDepth.Default.Get(reader); + parameter.Content(); + + if (depth.HasValue) + { + while (XmlDepth.Default.Get(reader) > depth) + { + reader.Skip(); + parameter.Set(); + } + } } } } \ No newline at end of file diff --git a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue521Tests.cs b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue521Tests.cs new file mode 100644 index 000000000..a7854da16 --- /dev/null +++ b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue521Tests.cs @@ -0,0 +1,95 @@ +using ExtendedXmlSerializer.Configuration; +using FluentAssertions; +using Xunit; + +namespace ExtendedXmlSerializer.Tests.ReportedIssues +{ + public sealed class Issue521Tests + { + [Fact] + public void Verify() + { + const string document = + @" + + 11 + + 1 + + 22 + +"; + + var serializer = new ConfigurationContainer().EnableImplicitTyping(typeof(Test)) + .WithUnknownContent() + .Continue() + //.Call(_ => Console.WriteLine("Unknown content: " + _.Name)) + .Create(); + var instance = serializer.Deserialize(document); + instance.Value2.Should().Be(22); + } + + [Fact] + public void VerifyComprehensive() + { + const string document = + @" + + 11 + + 1 + + 22 + + 4 + 1 + 2 + 3 + + 33 + + 4 + + 1 + + + 2 + + + 3 + + + 44 +"; + + var serializer = new ConfigurationContainer().EnableImplicitTyping(typeof(Test), typeof(Item)) + .WithUnknownContent() + .Continue() + //.Call(_ => Console.WriteLine("Unknown content: " + _.Name)) + .Create(); + var instance = serializer.Deserialize(document); + instance.Value2.Should().Be(22); + instance.Value3.Should().Be(33); + instance.Value4.Should().Be(44); + } + + public class Test + { + public int Value1 { get; set; } + + //public Item Item1 { get; set; } + public int Value2 { get; set; } + + //public List Items2 { get; set; } + public int Value3 { get; set; } + + //public List Items3 { get; set; } + public int Value4 { get; set; } + } + + public class Item + { + public int Value { get; set; } + } + } +} \ No newline at end of file