Skip to content
Closed
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
49 changes: 49 additions & 0 deletions src/Components/Components/src/BindConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,47 @@ private static bool ConvertToNullableTimeOnlyCore(object? obj, CultureInfo? cult
return false;
}

internal static readonly BindParser<Guid> ConvertToGuid = ConvertToGuidCore;
internal static readonly BindParser<Guid?> ConvertToNullableGuid = ConvertToNullableGuidCore;

private static bool ConvertToGuidCore(object? obj, CultureInfo? culture, out Guid value)
{
var text = (string?)obj;
if (string.IsNullOrEmpty(text))
{
value = default;
return false;
}

if (!Guid.TryParse(text, out var converted))
{
value = default;
return false;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must be missing something here. How come you don't need a try/catch any more? I thought that adding the try/catch was the main reason for the change.

Does structuring the code this way somehow mean there isn't an unhandled exception any more? If you could clarify that would be great!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because before the GuidConverter would call the ctor which throws on invalid values. #33587 (comment)
You can see it in the stack trace from here #33587 (comment)


Should there be another case for Guid??

@jeffl8n jeffl8n Jun 18, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. Calling Guid.TryParse sets the ThrowStyle to None - https://github.com/dotnet/runtime/blob/5221db92ed579f8ffa56f1d8e84f07ba6d896077/src/libraries/System.Private.CoreLib/src/System/Guid.cs#L242
But calling the Guid ctor sets the ThrowStyle to All - https://github.com/dotnet/runtime/blob/5221db92ed579f8ffa56f1d8e84f07ba6d896077/src/libraries/System.Private.CoreLib/src/System/Guid.cs#L210

Should there be another case for Guid??

Do you mean like the check for null/empty string? The Guid class performs several checks for length and formatting, so that current check could be removed as well, but I left it since it would prevent having to call TryParse for the simplest invalid case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant for the typeof(Guid?) type, all the others do both checks like typeof(DateTimeOffset) and typeof(DateTimeOffset?):

else if (typeof(T) == typeof(DateTimeOffset))
{
parser = ConvertToDateTime;
}
else if (typeof(T) == typeof(DateTimeOffset?))
{
parser = ConvertToNullableDateTime;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I've just added another case for Nullable Guid.


value = converted;
return true;
}

private static bool ConvertToNullableGuidCore(object? obj, CultureInfo? culture, out Guid? value)
{
var text = (string?)obj;
if (string.IsNullOrEmpty(text))
{
value = default;
return true;
}

if (!Guid.TryParse(text, out var converted))
{
value = default;
return false;
}

value = converted;
return true;
}

private static bool ConvertToEnum<T>(object? obj, CultureInfo? culture, out T value) where T : struct, Enum
{
var text = (string?)obj;
Expand Down Expand Up @@ -1934,6 +1975,14 @@ internal static class ParserDelegateCache
{
parser = ConvertToNullableTimeOnly;
}
else if (typeof(T) == typeof(Guid))
{
parser = ConvertToGuid;
}
else if (typeof(T) == typeof(Guid?))
{
parser = ConvertToNullableGuid;
}
else if (typeof(T).IsEnum)
{
// We have to deal invoke this dynamically to work around the type constraint on Enum.TryParse.
Expand Down
71 changes: 71 additions & 0 deletions src/Components/Components/test/BindConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,77 @@ public void FormatValue_TypeConverter()
Assert.Equal(expected, actual);
}

[Fact]
public void TryConvertTo_Guid_Valid()
{
// Arrange
var expected = Guid.NewGuid();
var value = expected.ToString();

// Act
var successfullyConverted = BindConverter.TryConvertTo<Guid>(value, System.Globalization.CultureInfo.CurrentCulture, out var actual);

// Assert
Assert.True(successfullyConverted);
Assert.Equal(expected, actual);
}

[Fact]
public void TryConvertTo_Guid_Invalid()
{
// Arrange
var value = "invalidguid";

// Act
var successfullyConverted = BindConverter.TryConvertTo<Guid>(value, System.Globalization.CultureInfo.CurrentCulture, out var actual);

// Assert
Assert.False(successfullyConverted);
}

[Fact]
public void TryConvertTo_NullableGuid_Valid()
{
// Arrange
var expected = Guid.NewGuid();
var value = expected.ToString();

// Act
var successfullyConverted = BindConverter.TryConvertTo<Guid?>(value, System.Globalization.CultureInfo.CurrentCulture, out var actual);

// Assert
Assert.True(successfullyConverted);
Assert.Equal(expected, actual.Value);
}

[Fact]
public void TryConvertTo_NullableGuid_ValidNull()
{
// Arrange
var expected = (Guid?) null;
var value = (Guid?) null;

// Act
var successfullyConverted = BindConverter.TryConvertTo<Guid?>(value, System.Globalization.CultureInfo.CurrentCulture, out var actual);

// Assert
Assert.True(successfullyConverted);
Assert.Equal(expected, actual);
}

[Fact]
public void TryConvertTo_NullableGuid__Invalid()
{
// Arrange
var value = "invalidguid";

// Act
var successfullyConverted = BindConverter.TryConvertTo<Guid?>(value, System.Globalization.CultureInfo.CurrentCulture, out var actual);

// Assert
Assert.False(successfullyConverted);
}

private enum SomeLetters
{
A,
Expand Down