-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
14 deletions.
There are no files selected for viewing
34 changes: 20 additions & 14 deletions
34
src/ExtendedXmlSerializer/ExtensionModel/References/ReferenceEncounters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IFormatWriter, IEncounters>, 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<object, Identifier> | ||
{ | ||
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())); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue454Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} | ||
} |