Skip to content

Commit

Permalink
Fixed type resolution issue with conflicting member name having the...
Browse files Browse the repository at this point in the history
... 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.
  • Loading branch information
Mike-EEE authored Jan 18, 2020
1 parent 9a5b3a9 commit 057934d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public Identifiers(IReadOnlyDictionary<Assembly, IIdentity> known, INamespaceFor
}

public string Get(TypeInfo parameter)
=> _known.Get(parameter.Assembly)
?.Identifier ?? _formatter.Get(parameter);
=> _known.Get(parameter.Assembly)?.Identifier ?? _formatter.Get(parameter);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)
Expand All @@ -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<object>(profile);
var composite = CollectionItemTypeLocator.Default.Get(profile.MemberType)
?.Name == profile.Name
var isMember = IsMember(profile);
var composite = isMember
? (IWriter<object>)new MemberPropertyWriter(identity)
: identity;
var start = composite.Adapt();
Expand Down
40 changes: 2 additions & 38 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue175Tests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,7 +38,7 @@ public void None()
var support = new ConfigurationContainer().InspectingType<None>()
.ForTesting();
support.Assert(new None {UniqueId = "123"},
@"<?xml version=""1.0"" encoding=""utf-8""?><None uniqueId=""123"" xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ReportedIssues;assembly=ExtendedXmlSerializer.Tests.ReportedIssues"" />");
@"<?xml version=""1.0"" encoding=""utf-8""?><None uniqueId=""123"" xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ReportedIssues.Shared;assembly=ExtendedXmlSerializer.Tests.ReportedIssues"" />");
}

[Fact]
Expand Down Expand Up @@ -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
{
/// <remarks />
[XmlElement("myElement")]
public MyElementType MyElement { get; set; }
}

/// <remarks />
[XmlType(Namespace = "http://namespace/file.xsd")]
public class MyElementType
{
/// <remarks />
[XmlAttribute("uniqueId")]
public string UniqueId { get; set; }
}

[XmlType(Namespace = "")]
public class None
{
/// <remarks />
[XmlAttribute("uniqueId")]
public string UniqueId { get; set; }
}
}
24 changes: 24 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue352Tests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <remarks />
[XmlElement("myElement")]
public MyElementType MyElement { get; set; }
}

/// <remarks />
[XmlType(Namespace = "http://namespace/file.xsd")]
public class MyElementType
{
/// <remarks />
[XmlAttribute("uniqueId")]
public string UniqueId { get; set; }
}

[XmlType(Namespace = "")]
public class None
{
/// <remarks />
[XmlAttribute("uniqueId")]
public string UniqueId { get; set; }
}
}
12 changes: 12 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Shared/Worker.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<DefineConstants>$(DefineConstants);CORE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net452' ">
<PropertyGroup Condition=" '$(TargetFramework)' == 'net452' ">
<DefineConstants>$(DefineConstants);LEGACY</DefineConstants>
</PropertyGroup>

Expand All @@ -33,7 +33,9 @@
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp2.1' ">
<Reference Include="WindowsBase" />
<Reference Include="WindowsBase">
<Private>True</Private>
</Reference>
<Reference Include="System.Data">
<Private>True</Private>
</Reference>
Expand Down

0 comments on commit 057934d

Please sign in to comment.