Treat empty query strings as null for nullable value types#65816
Open
BloodShop wants to merge 5 commits intodotnet:mainfrom
Open
Treat empty query strings as null for nullable value types#65816BloodShop wants to merge 5 commits intodotnet:mainfrom
BloodShop wants to merge 5 commits intodotnet:mainfrom
Conversation
When binding query parameters like ?myBool= (empty value) to nullable value types (bool?, int?, etc.), the framework was attempting to parse the empty string and failing with a 500 error. Controllers handle this correctly by treating empty query values as null for nullable parameters. Minimal APIs should do the same. The fix: for nullable value types with FromQuery binding, check for empty strings before attempting TryParse. Empty strings are skipped, leaving the parameter at its default value (null). Changes: - RequestDelegateFactory: Use NotNullOrEmpty check for nullable value types instead of NotNull - Add tests for bool? and int? with empty query string values Fixes dotnet#65754
Author
|
@dotnet-policy-service agree |
The conditional expression was mixing UnaryExpression and BinaryExpression types, which caused CS0173 compiler error. Cast to common Expression type to fix. Also removed incorrect 'isOptional' check - nullable value types should always treat empty query strings as null, not just when optional.
Author
🔧 Root Cause Found & FixedThe PR was failing to compile due to a type mismatch in the conditional expression: The ProblemLine 1804 in
This causes CS0173: Type of conditional expression cannot be determined — which cascades to fail ALL aspnetcore-ci builds. The Fix// Before (❌ compilation error)
var sourceCheckExpr = isOptional && isNullableValueType
? TempSourceStringIsNotNullOrEmptyExpr
: TempSourceStringNotNullExpr;
// After (✅ compiles cleanly)
var sourceCheckExpr = (Expression)(isNullableValueType
? TempSourceStringIsNotNullOrEmptyExpr
: TempSourceStringNotNullExpr);Also removed the faulty Verification✅ Microsoft.AspNetCore.Http.Extensions builds successfully The fix is minimal (2-line change) and surgical. Ready for testing. |
3182533 to
a736080
Compare
…otnet#65805) When an endpoint has multiple parameters including [FromBody] and other injected dependencies, the request body description should use the [FromBody] parameter's XML comment, not another parameter's comment. Example: PostData([FromBody] SomeData data, ILogger logger, CancellationToken ct) Expected: description from 'data' parameter Actual: description from 'ct' parameter (incorrectly)
This was referenced Mar 18, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Minimal APIs throw a 500 error when binding empty query string values (
?myBool=) to nullable value type parameters likebool?,int?, etc.Controllers handle this correctly — they treat empty query values as
nullfor nullable parameters. Minimal APIs should match that behavior.Reproduction
Calling
/test?myBool=throws:Root Cause
In
RequestDelegateFactory.cs, when constructing the parameter binding expression for parseable types, the code checksif (tempSourceString != null)before attemptingTryParse.But when the query string is
?myBool=, the value is an empty string (""), notnull. So:bool.TryParse("", out var result)is calledTryParsereturnsfalse(empty string is not a valid bool)For nullable value types, empty strings should be treated as
null, not as invalid input.The Fix
For nullable value types (
Nullable<T>), useIsNotNullOrEmptyinstead ofIsNotNullwhen deciding whether to attempt parsing:When the query value is empty, we skip the
TryParseblock entirely, and the parameter remains at its default value (nullfor nullable types).Changes
RequestDelegateFactory.csNullable.GetUnderlyingTypeIsNotNullOrEmptycheck for nullable value typesRequestDelegateCreationTests.QueryParameters.cs(tests)MapAction_NullableBoolParam_WithEmptyQueryStringValue_SetsNull— verifies?myBool=binds tonullMapAction_NullableIntParam_WithEmptyQueryStringValue_SetsNull— verifies?value=binds tonullBehavior After Fix
?myBool=bool?null✅?myBool=truebool?truetrue✅?myInt=int?null✅?name=string?""(empty string)""(unchanged) ✅Breaking Changes
None. This only changes error behavior to match controller semantics.
Fixes #65754