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

Set hint size for IPAddress string representation to 45 #21161

Merged
merged 2 commits into from
Jun 8, 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
11 changes: 9 additions & 2 deletions src/EFCore/Storage/ValueConversion/IPAddressToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class IPAddressToStringConverter : ValueConverter<IPAddress, string>
{
// IPv4-mapped IPv6 addresses can go up to 45 bytes, e.g. 0000:0000:0000:0000:0000:ffff:192.168.1.1
private static readonly ConverterMappingHints _defaultHints
roji marked this conversation as resolved.
Show resolved Hide resolved
= new ConverterMappingHints(size: 45);

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
Expand All @@ -24,15 +28,18 @@ public IPAddressToStringConverter([CanBeNull] ConverterMappingHints mappingHints
: base(
ToString(),
ToIPAddress(),
mappingHints)
_defaultHints.With(mappingHints))
{
}

/// <summary>
/// A <see cref="ValueConverterInfo" /> for the default use of this converter.
/// </summary>
public static ValueConverterInfo DefaultInfo { get; }
= new ValueConverterInfo(typeof(IPAddress), typeof(string), i => new IPAddressToStringConverter(i.MappingHints));
= new ValueConverterInfo(
typeof(IPAddress),
typeof(string), i => new IPAddressToStringConverter(i.MappingHints),
_defaultHints);

private static new Expression<Func<IPAddress, string>> ToString()
=> v => v == null ? default : v.ToString();
Expand Down
17 changes: 17 additions & 0 deletions test/EFCore.Tests/Storage/ValueConverterSelectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using Xunit;
Expand Down Expand Up @@ -697,6 +698,22 @@ public void Can_get_converters_for_Uri_to_string()
(typeof(UriToStringConverter), default));
}

[ConditionalFact]
public void Can_get_converters_for_IPAddress_to_string()
{
AssertConverters(
_selector.Select(typeof(IPAddress), typeof(string)).ToList(),
(typeof(IPAddressToStringConverter), new ConverterMappingHints(size: 45)));
}

[ConditionalFact]
public void Can_get_converters_for_IPAddress_to_bytes()
{
AssertConverters(
_selector.Select(typeof(IPAddress), typeof(byte[])).ToList(),
(typeof(IPAddressToBytesConverter), new ConverterMappingHints(size: 16)));
}

private static void AssertConverters(
IList<ValueConverterInfo> converterInfos,
params (Type InfoType, ConverterMappingHints Hints)[] converterTypes)
Expand Down