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

Adjusted reference detection to be more accurate in the case of attri… #400

Merged
merged 1 commit into from
Jun 8, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using JetBrains.Annotations;
using System;
using System.Reflection;
using System.Xml;

namespace ExtendedXmlSerializer.ExtensionModel.References
{
Expand All @@ -13,13 +14,15 @@ sealed class ReferenceActivation : IActivation
readonly Func<TypeInfo, IReader> _activation;
readonly IEntities _entities;
readonly IReferenceMaps _maps;

[UsedImplicitly]
public ReferenceActivation(IActivation activation, IEntities entities) : this(activation.Get, entities) {}
public ReferenceActivation(IActivation activation, IEntities entities)
: this(activation.Get, entities) {}

public ReferenceActivation(Func<TypeInfo, IReader> activation, IEntities entities)
: this(activation, entities, ReferenceMaps.Default) {}

// ReSharper disable once TooManyDependencies
public ReferenceActivation(Func<TypeInfo, IReader> activation, IEntities entities, IReferenceMaps maps)
{
_activation = activation;
Expand All @@ -35,6 +38,7 @@ sealed class Activator : IReader
readonly IEntities _entities;
readonly IReferenceMaps _maps;

// ReSharper disable once TooManyDependencies
public Activator(IReader activator, IEntities entities, IReferenceMaps maps)
{
_activator = activator;
Expand Down Expand Up @@ -63,7 +67,7 @@ public Activator(IReader activator, IEntities entities, IReferenceMaps maps)

public object Get(IFormatReader parameter)
{
var element = !string.IsNullOrEmpty(parameter.Identifier);
var element = parameter.Get().To<XmlReader>().NodeType != XmlNodeType.Attribute;

var declared = element ? Identity(parameter) : null;
var result = _activator.Get(parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ExtendedXmlSerializer.ContentModel.Format;
using ExtendedXmlSerializer.ContentModel.Reflection;
using System.Reflection;
using System.Xml;

namespace ExtendedXmlSerializer.ExtensionModel.References
{
Expand All @@ -27,15 +28,15 @@ public ReferenceReader(IReader reader, IReferenceMaps maps, IEntities entities,

ReferenceIdentity? GetReference(IFormatReader parameter)
{
if (!string.IsNullOrEmpty(parameter.Identifier))
if (parameter.Get().To<XmlReader>().NodeType != XmlNodeType.Attribute)
{
var identity = ReferenceIdentity.Get(parameter);
if (identity.HasValue)
{
return new ReferenceIdentity(identity.Value);
}

var type = _classification.GetClassification(parameter, _definition);
var type = _classification.GetClassification(parameter, _definition);
var entity = _entities.Get(type)?.Reference(parameter);
if (entity != null)
{
Expand Down
48 changes: 48 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue397Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
using FluentAssertions;
using System;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue397Tests
{
[Fact]
public void Verify()
{
var serializer = new ConfigurationContainer().UseAutoFormatting()
.UseOptimizedNamespaces()
.EnableImplicitTyping(typeof(MainDTO),
typeof(SubDTO), typeof(SubSubDTO))
.Type<SubSubDTO>()
.EnableReferences(definition => definition.Id)
.Create()
.ForTesting();

var instance = new MainDTO();
var cycled = serializer.Cycle(instance);
cycled.Sub1.SubSub1.Should().BeSameAs(cycled.Sub2.SubSub1);
cycled.Sub1.SubSub1.Should().BeSameAs(cycled.Sub3.SubSub1);
}

public class MainDTO
{
public SubDTO Sub1 { get; set; } = new SubDTO();
public SubDTO Sub2 { get; set; } = new SubDTO();
public SubDTO Sub3 { get; set; } = new SubDTO();
}

public class SubDTO
{
public SubSubDTO SubSub1 { get; set; } = SubSubDTO.NullObject;
}

public class SubSubDTO
{
public static SubSubDTO NullObject { get; } = new SubSubDTO();

public string Id { get; set; } = Guid.NewGuid().ToString();
}
}
}