Skip to content

Commit

Permalink
Support value conversion for ReadOnlyIPAddress
Browse files Browse the repository at this point in the history
Fixes #21159
  • Loading branch information
roji committed Aug 19, 2021
1 parent 0ba1695 commit 8904d6a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/EFCore/Storage/ValueConversion/ValueConverterSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class ValueConverterSelector : IValueConverterSelector
typeof(float)
};

private static readonly Type? _readOnlyIPAddressType = IPAddress.Loopback.GetType();

/// <summary>
/// Initializes a new instance of the <see cref="ValueConverterSelector" /> class.
/// </summary>
Expand Down Expand Up @@ -291,7 +293,7 @@ public virtual IEnumerable<ValueConverterInfo> Select(
NumberToBytesConverter<long>.DefaultInfo.MappingHints));
}
}
else if (modelClrType == typeof(IPAddress))
else if (modelClrType == typeof(IPAddress) || modelClrType == _readOnlyIPAddressType)
{
if (providerClrType == null
|| providerClrType == typeof(string))
Expand Down
8 changes: 8 additions & 0 deletions test/EFCore.Tests/Storage/ValueConverterSelectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,10 @@ public void Can_get_converters_for_IPAddress_to_string()
AssertConverters(
_selector.Select(typeof(IPAddress), typeof(string)).ToList(),
(typeof(IPAddressToStringConverter), new ConverterMappingHints(size: 45)));

AssertConverters(
_selector.Select(IPAddress.Loopback.GetType(), typeof(string)).ToList(),
(typeof(IPAddressToStringConverter), new ConverterMappingHints(size: 45)));
}

[ConditionalFact]
Expand All @@ -712,6 +716,10 @@ public void Can_get_converters_for_IPAddress_to_bytes()
AssertConverters(
_selector.Select(typeof(IPAddress), typeof(byte[])).ToList(),
(typeof(IPAddressToBytesConverter), new ConverterMappingHints(size: 16)));

AssertConverters(
_selector.Select(IPAddress.Loopback.GetType(), typeof(byte[])).ToList(),
(typeof(IPAddressToBytesConverter), new ConverterMappingHints(size: 16)));
}

private static void AssertConverters(
Expand Down

0 comments on commit 8904d6a

Please sign in to comment.