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 20, 2021
1 parent 0ba1695 commit 4c7c638
Show file tree
Hide file tree
Showing 6 changed files with 38 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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public override Task Projecting_entity_as_well_as_correlated_collection_of_scala
public override Task Correlated_collection_with_distinct_3_levels(bool async)
=> base.Correlated_collection_with_distinct_3_levels(async);

[ConditionalTheory(Skip = "Issue #25615")]
public override Task Comparison_with_value_converted_subclass(bool async)
=> base.Comparison_with_value_converted_subclass(async);

public override async Task Projecting_correlated_collection_followed_by_Distinct(bool async)
{
var message = (await Assert.ThrowsAsync<InvalidOperationException>(
Expand Down
10 changes: 10 additions & 0 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
Expand Down Expand Up @@ -8997,6 +8998,15 @@ public virtual Task Include_on_entity_that_is_not_present_in_final_projection_bu
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Comparison_with_value_converted_subclass(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Faction>().Where(f => f.ServerAddress == IPAddress.Loopback));
}

protected GearsOfWarContext CreateContext()
=> Fixture.CreateContext();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net;

namespace Microsoft.EntityFrameworkCore.TestModels.GearsOfWarModel
{
public abstract class Faction
{
public int Id { get; set; }
public string Name { get; set; }
public IPAddress ServerAddress { get; set; }

public string CapitalName { get; set; }
public City Capital { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8154,6 +8154,16 @@ END AS [IsOfficer]
FROM [Gears] AS [g]");
}

public override async Task Comparison_with_value_converted_subclass(bool async)
{
await base.Comparison_with_value_converted_subclass(async);

AssertSql(
@"SELECT [f].[Id], [f].[CapitalName], [f].[Discriminator], [f].[Name], [f].[ServerAddress], [f].[CommanderName], [f].[Eradicated]
FROM [Factions] AS [f]
WHERE [f].[ServerAddress] = CAST(N'127.0.0.1' AS nvarchar(45))");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
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 4c7c638

Please sign in to comment.