Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Deserialization of inherited class with different namespaces not possible with Migration (Issue 612) #613

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/ExtendedXmlSerializer/ExtensionModel/Xml/MigrationsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,21 @@ public IFormatReader Get(IFormatReader parameter)

foreach (var attribute in attributes)
{
var prefix = attribute.Name.LocalName;
if (manager.LookupNamespace(prefix) == null)
{
var @namespace = element.GetNamespaceOfPrefix(prefix);
if (@namespace != null)
{
manager.AddNamespace(prefix, @namespace.NamespaceName);
}
}
}
var prefix = attribute.Name.LocalName;
if (prefix == "xmlns")
{
//Add default namespace to the manager
manager.AddNamespace(string.Empty, attribute.Value);
}
else if (manager.LookupNamespace(prefix) == null)
{
var @namespace = element.GetNamespaceOfPrefix(prefix);
if (@namespace != null)
{
manager.AddNamespace(prefix, @namespace.NamespaceName);
}
}
}
}

XmlParserContexts.Default.Assign(native.NameTable, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ sealed class ReaderFormatter : IReaderFormatter

ReaderFormatter() {}

public string Get(IFormatReader parameter) => ((System.Xml.XmlReader)parameter.Get()).Name;
public string Get(IFormatReader parameter) => ((System.Xml.XmlReader)parameter.Get()).LocalName;
}
}
108 changes: 108 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue612Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
using FluentAssertions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue612Tests
{
[Fact]
public void Verify()
{
var serializer = new ConfigurationContainer().Type<BaseNamespace.Container>()
.AddMigration(EmptyMigration.Default)
.Create()
.ForTesting();

var container = new BaseNamespace.Container();
container.Content.Add(new InheritNamespace.Inherit() { Check = new InheritNamespace.InheritCheck() });
serializer.Cycle(container).Should().BeEquivalentTo(container);
}

[Fact]
public void VerifyWithoutMigration()
{
var serializer = new ConfigurationContainer().Type<BaseNamespace.Container>()
.Create()
.ForTesting();

var container = new BaseNamespace.Container();
container.Content.Add(new InheritNamespace.Inherit() { Check = new InheritNamespace.InheritCheck() });
serializer.Cycle(container).Should().BeEquivalentTo(container);
}

[Fact]
public void VerifyComplex()
{
var serializer = new ConfigurationContainer().Type<BaseNamespace.Container>()
.AddMigration(EmptyMigration.Default)
.Create()
.ForTesting();

var container = new BaseNamespace.Container();
container.Content.Add(new BaseNamespace.Base() { Check = new BaseNamespace.BaseCheck() });
container.Content.Add(new InheritNamespace.Inherit() { Check = new InheritNamespace.InheritCheck() });
container.Content.Add(new InheritNamespace.Inherit() { Check = new BaseNamespace.BaseCheck() });
serializer.Cycle(container).Should().BeEquivalentTo(container);
}

[Fact]
public void VerifyComplexWithoutMigration()
{
var serializer = new ConfigurationContainer().Type<BaseNamespace.Container>()
.Create()
.ForTesting();

var container = new BaseNamespace.Container();
container.Content.Add(new BaseNamespace.Base() { Check = new BaseNamespace.BaseCheck() });
container.Content.Add(new InheritNamespace.Inherit() { Check = new InheritNamespace.InheritCheck() });
container.Content.Add(new InheritNamespace.Inherit() { Check = new BaseNamespace.BaseCheck() });
serializer.Cycle(container).Should().BeEquivalentTo(container);
}

}
sealed class EmptyMigration : IEnumerable<Action<XElement>>
{
public static EmptyMigration Default { get; } = new EmptyMigration();

EmptyMigration() { }

public IEnumerator<Action<XElement>> GetEnumerator()
{
yield break;
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}

namespace BaseNamespace
{

public class Container
{
public List<Base> Content { get; set; } = new List<Base>();
}

public class Base
{
public BaseCheck Check { get; set; }
}
public class BaseCheck { }
}

namespace InheritNamespace
{

public class Inherit : BaseNamespace.Base { }


public class InheritCheck : BaseNamespace.BaseCheck { }

}

39 changes: 39 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue614Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
using FluentAssertions;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue614Tests
{
[Fact]
public void Verify()
{
var serializer = new ConfigurationContainer().Type<BaseNamespace.Container>()
.AddMigration(EmptyMigration.Default)
.Create()
.ForTesting();

var container = new BaseNamespace.Container();
container.Content.Add(new InheritNamespace.Inherit() { Check = new BaseNamespace.BaseCheck() });
container.Content.Add(new BaseNamespace.Base() { Check = new InheritNamespace.InheritCheck() });
serializer.Cycle(container).Should().BeEquivalentTo(container);
}

[Fact]
public void VerifyWithoutMigration()
{
var serializer = new ConfigurationContainer().Type<BaseNamespace.Container>()
.Create()
.ForTesting();

var container = new BaseNamespace.Container();
container.Content.Add(new InheritNamespace.Inherit() { Check = new BaseNamespace.BaseCheck() });
container.Content.Add(new BaseNamespace.Base() { Check = new InheritNamespace.InheritCheck() });
serializer.Cycle(container).Should().BeEquivalentTo(container);
}

}
}