Skip to content

Fix exception on parse failure for unsigned types in BindConverter#67091

Open
irfanajaffer wants to merge 7 commits into
dotnet:mainfrom
irfanajaffer:1031580_BindConverter
Open

Fix exception on parse failure for unsigned types in BindConverter#67091
irfanajaffer wants to merge 7 commits into
dotnet:mainfrom
irfanajaffer:1031580_BindConverter

Conversation

@irfanajaffer

Copy link
Copy Markdown

Description:

When attempting to convert negative values to unsigned integer types (uint, ushort, ulong, byte), the TypeConverter throws an ArgumentException. While the BindConverter correctly catches this exception and returns false (as expected behavior), the test suite lacked comprehensive coverage to verify this behavior across all unsigned types and edge cases

Root Cause

The BindConverter uses TypeDescriptor/TypeConverter for unsigned integer types (uint, ushort, ulong, byte, sbyte) since they don't have explicit handling in the ParserDelegateCache. When invalid values (like negative numbers for unsigned types) are passed, the TypeConverter throws ArgumentException, which is properly caught and handled by returning false.

Testing

Added comprehensive unit tests in BindConverterTest.cs to validate handling of unsigned integer types across all key scenarios. The tests ensure:

✅ Valid conversions succeed as expected
✅ Negative inputs return false without throwing (verifies ArgumentException handling)
✅ Out-of-range values return false (verifies OverflowException handling)
✅ Null and empty string inputs are handled gracefully
✅ Nullable unsigned type conversions behave correctly
✅ Boundary values (MinValue/MaxValue) are processed accurately

Fixes #42435

@irfanajaffer irfanajaffer requested a review from a team as a code owner June 9, 2026 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BindConverter throws exception on parse failure

1 participant