-
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
1 parent
c1f19d6
commit 1adeafd
Showing
11 changed files
with
150 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
45 changes: 45 additions & 0 deletions
45
src/ExtendedXmlSerializer/ExtensionModel/GeneratedListAwareExtension.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,45 @@ | ||
using ExtendedXmlSerializer.ContentModel.Conversion; | ||
using ExtendedXmlSerializer.ContentModel.Reflection; | ||
using ExtendedXmlSerializer.Core.Sources; | ||
using ExtendedXmlSerializer.Core.Specifications; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.ExtensionModel; | ||
|
||
sealed class GeneratedListAwareExtension : ISerializerExtension | ||
{ | ||
public static GeneratedListAwareExtension Default { get; } = new(); | ||
|
||
GeneratedListAwareExtension() {} | ||
|
||
public IServiceRepository Get(IServiceRepository parameter) | ||
=> parameter.Decorate<ITypePartResolver, TypePartResolver>(); | ||
|
||
public void Execute(IServices parameter) {} | ||
|
||
sealed class TypePartResolver : ITypePartResolver | ||
{ | ||
readonly ITypePartResolver _previous; | ||
readonly ISpecification<TypeInfo> _specification; | ||
readonly IAlteration<Type> _alter; | ||
|
||
public TypePartResolver(ITypePartResolver previous) | ||
: this(previous, IsGeneratedList.Default, GeneratedSubstitute.Default) {} | ||
|
||
public TypePartResolver(ITypePartResolver previous, ISpecification<TypeInfo> specification, | ||
IAlteration<Type> alter) | ||
{ | ||
_previous = previous; | ||
_specification = specification; | ||
_alter = alter; | ||
} | ||
|
||
public TypeParts Get(TypeInfo parameter) | ||
{ | ||
var type = _specification.IsSatisfiedBy(parameter) ? _alter.Get(parameter) : parameter; | ||
var result = _previous.Get(type); | ||
return result; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/ExtendedXmlSerializer/ExtensionModel/GeneratedSubstitute.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,29 @@ | ||
using ExtendedXmlSerializer.Core.Sources; | ||
using ExtendedXmlSerializer.ReflectionModel; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ExtendedXmlSerializer.ExtensionModel; | ||
|
||
sealed class GeneratedSubstitute : IAlteration<Type> | ||
{ | ||
public static GeneratedSubstitute Default { get; } = new(); | ||
|
||
GeneratedSubstitute() : this(typeof(List<>), CollectionItemTypeLocator.Default) {} | ||
|
||
readonly Type _definition; | ||
readonly ICollectionItemTypeLocator _locator; | ||
|
||
public GeneratedSubstitute(Type definition, ICollectionItemTypeLocator locator) | ||
{ | ||
_definition = definition; | ||
_locator = locator; | ||
} | ||
|
||
public Type Get(Type parameter) | ||
{ | ||
var arguments = _locator.Get(parameter); | ||
var result = _definition.MakeGenericType(arguments); | ||
return result; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/ExtendedXmlSerializer/ExtensionModel/IsGeneratedList.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,14 @@ | ||
using ExtendedXmlSerializer.Core.Specifications; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.ExtensionModel; | ||
|
||
sealed class IsGeneratedList : DelegatedSpecification<TypeInfo> | ||
{ | ||
public static IsGeneratedList Default { get; } = new(); | ||
|
||
IsGeneratedList() | ||
: base(x => x.FullName != null && | ||
(x.FullName.StartsWith("<>z__ReadOnlySingleElementList") || | ||
x.FullName.StartsWith("<>z__ReadOnlyArray"))) {} | ||
} |
13 changes: 3 additions & 10 deletions
13
src/ExtendedXmlSerializer/ReflectionModel/CollectionAwareConstructorLocator.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,21 +1,14 @@ | ||
using ExtendedXmlSerializer.Core.Specifications; | ||
using System.Collections.Generic; | ||
using ExtendedXmlSerializer.ExtensionModel; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.ReflectionModel | ||
{ | ||
sealed class CollectionAwareConstructorLocator : ListConstructorLocator, IConstructorLocator | ||
{ | ||
readonly static ISpecification<TypeInfo> Specification = IsInterface.Default.And(IsCollectionType.Instance); | ||
readonly static ISpecification<TypeInfo> Specification | ||
= IsInterface.Default.Or(IsGeneratedList.Default).And(IsCollectionTypeExpandedSpecification.Default); | ||
|
||
public CollectionAwareConstructorLocator(IConstructorLocator previous) : base(Specification, previous) {} | ||
|
||
sealed class IsCollectionType : AnySpecification<TypeInfo> | ||
{ | ||
public static IsCollectionType Instance { get; } = new IsCollectionType(); | ||
|
||
IsCollectionType() : base(IsCollectionTypeSpecification.Default, | ||
new IsAssignableGenericSpecification(typeof(IReadOnlyCollection<>))) {} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/ExtendedXmlSerializer/ReflectionModel/IsCollectionTypeExpandedSpecification.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,15 @@ | ||
using ExtendedXmlSerializer.Core.Specifications; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.ReflectionModel; | ||
|
||
sealed class IsCollectionTypeExpandedSpecification : AnySpecification<TypeInfo> | ||
{ | ||
public static IsCollectionTypeExpandedSpecification Default { get; } = new(); | ||
|
||
IsCollectionTypeExpandedSpecification() | ||
: base(IsCollectionTypeSpecification.Default, | ||
new IsAssignableGenericSpecification(typeof(IReadOnlyList<>)), | ||
new IsAssignableGenericSpecification(typeof(IReadOnlyCollection<>))) {} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue637Tests.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 @@ | ||
#if CORE | ||
using ExtendedXmlSerializer.Configuration; | ||
using ExtendedXmlSerializer.Tests.ReportedIssues.Support; | ||
using FluentAssertions; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace ExtendedXmlSerializer.Tests.ReportedIssues | ||
{ | ||
public sealed class Issue637Tests | ||
{ | ||
[Fact] | ||
public void Verify() | ||
{ | ||
var sut = new ConfigurationContainer().Create().ForTesting(); | ||
var instance = new Class2(); | ||
sut.Cycle(instance).Should().BeEquivalentTo(instance); | ||
} | ||
|
||
[Fact] | ||
public void VerifyModified() | ||
{ | ||
var sut = new ConfigurationContainer().Create().ForTesting(); | ||
var instance = new Class2 { List2 = [23]}; | ||
sut.Cycle(instance).Should().BeEquivalentTo(instance); | ||
} | ||
|
||
sealed class Class2 | ||
{ | ||
public IReadOnlyList<int> List1 { get; set; } = []; //this works | ||
public IReadOnlyList<int> List2 { get; set; } = [1]; //this breaks Serialize&Deserialize | ||
public IList<int> List3 { get; set; } = [1,2]; //this works | ||
public IReadOnlyList<int> List4 { get; set; } = new List<int>(){1}; //this works (workaround) | ||
} | ||
|
||
} | ||
} | ||
#endif |