From 038ca826b0979de43c1afad56db4afc98a367399 Mon Sep 17 00:00:00 2001 From: Mike-E Date: Mon, 28 Sep 2020 05:07:11 -0400 Subject: [PATCH] Scoped refereence encounter counter to per-writer. --- .../References/ReferenceEncounters.cs | 34 ++++++++++------- .../Issue454Tests.cs | 38 +++++++++++++++++++ 2 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue454Tests.cs diff --git a/src/ExtendedXmlSerializer/ExtensionModel/References/ReferenceEncounters.cs b/src/ExtendedXmlSerializer/ExtensionModel/References/ReferenceEncounters.cs index b0b37bb97..622543adc 100644 --- a/src/ExtendedXmlSerializer/ExtensionModel/References/ReferenceEncounters.cs +++ b/src/ExtendedXmlSerializer/ExtensionModel/References/ReferenceEncounters.cs @@ -1,35 +1,41 @@ -using System.Linq; -using System.Reflection; using ExtendedXmlSerializer.ContentModel.Format; using ExtendedXmlSerializer.Core; using ExtendedXmlSerializer.Core.Sources; using JetBrains.Annotations; +using System.Linq; namespace ExtendedXmlSerializer.ExtensionModel.References { sealed class ReferenceEncounters : ReferenceCacheBase, IReferenceEncounters { - readonly IRootReferences _references; - readonly IEntities _entities; - readonly ObjectIdGenerator _generator; + readonly IRootReferences _references; + readonly IEntities _entities; [UsedImplicitly] public ReferenceEncounters(IRootReferences references, IEntities entities) - : this(references, entities, new ObjectIdGenerator()) {} - - public ReferenceEncounters(IRootReferences references, IEntities entities, ObjectIdGenerator generator) { _references = references; _entities = entities; - _generator = generator; } protected override IEncounters Create(IFormatWriter parameter) - => new Encounters(_references.Get(parameter) - .ToDictionary(x => x, Get)); + => new Encounters(_references.Get(parameter).ToDictionary(x => x, new Generators(_entities).Get)); + + sealed class Generators : StructureCacheBase + { + readonly IEntities _entities; + readonly ObjectIdGenerator _generator; + + public Generators(IEntities entities) : this(entities, new ObjectIdGenerator()) {} - Identifier Get(object parameter) - => new Identifier(_generator.For(parameter), _entities.Get(parameter.GetType() - .GetTypeInfo())); + public Generators(IEntities entities, ObjectIdGenerator generator) + { + _entities = entities; + _generator = generator; + } + + protected override Identifier Create(object parameter) + => new Identifier(_generator.For(parameter), _entities.Get(parameter.GetType())); + } } } \ No newline at end of file diff --git a/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue454Tests.cs b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue454Tests.cs new file mode 100644 index 000000000..20074b4db --- /dev/null +++ b/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue454Tests.cs @@ -0,0 +1,38 @@ +using ExtendedXmlSerializer.Configuration; +using ExtendedXmlSerializer.Tests.ReportedIssues.Support; +using FluentAssertions; +using Xunit; + +namespace ExtendedXmlSerializer.Tests.ReportedIssues +{ + public sealed class Issue454Tests + { + [Fact] + public void Verify() + { + var serializer = new ConfigurationContainer().UseAutoFormatting() + .UseOptimizedNamespaces() + .EnableReferences() + .Create() + .ForTesting(); + + var myRef = new Foo(); + var instance = new Foo + { + Ref1 = myRef, + Ref2 = myRef + }; + + var first = serializer.Serialize(serializer.Cycle(instance)); + var second = serializer.Serialize(serializer.Cycle(instance)); + + first.Should().Be(second); + } + + public class Foo + { + public Foo Ref1 { get; set; } + public Foo Ref2 { get; set; } + } + } +} \ No newline at end of file