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 support for nullable structure types. #478

Merged
merged 2 commits into from
Nov 23, 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
24 changes: 0 additions & 24 deletions src/ExtendedXmlSerializer/ContentModel/Content/NullableContents.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ bool IsMember(IMember profile)
IMemberSerializer Content(IMember profile, IMemberAccess access)
{
var identity = new Identity<object>(profile);
var isMember = IsMember(profile);
var composite = isMember
var composite = IsMember(profile)
? (IWriter<object>)new MemberPropertyWriter(identity)
: identity;
var start = composite.Adapt();
Expand Down
3 changes: 0 additions & 3 deletions src/ExtendedXmlSerializer/ExtensionModel/Content/Contents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public IServiceRepository Get(IServiceRepository parameter)
.DecorateContentsWith<ReflectionContents>()
.When(ReflectionContentSpecification.Default)

.DecorateContentsWith<NullableContents>()
.When(IsNullableTypeSpecification.Default)

.DecorateContentsWith<ConverterContents>()
.When<ConverterSpecification>()
.DecorateContentsWith<RegisteredContents>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ sealed class ParameterizedResultHandler : IInnerContentResult
{
readonly IInnerContentResult _result;

public ParameterizedResultHandler(IInnerContentResult result)
{
_result = result;
}
public ParameterizedResultHandler(IInnerContentResult result) => _result = result;

public object Get(IInnerContent parameter)
=> (parameter.Current as IActivationContext)?.Get() ?? _result.Get(parameter);
=> parameter.Current is IActivationContext context ? context.Get() : _result.Get(parameter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public override IEnumerator<ISerializerExtension> GetEnumerator()
yield return new MemberFormatExtension();
yield return ImmutableArrayExtension.Default;
yield return SerializationExtension.Default;
yield return NullableStructureAwareExtension.Default;
yield return new CustomSerializationExtension();
yield return CachingExtension.Default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public IServiceRepository Get(IServiceRepository parameter)
.Register<RuntimeSerializers>()
.Register<ISerializers, Serializers>()
.RegisterInstance(RuntimeSerializationExceptionMessage.Default)
.Decorate<ISerializers, NullableAwareSerializers>()
.Decorate<ISerializers, ReferenceAwareSerializers>()
.Decorate<ISerializers, DynamicAwareSerializers>()
.Decorate<IContents, RecursionAwareContents>();
Expand Down
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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;
using ExtendedXmlSerializer.ContentModel.Conversion;
using ExtendedXmlSerializer.ContentModel.Members;
using ExtendedXmlSerializer.Core;
using ExtendedXmlSerializer.Core.Specifications;
using System.Reflection;

namespace ExtendedXmlSerializer.ExtensionModel.Xml
{
Expand Down Expand Up @@ -48,11 +48,7 @@ public MemberConverters(IMemberConverters members, IConverters converters)
_converters = converters;
}

public IConverter Get(MemberInfo parameter)
{
var converter = _members.Get(parameter);
return converter ?? From(parameter);
}
public IConverter Get(MemberInfo parameter) => _members.Get(parameter) ?? From(parameter);

IConverter From(MemberDescriptor parameter) => _converters.Get(parameter.MemberType.AccountForNullable());
}
Expand Down

This file was deleted.

92 changes: 92 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue477Tests.cs
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
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
}
}
}
Loading