Skip to content
Open
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
9 changes: 9 additions & 0 deletions src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,15 @@ private static Expression CreateDefaultValueExpression(object? defaultValue, Typ
{
if (defaultValue is null)
{
// For non-nullable value types (e.g., Guid, DateTime, TimeSpan), reflection reports
// DefaultValue == null when the parameter default is `= default`. Using
// Expression.Constant(null, valueType) would throw an ArgumentException because null
// is not valid for a non-nullable value type. Use Expression.Default instead.
if (parameterType.IsValueType && Nullable.GetUnderlyingType(parameterType) is null)
{
return Expression.Default(parameterType);
}

return Expression.Constant(null, parameterType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,52 @@ public async Task MapAction_ExplicitParsableParameter_StringReturn()
await VerifyResponseBodyAsync(httpContext, "10");
}

[Theory]
[InlineData(null, "Id: 00000000-0000-0000-0000-000000000000")]
[InlineData("a0e1f2b3-c4d5-4e6f-7a8b-9c0d1e2f3a4b", "Id: a0e1f2b3-c4d5-4e6f-7a8b-9c0d1e2f3a4b")]
public async Task CanSetGuidParamAsOptionalWithDefaultValue(string queryValue, string expectedResponse)
{
Comment on lines +142 to +146
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

queryValue is passed as null via [InlineData(null, ...)], but the parameter is declared as non-nullable string. Consider changing it to string? queryValue so the signature matches the test data and avoids nullable-analysis warnings.

Copilot uses AI. Check for mistakes.
var (_, compilation) = await RunGeneratorAsync("""
app.MapGet("/", ([FromQuery] Guid id = default) => $"Id: {id}");
""");
var endpoint = GetEndpointFromCompilation(compilation);

var httpContext = CreateHttpContext();
if (queryValue is not null)
{
httpContext.Request.Query = new QueryCollection(new Dictionary<string, StringValues>
{
["id"] = queryValue
});
}

await endpoint.RequestDelegate(httpContext);
await VerifyResponseBodyAsync(httpContext, expectedResponse);
}

[Theory]
[InlineData(null, "Date: 0001-01-01T00:00:00.0000000")]
[InlineData("2024-01-15", "Date: 2024-01-15T00:00:00.0000000")]
public async Task CanSetDateTimeParamAsOptionalWithDefaultValue(string queryValue, string expectedResponse)
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

queryValue is provided as null in one [InlineData] case, but the parameter is declared string (non-nullable). Update it to string? queryValue to accurately model the test inputs and avoid nullable warnings.

Suggested change
public async Task CanSetDateTimeParamAsOptionalWithDefaultValue(string queryValue, string expectedResponse)
public async Task CanSetDateTimeParamAsOptionalWithDefaultValue(string? queryValue, string expectedResponse)

Copilot uses AI. Check for mistakes.
{
var (_, compilation) = await RunGeneratorAsync("""
app.MapGet("/", ([FromQuery] DateTime date = default) => $"Date: {date:O}");
""");
var endpoint = GetEndpointFromCompilation(compilation);

var httpContext = CreateHttpContext();
if (queryValue is not null)
{
httpContext.Request.Query = new QueryCollection(new Dictionary<string, StringValues>
{
["date"] = queryValue
});
}

await endpoint.RequestDelegate(httpContext);
await VerifyResponseBodyAsync(httpContext, expectedResponse);
}

public static object[][] MapAction_ExplicitQueryParam_NameTest_Data
{
get
Expand Down
Loading