-
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
12 changed files
with
249 additions
and
88 deletions.
There are no files selected for viewing
24 changes: 0 additions & 24 deletions
24
src/ExtendedXmlSerializer/ContentModel/Content/NullableContents.cs
This file was deleted.
Oops, something went wrong.
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
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
33 changes: 33 additions & 0 deletions
33
src/ExtendedXmlSerializer/ExtensionModel/Types/NullableStructureAwareExtension.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,33 @@ | ||
using ExtendedXmlSerializer.ContentModel; | ||
using ExtendedXmlSerializer.ContentModel.Content; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.ExtensionModel.Types | ||
{ | ||
sealed class NullableStructureAwareExtension : ISerializerExtension | ||
{ | ||
public static NullableStructureAwareExtension Default { get; } = new NullableStructureAwareExtension(); | ||
|
||
NullableStructureAwareExtension() {} | ||
|
||
public IServiceRepository Get(IServiceRepository parameter) | ||
=> parameter.DecorateContentsWith<NullableStructureAwareContents>().Then(); | ||
|
||
public void Execute(IServices parameter) {} | ||
|
||
sealed class NullableStructureAwareContents : IContents | ||
{ | ||
readonly IContents _previous; | ||
|
||
public NullableStructureAwareContents(IContents previous) => _previous = previous; | ||
|
||
public ISerializer Get(TypeInfo parameter) | ||
{ | ||
var underlying = Nullable.GetUnderlyingType(parameter); | ||
var serializer = _previous.Get(underlying != null ? underlying : parameter); | ||
return serializer; | ||
} | ||
} | ||
} | ||
} |
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
12 changes: 0 additions & 12 deletions
12
src/ExtendedXmlSerializer/ReflectionModel/IsNullableTypeSpecification.cs
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue477Tests.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,92 @@ | ||
using ExtendedXmlSerializer.Configuration; | ||
using ExtendedXmlSerializer.Tests.ReportedIssues.Support; | ||
using FluentAssertions; | ||
using System; | ||
using Xunit; | ||
|
||
#nullable enable | ||
namespace ExtendedXmlSerializer.Tests.ReportedIssues | ||
{ | ||
public sealed class Issue477Tests | ||
{ | ||
[Fact] | ||
public void SerializeAndDeserializeInstanceA() | ||
{ | ||
var serializer = new ConfigurationContainer().EnableParameterizedContentWithPropertyAssignments() | ||
.Create() | ||
.ForTesting(); | ||
|
||
var instance = new A { Point = new Point(2, 3) }; | ||
|
||
serializer.Cycle(instance).Should().BeEquivalentTo(instance); | ||
} | ||
|
||
[Fact] | ||
public void SerializeAndDeserializeInstanceB() | ||
{ | ||
var serializer = new ConfigurationContainer().EnableParameterizedContent() | ||
.Create() | ||
.ForTesting(); | ||
|
||
var instance = new B { NullablePoint = new Point(24, 7) }; | ||
|
||
serializer.Cycle(instance).Should().BeEquivalentTo(instance); | ||
} | ||
|
||
[Fact] | ||
public void SerializeAndDeserializeInstanceBNull() | ||
{ | ||
var serializer = new ConfigurationContainer().EnableParameterizedContent() | ||
.Create() | ||
.ForTesting(); | ||
|
||
var instance = new B { NullablePoint = null }; | ||
|
||
serializer.Assert(instance, @"<?xml version=""1.0"" encoding=""utf-8""?><Issue477Tests-B xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ReportedIssues;assembly=ExtendedXmlSerializer.Tests.ReportedIssues"" />"); | ||
|
||
serializer.Cycle(instance).Should().BeEquivalentTo(instance); | ||
} | ||
|
||
public sealed class A | ||
{ | ||
public Point Point { get; set; } | ||
} | ||
|
||
public sealed class B | ||
{ | ||
public Point? NullablePoint { get; set; } | ||
} | ||
|
||
public readonly struct Point : IEquatable<Point> | ||
{ | ||
public Point(int x, int y) | ||
{ | ||
X = x; | ||
Y = y; | ||
} | ||
|
||
public int X { get; } | ||
|
||
public int Y { get; } | ||
|
||
public bool Equals(Point other) => X == other.X && Y == other.Y; | ||
|
||
public override bool Equals(object? obj) => obj is Point other && Equals(other); | ||
|
||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
return (X * 397) ^ Y; | ||
} | ||
} | ||
|
||
public override string ToString() => $"({X}, {Y})"; | ||
|
||
public static bool operator ==(Point left, Point right) => left.Equals(right); | ||
|
||
public static bool operator !=(Point left, Point right) => !left.Equals(right); | ||
} | ||
} | ||
} | ||
#nullable restore |
41 changes: 41 additions & 0 deletions
41
test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue477Tests_Extended.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,41 @@ | ||
using ExtendedXmlSerializer.Configuration; | ||
using ExtendedXmlSerializer.Tests.ReportedIssues.Support; | ||
using FluentAssertions; | ||
using System; | ||
using Xunit; | ||
|
||
namespace ExtendedXmlSerializer.Tests.ReportedIssues | ||
{ | ||
public sealed class Issue477Tests_Extended | ||
{ | ||
[Fact] | ||
public void Verify() | ||
{ | ||
var serializer = new ConfigurationContainer().Type<DateTime>() | ||
.Register() | ||
.Serializer() | ||
.ByCalling((writer, date) => writer.Content(date.ToString("yyyy-MM-dd")), | ||
null) | ||
.Type<DateTime?>() | ||
.Register() | ||
.Serializer() | ||
.ByCalling((writer, date) => writer.Content(date?.ToString("yyyy-MM-dd") ?? string.Empty), null) | ||
.Create() | ||
.ForTesting(); | ||
|
||
var instance = new Test(); | ||
var content = serializer.Serialize(instance); | ||
|
||
content.Should().Be(@"<?xml version=""1.0"" encoding=""utf-8""?><Issue477Tests_Extended-Test xmlns=""clr-namespace:ExtendedXmlSerializer.Tests.ReportedIssues;assembly=ExtendedXmlSerializer.Tests.ReportedIssues""><Date1>2020-11-23</Date1><Date3>2020-11-23</Date3></Issue477Tests_Extended-Test>"); | ||
|
||
|
||
} | ||
|
||
class Test | ||
{ | ||
public DateTime Date1 { get; set; } = DateTime.Now; // Formatted OK | ||
public DateTime? Date2 { get; set; } = null; // Is not emitted = OK | ||
public DateTime? Date3 { get; set; } = DateTime.Now; // Completety ignores configured formatting | ||
} | ||
} | ||
} |
Oops, something went wrong.