From 6cc226ed69be92780b2945b4bc5969d7b38e0702 Mon Sep 17 00:00:00 2001 From: Mike-E Date: Sat, 10 Oct 2020 09:54:33 -0400 Subject: [PATCH] Skipped processing of parameterized-content members that have custom serializers applied. --- .../Members/ParameterizedTypeMembers.cs | 33 ++++++++---- .../Issue458Tests.cs | 53 +++++++++++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue458Tests.cs diff --git a/src/ExtendedXmlSerializer/ExtensionModel/Content/Members/ParameterizedTypeMembers.cs b/src/ExtendedXmlSerializer/ExtensionModel/Content/Members/ParameterizedTypeMembers.cs index d1fbc68f1..3bd344023 100644 --- a/src/ExtendedXmlSerializer/ExtensionModel/Content/Members/ParameterizedTypeMembers.cs +++ b/src/ExtendedXmlSerializer/ExtensionModel/Content/Members/ParameterizedTypeMembers.cs @@ -1,29 +1,44 @@ +using ExtendedXmlSerializer.ContentModel.Identification; +using ExtendedXmlSerializer.ContentModel.Members; +using ExtendedXmlSerializer.ExtensionModel.Xml; +using System; using System.Collections.Immutable; using System.Linq; using System.Reflection; -using ExtendedXmlSerializer.ContentModel.Identification; -using ExtendedXmlSerializer.ContentModel.Members; namespace ExtendedXmlSerializer.ExtensionModel.Content.Members { sealed class ParameterizedTypeMembers : ITypeMembers { - readonly ITypeMembers _typed; - readonly IParameterizedMembers _members; + readonly ITypeMembers _previous; + readonly IContainsCustomSerialization _custom; + readonly IParameterizedMembers _members; + readonly Func _group; + + public ParameterizedTypeMembers(ITypeMembers previous, IContainsCustomSerialization custom, + IParameterizedMembers members) + : this(previous, custom, members, IdentityFormatter.Default.Get) {} - public ParameterizedTypeMembers(ITypeMembers typed, IParameterizedMembers members) + // ReSharper disable once TooManyDependencies + public ParameterizedTypeMembers(ITypeMembers previous, IContainsCustomSerialization custom, + IParameterizedMembers members, Func group) { - _typed = typed; - _members = members; + _previous = previous; + _custom = custom; + _members = members; + _group = @group; } public ImmutableArray Get(TypeInfo parameter) + => _custom.IsSatisfiedBy(parameter) ? ImmutableArray.Empty : Implementation(parameter); + + ImmutableArray Implementation(TypeInfo parameter) { var members = _members.Get(parameter); - var typed = _typed.Get(parameter); + var typed = _previous.Get(parameter); var items = members?.AddRange(typed) ?? typed; - var result = items.GroupBy(IdentityFormatter.Default.Get) + var result = items.GroupBy(_group) .Select(x => x.OfType() .FirstOrDefault() ?? x.First()) .ToImmutableArray(); diff --git a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue458Tests.cs b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue458Tests.cs new file mode 100644 index 000000000..c88965dad --- /dev/null +++ b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue458Tests.cs @@ -0,0 +1,53 @@ +using ExtendedXmlSerializer.Configuration; +using ExtendedXmlSerializer.ContentModel; +using ExtendedXmlSerializer.ContentModel.Format; +using ExtendedXmlSerializer.Tests.ReportedIssues.Support; +using System; +using Xunit; + +namespace ExtendedXmlSerializer.Tests.ReportedIssues +{ + public sealed class Issue458Tests + { + [Fact] + public void Verify() + { + var instance = new DecoratedImage(null); + + var serializer = new ConfigurationContainer().EnableParameterizedContentWithPropertyAssignments() + .UseOptimizedNamespaces() + // + .Type() + .Register() + .Serializer() + .Of() + // + .Create() + .ForTesting(); + + serializer.Assert(instance, @""); + } + + public class ImageSerializer : ISerializer + { + public DecoratedImage Get(IFormatReader parameter) => new DecoratedImage(null); + + public void Write(IFormatWriter writer, DecoratedImage instance) {} + } + + public class DecoratedImage + { + public DecoratedImage(Metadata metadata) => Metadata = metadata; + + public Metadata Metadata + { + // ReSharper disable once ThrowExceptionInUnexpectedLocation + get => throw new NotImplementedException(); + // ReSharper disable once RedundantAssignment + set => value = null; + } + } + + public class Metadata {} + } +} \ No newline at end of file