dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

Building ASP.NET minimal API with either PublishTrimmed or PublishAot throw BadHttpRequestException: Required parameter #52506

Closed alex-jitbit closed 9 months ago

alex-jitbit commented 9 months ago

Is there an existing issue for this?

Describe the bug

Runing ASP.NET 8 app with either PublishTrimmed or PublishAot throws BadHttpRequestException: Required parameter "string xxx" was not provided from route or query string

`app.MapGet("/", async (string xxx) => {
    return Results.Content("");
});

Without PublishTrimmed/Aot everyhting works as it should (because string is nullable)

Adding a default value workaround also helps:

`app.MapGet("/", async (string xxx = null) => {
    return Results.Content("");
});

Expected Behavior

Should work like it would without Timmed/Aot

Steps To Reproduce

`app.MapGet("/", async (string xxx) => {
    return Results.Content("");
});

Exceptions (if any)

BadHttpRequestException

.NET Version

8.0

Anything else?

No response

captainsafia commented 9 months ago

@alex-jitbit I don't think the behavior that you are seeing is related to publishing the app with trimming and native AoT enabled.

Minimal APIs enforces requiredness checks on parameters by default. If a parameter is seen as required (no default value and no nullability annotations), then we will generate code that throws a BadHttpRequestException at runtime.

You can read more about this in this docs.

ghost commented 9 months ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

alex-jitbit commented 9 months ago

No @captainsafia , Minimal API is not enforcing parameter requirement for nullable types (references types and nullable value types) Please make sure you read the issue correctly. It works just fine without Native AOT, but stops working once I enable trimmed/aot compilation.

captainsafia commented 9 months ago

@alex-jitbit Any chance you can share a repo? When I try to repro this, I observe the same behavior whether or not EnabledAot is set.

var app = WebApplication.Create();

app.MapGet("/", (string foo) =>
{
    return Results.Content("");
});

app.Run();

PublishAoT = false

http http://localhost:5261/
HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Date: Mon, 11 Dec 2023 04:33:58 GMT
Server: Kestrel
Transfer-Encoding: chunked

Microsoft.AspNetCore.Http.BadHttpRequestException: Required parameter "string foo" was not provided from query string.
   at lambda_method1(Closure, Object, HttpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

HEADERS
=======
Accept: */*
Connection: keep-alive
Host: localhost:5261
User-Agent: HTTPie/3.2.2
Accept-Encoding: gzip, deflate

PublishAoT = true

http http://localhost:5261/
HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Date: Mon, 11 Dec 2023 04:37:09 GMT
Server: Kestrel
Transfer-Encoding: chunked

Microsoft.AspNetCore.Http.BadHttpRequestException: Required parameter "string foo" was not provided from route or query string.
   at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>FB49A76E00958A78ED8A3DC031D4149080B37ED6EB129C512F9112A3DB80F087D__LogOrThrowExceptionHelper.RequiredParameterNotProvided(String parameterTypeName, String parameterName, String source) in /Users/captainsafia/tests/NullableStringTest/Microsoft.AspNetCore.Http.RequestDelegateGenerator/Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator/GeneratedRouteBuilderExtensions.g.cs:line 314
   at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>FB49A76E00958A78ED8A3DC031D4149080B37ED6EB129C512F9112A3DB80F087D__GeneratedRouteBuilderExtensionsCore.<>c__DisplayClass2_0.<MapGet0>g__RequestHandler|4(HttpContext httpContext) in /Users/captainsafia/tests/NullableStringTest/Microsoft.AspNetCore.Http.RequestDelegateGenerator/Microsoft.AspNetCore.Http.RequestDelegateGenerator.RequestDelegateGenerator/GeneratedRouteBuilderExtensions.g.cs:line 112
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

HEADERS
=======
Accept: */*
Connection: keep-alive
Host: localhost:5261
User-Agent: HTTPie/3.2.2
Accept-Encoding: gzip, deflate