AspNetCoreServer incorrectly sets IHttpRequestBodyDetectionFeature.CanHaveBody that leads to breaking change after net 7 upgrade #1854
Labels
bug
This issue is a bug.
module/aspnetcore-support
needs-reproduction
This issue needs reproduction.
p2
This is a standard priority issue
Describe the bug
Hi,
It appears that the behavior of the APIGatewayProxyFunction is inconsistent with the behavior of Kestrel when handling requests with an empty body directed to endpoints with a parameter marked [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)].
In the InvokeFeatures class, the CanHaveBody property is set based on requestFeature.Body != null. This is problematic because the IHttpRequestFeature.Body property is initialized with a new MemoryStream() value, therefor CanHaveBody is never false.
As a result, this change introduces a breaking behavior when upgrading from .NET 6.0 to .NET 7.0 in scenarios where requests with an empty body are sent from AWS Lambda. ASP.NET Core attempts to deserialize the request (due to the CanHaveBody property), and because the request body is an empty stream, it causes the API to throw an error.
Regression Issue
Expected Behavior
Endpoints with parameters marked with
[FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)]
attribute can handle request both with and without body.Current Behavior
AspNetCore tries to deserialize the body and throws
The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
Reproduction Steps
Create
PUT
endpoint with optional body:and send requests with and without body.
Possible Solution
No response
Additional Information/Context
No response
AWS .NET SDK and/or Package version used
Amazon.Lambda.AspNetCoreServer 9.0.1
Targeted .NET Platform
NET 8.0
Operating System and version
Windows 10
The text was updated successfully, but these errors were encountered: