Repository navigation
Fix TryConvertTo<T> throwing exceptions in the TypeConverter fallback path - #67091
Conversation
|
Let me add a bit more context. The docs confirm the
|
There was a problem hiding this comment.
Pull request overview
This PR fixes BindConverter.TryConvertTo<T> when conversion relies on TypeConverter, ensuring parse failures for unsigned numeric types (and similar converters) return false instead of throwing (notably covering ArgumentException from unsigned conversions). It also expands unit test coverage to validate the corrected behavior across several unsigned/edge-case scenarios.
Changes:
- Update the type-converter-based conversion path to catch
ArgumentException(andJsonException) and returnfalseon parse failure. - Adjust null-input handling for type-converter conversions so non-nullable value types return
false. - Add extensive unit tests for unsigned types (
uint,ushort,ulong,byte) andsbyte, including nullable variants and boundary/invalid inputs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/Components/Components/src/BindConverter.cs | Extends the TypeConverter fallback to treat more conversion failures as non-throwing false results and tightens null handling. |
| src/Components/Components/test/BindConverterTest.cs | Adds new test coverage for unsigned numeric conversions (and sbyte) including invalid/negative/overflow/null/empty/boundary cases. |
|
/review |
There was a problem hiding this comment.
🟡 Changes recommended
The test helper replaces failed conversions’ out values with defaults, masking incorrect converter results and making the unsigned failure-case assertions ineffective.
1 open issue
5 resolved since last review
Similarly,byte?has negative/empty/invalid-string coverage but no overflow case. Adding an… For nullable unsigned types, there are overflow tests foruint?but not forushort?orbyte?.… Nullableulongtests cover negative/empty/invalid-string, but there's no out-of-range test (e.g.,… The new unsigned coverage includes overflow tests for several types, butulongis missing an… The null-input path does a per-call reflection check (typeof(T)/Nullable.GetUnderlyingType) to…
Review effort: Balanced
| Assert.Null(actual); | ||
| } | ||
|
|
||
| private static bool TryConvertUnsigned(string typeKey, string incomingValue, bool isNullable, out object actual) |
There was a problem hiding this comment.
Source-only review of frozen head 31b7ae245a11b501c86bbe23bcb709fccccc7d20 found one unresolved P2 candidate: the repository proves that unsigned input reaches the new TypeConverter exception filter and that an unmatched exception would violate the TryConvertTo contract, but the frozen evidence does not include the target BCL converter implementation needed to establish its exact exception type.
Both routed guides completed with gpt-5.6-sol: CrossCuttingGuidance found no concrete candidate; BlazorComponentsGuidance raised the external-runtime candidate published inline after independent verification downgraded it as unresolved. Existing feedback contained no equivalent current finding requiring deduplication; older null-handling feedback was addressed in the frozen head.
Provenance: manifest v2 bundle; merge base c7fc0077f01b1069028af41b1dd90ddc090af63f; base tip and immutable guidance commit 7c704fed602c54d174e5674aee7b2eefe57073c9. Tests, target code, CI, browser workflows, and samples were not executed under the hosted source-only boundary. Runtime behavior of the external BCL converter remains uncovered; security assessment was explicitly excluded. Tracked Git bytes only; symlink, submodule, and LFS targets cannot establish behavior.
Generated by ASP.NET Core Pull Request Review for #67091 · copilot · gpt56 · 228.4 AIC · ⌖ 8.39 AIC · ⊞ 20.1K
Comment /review to run again
|
|
||
| value = (T)converted; | ||
| return true; | ||
| catch (Exception ex) when (ex is NotSupportedException || ex is FormatException || ex is OverflowException || ex is ArgumentException || ex is JsonException) |
There was a problem hiding this comment.
[P2][UNRESOLVED] Invalid unsigned input may still escape this filter
TryConvertTo<uint>("-1", CultureInfo.InvariantCulture, out _) reaches this TypeConverter fallback. If the target BCL numeric converter wraps the parse failure in a plain Exception, it is not matched here, so TryConvertTo throws instead of returning false and form consumers cannot report their normal validation error.
Why unresolved: The frozen repository establishes the routing, filter, API contract, and conditional consumer effect, but it does not include target-version BCL source or immutable execution evidence proving the converter selected for uint and its exact exception type.
Minimal repro: BindConverter.TryConvertTo<uint>("-1", CultureInfo.InvariantCulture, out _).
Suggested fix if confirmed: add the converter's exact wrapper exception to the filter, or route these numeric types through dedicated TryParse-based parsers rather than catching arbitrary exceptions.
|
Copilot's local review panel found no issues to raise at this commit. The three independent local arms reviewed Informational discarded checks (non-blocking):
Posted for informational purposes so the author can confirm the verifier didn't discard or leave unresolved a real finding; this item does not block Untested coverage gaps: Other runtime versions, browser workflows, publishing/trimming, and performance were not exercised. These limits are not blocking findings. Validation boundary: G and S performed source review. O ran scoped managed Human-directed and GitHub Copilot-generated. |
…o 1031580_BindConverter
…er/aspnetcore into 1031580_BindConverter
|
✅ No documentation PR was created. Confidence that documentation is needed: 10% No docs needed: confident bug fix in BindConverter.cs with tests; signals showed no_signal. Documentation surfaces considered
|


Description:
BindConverter.TryConvertTo<T>()violates theTry*contract for anyTwhose conversion falls through to theTypeConverterpath: on parse failure theTypeConverter.ConvertFromexception escapes the call instead of being converted tofalse. This affects unsigned integer types (uint,ushort,ulong,byte),sbyte,char, and any user-defined or BCL type that is resolved via aTypeConverter(for example[TypeConverter]-attributed custom types).This PR adds a
try/catcharound theTypeConverter.ConvertFromcall inMakeTypeConverterConverter<T>()so that any of the documented conversion-failure exceptions (NotSupportedException,FormatException,OverflowException,ArgumentException,JsonException) are converted to afalsereturn, matching the behavior of the explicitTryParse-based parsers for the other primitive types and aligning with theTry*contract.The narrow null-handling tweak that was originally bundled in this change has been removed — it is unrelated to the exception-escape bug and would have been a stealth behavior change for any non-primitive
Trouted through the fallback. Any future change to the null/empty contract for non-nullable value types should be a separate, intentionally scoped change.Root Cause
ParserDelegateCache.Get<T>()only branches on the primitive types that have a dedicatedTryParse-based parser (string,bool,int,long,short,float,double,decimal,DateTime/DateTimeOffset/DateOnly/TimeOnlyand their nullable variants,Guid, enums, and arrays). Every otherT— including all unsigned integer types,sbyte,char, and any customTypeConverter-attributed type — falls through toMakeTypeConverterConverter<T>(), which callstypeConverter.ConvertFrom(...)without atry/catch. For invalid input (for example,"-42"parsed asuint,"4294967296"asuint,"AB"aschar, or any input that a customTypeConverterrejects), the exception propagates out ofTryConvertTo<T>, breaking theTry*contract.The same issue was previously fixed for
Guidin #39649; this PR extends the same pattern to the generic fallback path so that any exception thrown byTypeConverter.ConvertFromis converted tofalse.Testing
Added comprehensive unit tests in BindConverterTest.cs to validate handling of unsigned integer types across all key scenarios. The tests ensure:
✅ Valid unsigned integer, sbyte, char, and custom TypeConverter conversions succeed as expected
✅ Negative inputs for unsigned types return false without throwing
✅ Out-of-range and overflow values return false (verifies OverflowException handling)
✅ Invalid strings return false consistently across all supported types
✅ Null and empty string inputs are handled gracefully for nullable types
✅ Nullable unsigned, sbyte, and char conversions behave correctly
✅ Boundary values (MinValue/MaxValue) are processed accurately for numeric types
✅ Character conversion accepts single-character values and rejects multi-character strings
✅ Custom TypeConverter JsonException scenarios return false as expected
✅ Custom TypeConverter exceptions other than JsonException are caught and return false (verifies converter exception handling)
Before Fix:
After Fix:
Fixes #42435