Skip to content

Commit

Permalink
Fixed problem with implicit type/optimized namespace scenarios. (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo authored Sep 1, 2021
1 parent 44a9b2f commit 8e702ea
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 52 deletions.
9 changes: 1 addition & 8 deletions src/ExtendedXmlSerializer/ExtensionModel/Xml/XmlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ public void Start(IIdentity identity)
var identifier = identity.Identifier.NullIfEmpty();
if (identifier != null)
{
switch (Count)
{
case 0:
Add(string.Empty, identifier);
break;
}

_writer.WriteStartElement(identity.Name, identifier);
}
else
Expand Down Expand Up @@ -141,7 +134,7 @@ string CreatePrefix(string identifier)

string Create(string parameter)
{
var result = _formatter.Get(Count);
var result = _formatter.Get(Count + 1);
Add(result, parameter);
return result;
}
Expand Down
73 changes: 73 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue545Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
using FluentAssertions;
using System.Collections.Generic;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue545Tests
{
[Fact]
public void Verify()
{
var instance = new ClassWithDictionary();
instance.Dict.Add("key",123);

var configuration = new ConfigurationContainer();
var serializer = configuration.Create().ForTesting();

serializer.Cycle(instance).Should().BeEquivalentTo(instance);
}

[Fact]
public void VerifyImplicitTyping()
{
var instance = new ClassWithDictionary();
instance.Dict.Add("key",123);

var configuration = new ConfigurationContainer().EnableImplicitTyping(typeof(ClassWithDictionary));
var serializer = configuration.Create().ForTesting();

//serializer.WriteLine(instance);

serializer.Cycle(instance).Should().BeEquivalentTo(instance);
}

[Fact]
public void VerifyOptimizedImplicitTyping()
{
var instance = new ClassWithDictionary();
instance.Dict.Add("key",123);

var configuration = new ConfigurationContainer().UseOptimizedNamespaces().EnableImplicitTyping(typeof(ClassWithDictionary));
var serializer = configuration.Create().ForTesting();

//serializer.WriteLine(instance);

serializer.Cycle(instance).Should().BeEquivalentTo(instance);
}


public class ClassWithDictionary
{
public ClassWithDictionary()
{
Dict = new Dictionary<string, object>();

ListWithItems = new List<ClassWithProperty>();
ListWithItems.Add(new ClassWithProperty());
}

public Dictionary<string,object> Dict { get; set; }

public IList<ClassWithProperty> ListWithItems { get; set; }
}

public class ClassWithProperty
{
public string Text { get; set; }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void SimpleIdentity()
var support = new SerializationSupport(new ConfigurationContainer().EnableReferences()
.Create());
var instance = new Subject
{Id = new Guid("{0E2DECA4-CC38-46BA-9C47-94B8070D7353}"), PropertyName = "Hello World!"};
{ Id = new Guid("{0E2DECA4-CC38-46BA-9C47-94B8070D7353}"), PropertyName = "Hello World!" };
instance.Self = instance;
var actual = support.Assert(instance,
@"<?xml version=""1.0"" encoding=""utf-8""?><ReferencesExtensionTests-Subject xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:identity=""1"" xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.References;assembly=ExtendedXmlSerializer.Tests""><Id>0e2deca4-cc38-46ba-9c47-94b8070d7353</Id><Self exs:reference=""1"" /><PropertyName>Hello World!</PropertyName></ReferencesExtensionTests-Subject>");
Expand All @@ -38,7 +38,7 @@ public void EnabledWithoutConfiguration()
{
Id = Guid,
PropertyName = "Primary Root",
Self = new Subject {Id = Guid, PropertyName = "Another subject"}
Self = new Subject { Id = Guid, PropertyName = "Another subject" }
};
var actual = support.Assert(expected,
@"<?xml version=""1.0"" encoding=""utf-8""?><ReferencesExtensionTests-Subject xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ExtensionModel.References;assembly=ExtendedXmlSerializer.Tests""><Id>6dbb618f-dbbd-4909-9644-a1d955f06249</Id><Self><Id>6dbb618f-dbbd-4909-9644-a1d955f06249</Id><PropertyName>Another subject</PropertyName></Self><PropertyName>Primary Root</PropertyName></ReferencesExtensionTests-Subject>");
Expand Down Expand Up @@ -80,14 +80,14 @@ public void ComplexInstance()
var instance = new TestClassReference
{
Id = 1,
ObjectA = new TestClassReference {Id = 2}
ObjectA = new TestClassReference { Id = 2 }
};
instance.CyclicReference = instance;
instance.ReferenceToObjectA = instance.ObjectA;
instance.Lists = new List<IReference>
{
new TestClassReference {Id = 3},
new TestClassReference {Id = 4}
new TestClassReference { Id = 3 },
new TestClassReference { Id = 4 }
};

var actual = support.Assert(instance,
Expand All @@ -106,8 +106,8 @@ public void PropertyInterfaceOfList()
{
var expected = new ClassWithPropertyInterfaceOfList
{
List = new List<string> {"Item1"},
Set = new List<string> {"Item1"}
List = new List<string> { "Item1" },
Set = new List<string> { "Item1" }
};

var actual = new SerializationSupport(new ConfigurationContainer()).Cycle(expected);
Expand All @@ -123,8 +123,8 @@ public void PropertyInterfaceOfListEnabledReferences()
{
var expected = new ClassWithPropertyInterfaceOfList
{
List = new List<string> {"Item1"},
Set = new List<string> {"Item1"}
List = new List<string> { "Item1" },
Set = new List<string> { "Item1" }
};

var container = new ConfigurationContainer();
Expand All @@ -142,8 +142,8 @@ public void PropertyInterfaceOfListReferencesCleared()
{
var expected = new ClassWithPropertyInterfaceOfList
{
List = new List<string> {"Item1"},
Set = new List<string> {"Item1"}
List = new List<string> { "Item1" },
Set = new List<string> { "Item1" }
};

var container = new ConfigurationContainer();
Expand All @@ -160,8 +160,8 @@ public void PropertyInterfaceOfListReferencesCleared()
[Fact]
public void GeneralInheritance()
{
var first = new ChildClass {Name = "Key"};
var instance = new Container {First = first, Second = first};
var first = new ChildClass { Name = "Key" };
var instance = new Container { First = first, Second = first };
var container = new ConfigurationContainer();
container.EnableReferences();
var support = new SerializationSupport(container);
Expand All @@ -175,8 +175,8 @@ public void GeneralInheritance()
[Fact]
public void SpecificParentInheritance()
{
var first = new ChildClass {Name = "Key"};
var instance = new Container {First = first, Second = first};
var first = new ChildClass { Name = "Key" };
var instance = new Container { First = first, Second = first };
var container = new ConfigurationContainer();
container.Type<ParentClass>()
.EnableReferences(x => x.Name);
Expand All @@ -191,8 +191,8 @@ public void SpecificParentInheritance()
[Fact]
public void SpecificChildInheritance()
{
var first = new ChildClass {Name = "Key"};
var instance = new Container {First = first, Second = first};
var first = new ChildClass { Name = "Key" };
var instance = new Container { First = first, Second = first };
var container = new ConfigurationContainer();
container.Type<ChildClass>()
.EnableReferences(x => x.Name);
Expand Down Expand Up @@ -234,13 +234,13 @@ public void ComplexList()
.EnableReferences(x => x.Id);
var support = new SerializationSupport(container);

var instance = new TestClassReferenceWithList {Parent = new TestClassReference {Id = 1}};
var instance = new TestClassReferenceWithList { Parent = new TestClassReference { Id = 1 } };
var other = new TestClassReference
{Id = 2, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent};
{ Id = 2, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent };
instance.All = new List<IReference>
{
new TestClassReference {Id = 3, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent},
new TestClassReference {Id = 4, ObjectA = other, ReferenceToObjectA = other},
new TestClassReference { Id = 3, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent },
new TestClassReference { Id = 4, ObjectA = other, ReferenceToObjectA = other },
other,
instance.Parent
};
Expand Down Expand Up @@ -288,20 +288,21 @@ public void Dictionary()
}
};
var other = new TestClassReference
{Id = 2, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent};
{ Id = 2, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent };

instance.All = new Dictionary<int, IReference>
{
{
3, new TestClassReference {Id = 3, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent}
3,
new TestClassReference { Id = 3, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent }
},
{4, new TestClassReference {Id = 4, ObjectA = other, ReferenceToObjectA = other}},
{2, other},
{1, instance.Parent}
{ 4, new TestClassReference { Id = 4, ObjectA = other, ReferenceToObjectA = other } },
{ 2, other },
{ 1, instance.Parent }
};

var actual = support.Assert(instance,
@"<?xml version=""1.0"" encoding=""utf-8""?><TestClassReferenceWithDictionary xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.TestObject;assembly=ExtendedXmlSerializer.Tests""><Parent xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""TestClassReference"" Id=""1""><Name>Hello World, this is a Name!</Name></Parent><All><Item xmlns=""https://extendedxmlserializer.github.io/system""><Key>3</Key><Value xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""TestClassReference"" Id=""3""><ObjectA exs:type=""TestClassReference"" exs:entity=""1"" /><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""1"" /></Value></Item><Item xmlns=""https://extendedxmlserializer.github.io/system""><Key>4</Key><Value xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""TestClassReference"" Id=""4""><ObjectA exs:type=""TestClassReference"" Id=""2""><ObjectA exs:type=""TestClassReference"" exs:entity=""1"" /><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""1"" /></ObjectA><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""2"" /></Value></Item><Item xmlns=""https://extendedxmlserializer.github.io/system""><Key>2</Key><Value xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""TestClassReference"" exs:entity=""2"" /></Item><Item xmlns=""https://extendedxmlserializer.github.io/system""><Key>1</Key><Value xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""TestClassReference"" exs:entity=""1"" /></Item></All></TestClassReferenceWithDictionary>");
@"<?xml version=""1.0"" encoding=""utf-8""?><TestClassReferenceWithDictionary xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.TestObject;assembly=ExtendedXmlSerializer.Tests""><Parent xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""TestClassReference"" Id=""1""><Name>Hello World, this is a Name!</Name></Parent><All><Item xmlns=""https://extendedxmlserializer.github.io/system""><Key>3</Key><Value xmlns:ns1=""clr-namespace:ExtendedXmlSerializer.Tests.TestObject;assembly=ExtendedXmlSerializer.Tests"" xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""ns1:TestClassReference"" Id=""3""><ObjectA exs:type=""ns1:TestClassReference"" exs:entity=""1"" /><ReferenceToObjectA exs:type=""ns1:TestClassReference"" exs:entity=""1"" /></Value></Item><Item xmlns=""https://extendedxmlserializer.github.io/system""><Key>4</Key><Value xmlns:ns2=""clr-namespace:ExtendedXmlSerializer.Tests.TestObject;assembly=ExtendedXmlSerializer.Tests"" xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""ns2:TestClassReference"" Id=""4""><ObjectA exs:type=""ns2:TestClassReference"" Id=""2""><ObjectA exs:type=""ns2:TestClassReference"" exs:entity=""1"" /><ReferenceToObjectA exs:type=""ns2:TestClassReference"" exs:entity=""1"" /></ObjectA><ReferenceToObjectA exs:type=""ns2:TestClassReference"" exs:entity=""2"" /></Value></Item><Item xmlns=""https://extendedxmlserializer.github.io/system""><Key>2</Key><Value xmlns:ns3=""clr-namespace:ExtendedXmlSerializer.Tests.TestObject;assembly=ExtendedXmlSerializer.Tests"" xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""ns3:TestClassReference"" exs:entity=""2"" /></Item><Item xmlns=""https://extendedxmlserializer.github.io/system""><Key>1</Key><Value xmlns:ns4=""clr-namespace:ExtendedXmlSerializer.Tests.TestObject;assembly=ExtendedXmlSerializer.Tests"" xmlns:exs=""https://extendedxmlserializer.github.io/v2"" exs:type=""ns4:TestClassReference"" exs:entity=""1"" /></Item></All></TestClassReferenceWithDictionary>");
Assert.NotNull(actual.Parent);
var list = actual.All;
Assert.Same(actual.Parent, list[3]
Expand All @@ -328,28 +329,28 @@ public void Dictionary()
[Fact]
public void OptimizedDictionary()
{
var container = new ConfigurationContainer();
container.UseOptimizedNamespaces()
.Type<TestClassReference>()
.EnableReferences(x => x.Id);
var support = container.ForTesting();
var container = new ConfigurationContainer().UseOptimizedNamespaces()
.Type<TestClassReference>()
.EnableReferences(x => x.Id)
.ForTesting();

var instance = new TestClassReferenceWithDictionary {Parent = new TestClassReference {Id = 1}};
var instance = new TestClassReferenceWithDictionary { Parent = new TestClassReference { Id = 1 } };
var other = new TestClassReference
{Id = 2, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent};
{ Id = 2, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent };

instance.All = new Dictionary<int, IReference>
{
{
3, new TestClassReference {Id = 3, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent}
3,
new TestClassReference { Id = 3, ObjectA = instance.Parent, ReferenceToObjectA = instance.Parent }
},
{4, new TestClassReference {Id = 4, ObjectA = other, ReferenceToObjectA = other}},
{2, other},
{1, instance.Parent}
{ 4, new TestClassReference { Id = 4, ObjectA = other, ReferenceToObjectA = other } },
{ 2, other },
{ 1, instance.Parent }
};

var actual = support.Assert(instance,
@"<?xml version=""1.0"" encoding=""utf-8""?><TestClassReferenceWithDictionary xmlns:exs=""https://extendedxmlserializer.github.io/v2"" xmlns:sys=""https://extendedxmlserializer.github.io/system"" xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.TestObject;assembly=ExtendedXmlSerializer.Tests""><Parent exs:type=""TestClassReference"" Id=""1"" /><All><sys:Item><Key>3</Key><Value exs:type=""TestClassReference"" Id=""3""><ObjectA exs:type=""TestClassReference"" exs:entity=""1"" /><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""1"" /></Value></sys:Item><sys:Item><Key>4</Key><Value exs:type=""TestClassReference"" Id=""4""><ObjectA exs:type=""TestClassReference"" Id=""2""><ObjectA exs:type=""TestClassReference"" exs:entity=""1"" /><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""1"" /></ObjectA><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""2"" /></Value></sys:Item><sys:Item><Key>2</Key><Value exs:type=""TestClassReference"" exs:entity=""2"" /></sys:Item><sys:Item><Key>1</Key><Value exs:type=""TestClassReference"" exs:entity=""1"" /></sys:Item></All></TestClassReferenceWithDictionary>");
var actual = container.Assert(instance,
@"<?xml version=""1.0"" encoding=""utf-8""?><TestClassReferenceWithDictionary xmlns:exs=""https://extendedxmlserializer.github.io/v2"" xmlns:sys=""https://extendedxmlserializer.github.io/system"" xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.TestObject;assembly=ExtendedXmlSerializer.Tests""><Parent exs:type=""TestClassReference"" Id=""1"" /><All><sys:Item><Key>3</Key><Value exs:type=""TestClassReference"" Id=""3""><ObjectA exs:type=""TestClassReference"" exs:entity=""1"" /><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""1"" /></Value></sys:Item><sys:Item><Key>4</Key><Value exs:type=""TestClassReference"" Id=""4""><ObjectA exs:type=""TestClassReference"" Id=""2""><ObjectA exs:type=""TestClassReference"" exs:entity=""1"" /><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""1"" /></ObjectA><ReferenceToObjectA exs:type=""TestClassReference"" exs:entity=""2"" /></Value></sys:Item><sys:Item><Key>2</Key><Value exs:type=""TestClassReference"" exs:entity=""2"" /></sys:Item><sys:Item><Key>1</Key><Value exs:type=""TestClassReference"" exs:entity=""1"" /></sys:Item></All></TestClassReferenceWithDictionary>");
Assert.NotNull(actual.Parent);
var list = actual.All;
Assert.Same(actual.Parent, list[3]
Expand Down Expand Up @@ -381,13 +382,13 @@ public void List()
.EnableReferences(x => x.Id);
var support = new SerializationSupport(container);

var parent = new TestClassReference {Id = 1};
var other = new TestClassReference {Id = 2, ObjectA = parent, ReferenceToObjectA = parent};
var parent = new TestClassReference { Id = 1 };
var other = new TestClassReference { Id = 2, ObjectA = parent, ReferenceToObjectA = parent };

var instance = new List<IReference>
{
new TestClassReference {Id = 3, ObjectA = parent, ReferenceToObjectA = parent},
new TestClassReference {Id = 4, ObjectA = other, ReferenceToObjectA = other},
new TestClassReference { Id = 3, ObjectA = parent, ReferenceToObjectA = parent },
new TestClassReference { Id = 4, ObjectA = other, ReferenceToObjectA = other },
other,
parent
};
Expand Down

0 comments on commit 8e702ea

Please sign in to comment.