From 057934d6f36a6adb952d24588984d16c0bc4e7c2 Mon Sep 17 00:00:00 2001 From: Mike-EEE Date: Sat, 18 Jan 2020 00:59:06 -0500 Subject: [PATCH] Fixed type resolution issue with conflicting member name having the... ... same name as a type in its containing namespace. * Adding sample code. * Fixed issue where type resolution failed with member of same name as a type in same namespace. --- .../Identification/Identifiers.cs | 3 +- .../ContentModel/Members/MemberSerializers.cs | 27 ++++++++---- .../Issue175Tests.cs | 40 +----------------- .../Issue352Tests.cs | 24 +++++++++++ .../Shared/Inspections.cs | 42 +++++++++++++++++++ .../Shared/Worker.cs | 12 ++++++ .../ExtendedXmlSerializer.Tests.csproj | 6 ++- 7 files changed, 105 insertions(+), 49 deletions(-) create mode 100644 test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue352Tests.cs create mode 100644 test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Inspections.cs create mode 100644 test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Worker.cs diff --git a/src/ExtendedXmlSerializer/ContentModel/Identification/Identifiers.cs b/src/ExtendedXmlSerializer/ContentModel/Identification/Identifiers.cs index 08240338e..c32ed5c78 100644 --- a/src/ExtendedXmlSerializer/ContentModel/Identification/Identifiers.cs +++ b/src/ExtendedXmlSerializer/ContentModel/Identification/Identifiers.cs @@ -16,7 +16,6 @@ public Identifiers(IReadOnlyDictionary known, INamespaceFor } public string Get(TypeInfo parameter) - => _known.Get(parameter.Assembly) - ?.Identifier ?? _formatter.Get(parameter); + => _known.Get(parameter.Assembly)?.Identifier ?? _formatter.Get(parameter); } } \ No newline at end of file diff --git a/src/ExtendedXmlSerializer/ContentModel/Members/MemberSerializers.cs b/src/ExtendedXmlSerializer/ContentModel/Members/MemberSerializers.cs index 3c52ca285..4d4707f05 100644 --- a/src/ExtendedXmlSerializer/ContentModel/Members/MemberSerializers.cs +++ b/src/ExtendedXmlSerializer/ContentModel/Members/MemberSerializers.cs @@ -1,6 +1,8 @@ using ExtendedXmlSerializer.ContentModel.Content; using ExtendedXmlSerializer.ContentModel.Conversion; +using ExtendedXmlSerializer.ContentModel.Identification; using ExtendedXmlSerializer.ContentModel.Properties; +using ExtendedXmlSerializer.ContentModel.Reflection; using ExtendedXmlSerializer.Core.Sources; using ExtendedXmlSerializer.ReflectionModel; @@ -12,15 +14,22 @@ sealed class MemberSerializers : IMemberSerializers readonly IAttributeSpecifications _runtime; readonly IMemberConverters _converters; readonly IMemberContents _content; + readonly IIdentityStore _identities; + readonly ITypes _types; + readonly IIdentifiers _identifiers; // ReSharper disable once TooManyDependencies public MemberSerializers(IAttributeSpecifications runtime, IMemberAccessors accessors, - IMemberConverters converters, IMemberContents content) + IMemberConverters converters, IMemberContents content, IIdentityStore identities, + ITypes types, IIdentifiers identifiers) { - _runtime = runtime; - _accessors = accessors; - _converters = converters; - _content = content; + _runtime = runtime; + _accessors = accessors; + _converters = converters; + _content = content; + _identities = identities; + _types = types; + _identifiers = identifiers; } public IMemberSerializer Get(IMember parameter) @@ -44,11 +53,15 @@ IMemberSerializer Property(IConverter converter, IMember profile, IMemberAccess : property; } + bool IsMember(IMember profile) + => CollectionItemTypeLocator.Default.Get(profile.MemberType)?.Name == profile.Name || + _types.Get(_identities.Get(profile.Name, _identifiers.Get(profile.Metadata.ReflectedType))) != null; + IMemberSerializer Content(IMember profile, IMemberAccess access) { var identity = new Identity(profile); - var composite = CollectionItemTypeLocator.Default.Get(profile.MemberType) - ?.Name == profile.Name + var isMember = IsMember(profile); + var composite = isMember ? (IWriter)new MemberPropertyWriter(identity) : identity; var start = composite.Adapt(); diff --git a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue175Tests.cs b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue175Tests.cs index 50362ac55..123cc9e8c 100644 --- a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue175Tests.cs +++ b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue175Tests.cs @@ -1,8 +1,7 @@ using ExtendedXmlSerializer.Configuration; +using ExtendedXmlSerializer.Tests.ReportedIssues.Shared; using ExtendedXmlSerializer.Tests.ReportedIssues.Support; using FluentAssertions; -using System; -using System.Xml.Serialization; using Xunit; namespace ExtendedXmlSerializer.Tests.ReportedIssues @@ -39,7 +38,7 @@ public void None() var support = new ConfigurationContainer().InspectingType() .ForTesting(); support.Assert(new None {UniqueId = "123"}, - @""); + @""); } [Fact] @@ -72,40 +71,5 @@ void VerifyCrossAttributeCombination() } } - [Serializable] - [XmlType(AnonymousType = true, Namespace = "http://namespace/file.xsd")] - [XmlRoot(ElementName = "FooBar", IsNullable = false)] - public class Foo : IFoo - { - [XmlElement("num")] - public int Number { get; set; } - } - - public interface IFoo {} - - [Serializable, XmlType(AnonymousType = true, Namespace = "http://namespace/file.xsd"), - XmlRoot("myMessage", Namespace = "http://namespace/file.xsd", IsNullable = false)] - public class MyMessage - { - /// - [XmlElement("myElement")] - public MyElementType MyElement { get; set; } - } - /// - [XmlType(Namespace = "http://namespace/file.xsd")] - public class MyElementType - { - /// - [XmlAttribute("uniqueId")] - public string UniqueId { get; set; } - } - - [XmlType(Namespace = "")] - public class None - { - /// - [XmlAttribute("uniqueId")] - public string UniqueId { get; set; } - } } \ No newline at end of file diff --git a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue352Tests.cs b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue352Tests.cs new file mode 100644 index 000000000..72cb8b7de --- /dev/null +++ b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue352Tests.cs @@ -0,0 +1,24 @@ +using ExtendedXmlSerializer.Configuration; +using ExtendedXmlSerializer.Tests.ReportedIssues.Shared; +using ExtendedXmlSerializer.Tests.ReportedIssues.Support; +using FluentAssertions; +using Xunit; + +// ReSharper disable All + +namespace ExtendedXmlSerializer.Tests.ReportedIssues +{ + public sealed class Issue352Tests + { + [Fact] + void Verify() + { + var instance = new Worker(); + var cycled = new ConfigurationContainer().Create() + .ForTesting() + .Cycle(instance); + + cycled.Should().BeEquivalentTo(instance); + } + } +} \ No newline at end of file diff --git a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Inspections.cs b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Inspections.cs new file mode 100644 index 000000000..f737acf09 --- /dev/null +++ b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Inspections.cs @@ -0,0 +1,42 @@ +using System; +using System.Xml.Serialization; + +namespace ExtendedXmlSerializer.Tests.ReportedIssues.Shared +{ + [Serializable] + [XmlType(AnonymousType = true, Namespace = "http://namespace/file.xsd")] + [XmlRoot(ElementName = "FooBar", IsNullable = false)] + public class Foo : IFoo + { + [XmlElement("num")] + public int Number { get; set; } + } + + public interface IFoo {} + + [Serializable, XmlType(AnonymousType = true, Namespace = "http://namespace/file.xsd"), + XmlRoot("myMessage", Namespace = "http://namespace/file.xsd", IsNullable = false)] + public class MyMessage + { + /// + [XmlElement("myElement")] + public MyElementType MyElement { get; set; } + } + + /// + [XmlType(Namespace = "http://namespace/file.xsd")] + public class MyElementType + { + /// + [XmlAttribute("uniqueId")] + public string UniqueId { get; set; } + } + + [XmlType(Namespace = "")] + public class None + { + /// + [XmlAttribute("uniqueId")] + public string UniqueId { get; set; } + } +} diff --git a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Worker.cs b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Worker.cs new file mode 100644 index 000000000..308e96ab9 --- /dev/null +++ b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Worker.cs @@ -0,0 +1,12 @@ +namespace ExtendedXmlSerializer.Tests.ReportedIssues.Shared +{ + class Backend {} + + class WorkerBackend {} + + class Worker + { + public string Name { get; set; } = "worker"; + public WorkerBackend Backend { get; set; } = new WorkerBackend(); + } +} diff --git a/test/ExtendedXmlSerializer.Tests/ExtendedXmlSerializer.Tests.csproj b/test/ExtendedXmlSerializer.Tests/ExtendedXmlSerializer.Tests.csproj index 6df37bce2..00dcefcb0 100644 --- a/test/ExtendedXmlSerializer.Tests/ExtendedXmlSerializer.Tests.csproj +++ b/test/ExtendedXmlSerializer.Tests/ExtendedXmlSerializer.Tests.csproj @@ -12,7 +12,7 @@ $(DefineConstants);CORE - + $(DefineConstants);LEGACY @@ -33,7 +33,9 @@ - + + True + True