Skip to content

Commit

Permalink
[6.0] Mark internal the ValueConverter constructors that allow conver…
Browse files Browse the repository at this point in the history
…sion of nulls (#26232)
  • Loading branch information
ajcvickers authored Oct 9, 2021
1 parent 81c317a commit 938a8b0
Show file tree
Hide file tree
Showing 19 changed files with 35 additions and 55 deletions.
5 changes: 2 additions & 3 deletions src/EFCore/Storage/ValueConversion/BytesToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public BytesToStringConverter()
/// </param>
public BytesToStringConverter(ConverterMappingHints? mappingHints)
: base(
v => v == null ? null : Convert.ToBase64String(v),
v => v == null ? null : Convert.FromBase64String(v),
convertsNulls: true,
v => Convert.ToBase64String(v!),
v => Convert.FromBase64String(v!),
mappingHints)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public IPAddressToBytesConverter()
/// </param>
public IPAddressToBytesConverter(ConverterMappingHints? mappingHints)
: base(
v => v == null ? default : v.GetAddressBytes(),
v => v == null ? default : new IPAddress(v),
convertsNulls: true,
v => v!.GetAddressBytes(),
v => new IPAddress(v!),
_defaultHints.With(mappingHints))
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public IPAddressToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToIPAddress(),
convertsNulls: true,
_defaultHints.With(mappingHints))
{
}
Expand All @@ -52,9 +51,9 @@ public IPAddressToStringConverter(ConverterMappingHints? mappingHints)
= new(typeof(IPAddress), typeof(string), i => new IPAddressToStringConverter(i.MappingHints), _defaultHints);

private new static Expression<Func<IPAddress?, string?>> ToString()
=> v => v == null ? default : v.ToString();
=> v => v!.ToString();

private static Expression<Func<string?, IPAddress?>> ToIPAddress()
=> v => v == null ? default : IPAddress.Parse(v);
=> v => IPAddress.Parse(v!);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public class StringNumberConverter<TModel, TProvider, TNumber> : ValueConverter<
public StringNumberConverter(
Expression<Func<TModel, TProvider>> convertToProviderExpression,
Expression<Func<TProvider, TModel>> convertFromProviderExpression,
bool convertsNulls,
ConverterMappingHints? mappingHints = null)
: base(convertToProviderExpression, convertFromProviderExpression, convertsNulls, mappingHints)
: base(convertToProviderExpression, convertFromProviderExpression, mappingHints)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public StringUriConverter(
: base(
convertToProviderExpression,
convertFromProviderExpression,
convertsNulls: true,
mappingHints)
{
}
Expand All @@ -39,7 +38,7 @@ public StringUriConverter(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected new static Expression<Func<Uri?, string?>> ToString()
=> v => v != null ? v.ToString() : null;
=> v => v!.ToString();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -48,6 +47,6 @@ public StringUriConverter(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected static Expression<Func<string?, Uri?>> ToUri()
=> v => v != null ? new Uri(v, UriKind.RelativeOrAbsolute) : null;
=> v => new Uri(v!, UriKind.RelativeOrAbsolute);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public NumberToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToNumber(),
typeof(TNumber).IsNullableType(),
_defaultHints.With(mappingHints))
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public PhysicalAddressToBytesConverter()
/// </param>
public PhysicalAddressToBytesConverter(ConverterMappingHints? mappingHints)
: base(
v => v == null ? default : v.GetAddressBytes(),
v => v == null ? default : new PhysicalAddress(v),
convertsNulls: true,
v => v!.GetAddressBytes(),
v => new PhysicalAddress(v!),
_defaultHints.With(mappingHints))
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public PhysicalAddressToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToPhysicalAddress(),
convertsNulls: true,
_defaultHints.With(mappingHints))
{
}
Expand All @@ -54,9 +53,9 @@ public PhysicalAddressToStringConverter(ConverterMappingHints? mappingHints)
= new(typeof(PhysicalAddress), typeof(string), i => new PhysicalAddressToStringConverter(i.MappingHints), _defaultHints);

private new static Expression<Func<PhysicalAddress?, string?>> ToString()
=> v => v == null ? default! : v.ToString();
=> v => v!.ToString();

private static Expression<Func<string?, PhysicalAddress?>> ToPhysicalAddress()
=> v => v == null ? default! : PhysicalAddress.Parse(v);
=> v => PhysicalAddress.Parse(v!);
}
}
5 changes: 2 additions & 3 deletions src/EFCore/Storage/ValueConversion/StringToBytesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public StringToBytesConverter(
Encoding encoding,
ConverterMappingHints? mappingHints = null)
: base(
v => v == null ? null : encoding.GetBytes(v),
v => v == null ? null : encoding.GetString(v),
convertsNulls: true,
v => encoding.GetBytes(v!),
v => encoding.GetString(v!),
mappingHints)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public StringToNumberConverter(ConverterMappingHints? mappingHints)
: base(
ToNumber(),
ToString(),
typeof(TNumber).IsNullableType(),
_defaultHints.With(mappingHints))
{
}
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/ValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ protected ValueConverter(
}

/// <summary>
/// Initializes a new instance of the <see cref="ValueConverter" /> class.
/// <para>
/// Initializes a new instance of the <see cref="ValueConverter" /> class, allowing conversion of
/// nulls.
/// </para>
/// <para>
/// Warning: this is currently an internal API since converting nulls to and from the database can lead to broken
/// queries and other issues. See https://github.com/dotnet/efcore/issues/26230 for more information.
/// </para>
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information.
Expand All @@ -72,6 +79,7 @@ protected ValueConverter(
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
[EntityFrameworkInternal]
protected ValueConverter(
LambdaExpression convertToProviderExpression,
LambdaExpression convertFromProviderExpression,
Expand Down
11 changes: 10 additions & 1 deletion src/EFCore/Storage/ValueConversion/ValueConverter`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
Expand Down Expand Up @@ -40,7 +41,14 @@ public ValueConverter(
}

/// <summary>
/// Initializes a new instance of the <see cref="ValueConverter{TModel,TProvider}" /> class.
/// <para>
/// Initializes a new instance of the <see cref="ValueConverter{TModel,TProvider}" /> class, allowing conversion of
/// nulls.
/// </para>
/// <para>
/// Warning: this is currently an internal API since converting nulls to and from the database can lead to broken
/// queries and other issues. See https://github.com/dotnet/efcore/issues/26230 for more information.
/// </para>
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-value-converters">EF Core value converters</see> for more information.
Expand All @@ -55,6 +63,7 @@ public ValueConverter(
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
[EntityFrameworkInternal]
public ValueConverter(
Expression<Func<TModel, TProvider>> convertToProviderExpression,
Expression<Func<TProvider, TModel>> convertFromProviderExpression,
Expand Down
4 changes: 1 addition & 3 deletions test/EFCore.Tests/Storage/BytesToStringConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public class BytesToStringConverterTest
public void Can_convert_strings_to_bytes()
{
var converter = _bytesToStringConverter.ConvertToProviderExpression.Compile();
Assert.True(_bytesToStringConverter.ConvertsNulls);
Assert.False(_bytesToStringConverter.ConvertsNulls);

Assert.Equal("U3DEsW7MiGFsIFRhcA==", converter(new byte[] { 83, 112, 196, 177, 110, 204, 136, 97, 108, 32, 84, 97, 112 }));
Assert.Equal("", converter(Array.Empty<byte>()));
Assert.Null(converter(null));
}

[ConditionalFact]
Expand All @@ -29,7 +28,6 @@ public void Can_convert_bytes_to_strings()

Assert.Equal(new byte[] { 83, 112, 196, 177, 110, 204, 136, 97, 108, 32, 84, 97, 112 }, converter("U3DEsW7MiGFsIFRhcA=="));
Assert.Equal(Array.Empty<byte>(), converter(""));
Assert.Null(converter(null));
}

[ConditionalFact]
Expand Down
6 changes: 0 additions & 6 deletions test/EFCore.Tests/Storage/IPAddressToBytesConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public void Can_convert_ipaddress_ipv4_to_bytes(string ipv4)
var bytes = ip.GetAddressBytes();

Assert.Equal(bytes, converter(ip));

Assert.Null(converter(null));
}

[ConditionalTheory]
Expand Down Expand Up @@ -57,8 +55,6 @@ public void Can_convert_bytes_to_ipaddress_ipv4(byte[] bytesIPV4Invalid)

Assert.Throws<ArgumentException>(
() => converter(bytesIPV4Invalid));

Assert.Null(converter(null));
}

[ConditionalTheory]
Expand All @@ -75,8 +71,6 @@ public void Can_convert_bytes_to_ipaddress_ipv6(string ipv6)
var bytes = ip.GetAddressBytes();

Assert.Equal(ip, converter(bytes));

Assert.Null(converter(null));
}

[ConditionalTheory]
Expand Down
8 changes: 0 additions & 8 deletions test/EFCore.Tests/Storage/IPAddressToStringConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public void Can_convert_ipaddress_ipv4_to_String(string ipv4)
var converter = _ipAddressToString.ConvertToProviderExpression.Compile();

Assert.Equal(ipv4, converter(IPAddress.Parse(ipv4)));

Assert.Null(converter(null));
}

[ConditionalTheory]
Expand All @@ -38,8 +36,6 @@ public void Can_convert_ipaddress_ipv6_to_String(string ipv6)
var converter = _ipAddressToString.ConvertToProviderExpression.Compile();

Assert.Equal(ipv6, converter(IPAddress.Parse(ipv6)));

Assert.Null(converter(null));
}

[ConditionalTheory]
Expand All @@ -55,8 +51,6 @@ public void Can_convert_String_to_ipaddress_ipv4(string ipv4)
Assert.Equal(
IPAddress.Parse(ipv4),
converter(ipv4));

Assert.Null(converter(null));
}

[ConditionalTheory]
Expand All @@ -72,8 +66,6 @@ public void Can_convert_String_to_ipaddress_ipv6(string ipv6)
Assert.Equal(
IPAddress.Parse(ipv6),
converter(ipv6));

Assert.Null(converter(null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public void Can_convert_physical_address_to_bytes(string address)
var bytes = physicalAddress.GetAddressBytes();

Assert.Equal(bytes, converter(physicalAddress));
Assert.Null(converter(null));
}

[ConditionalTheory]
Expand All @@ -46,7 +45,6 @@ public void Can_convert_bytes_to_physical_address(string address)
var bytes = physicalAddress.GetAddressBytes();

Assert.Equal(physicalAddress, converter(bytes));
Assert.Null(converter(null));
}

[ConditionalTheory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public void Can_convert_physical_address_to_String(string physicalAddress)
Assert.Equal(
alphaNumerics.Replace(physicalAddress, ""),
converter(PhysicalAddress.Parse(physicalAddress)));

Assert.Null(converter(null));
}

[ConditionalTheory]
Expand All @@ -39,8 +37,6 @@ public void Can_convert_String_to_physical_address(string physicalAddress)
Assert.Equal(
PhysicalAddress.Parse(physicalAddress),
converter(physicalAddress));

Assert.Null(converter(null));
}

[ConditionalTheory]
Expand All @@ -59,7 +55,6 @@ public void Can_convert_bytes_to_physical_address(byte[] bytesPhysicalAddressInv
converter(physicalAddress);
});

Assert.Null(converter(null));
Assert.Equal($"An invalid physical address was specified: '{physicalAddress}'.", exception.Message);
}

Expand Down
2 changes: 0 additions & 2 deletions test/EFCore.Tests/Storage/StringToBytesConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public void Can_convert_strings_to_UTF8()

Assert.Equal(new byte[] { 83, 112, 196, 177, 110, 204, 136, 97, 108, 32, 84, 97, 112 }, converter("Spın̈al Tap"));
Assert.Equal(Array.Empty<byte>(), converter(""));
Assert.Null(converter(null));
}

[ConditionalFact]
Expand All @@ -29,7 +28,6 @@ public void Can_convert_UTF8_to_strings()

Assert.Equal("Spın̈al Tap", converter(new byte[] { 83, 112, 196, 177, 110, 204, 136, 97, 108, 32, 84, 97, 112 }));
Assert.Equal("", converter(Array.Empty<byte>()));
Assert.Null(converter(null));
}

[ConditionalFact]
Expand Down
2 changes: 0 additions & 2 deletions test/EFCore.Tests/Storage/StringToUriConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public void Can_convert_strings_to_uris()
Assert.Equal(new Uri("ftp://www.github.com", UriKind.Absolute), converter("ftp://www.github.com/"));
Assert.Equal(new Uri(".", UriKind.Relative), converter("."));

Assert.Null(converter(null));
Assert.Throws<UriFormatException>(() => converter("http:///"));
}

Expand All @@ -48,7 +47,6 @@ public void Can_convert_uris_to_strings()
Assert.Equal("/relative/path", converter(new Uri("/relative/path", UriKind.Relative)));
Assert.Equal("ftp://www.github.com/", converter(new Uri("ftp://www.github.com/", UriKind.Absolute)));
Assert.Equal(".", converter(new Uri(".", UriKind.Relative)));
Assert.Null(converter(null));
}

[ConditionalFact]
Expand Down

0 comments on commit 938a8b0

Please sign in to comment.