-
Notifications
You must be signed in to change notification settings - Fork 475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AspNetCoreServer incorrectly sets IHttpRequestBodyDetectionFeature.CanHaveBody that leads to breaking change after net 7 upgrade #1854
Comments
@MadSciencist Good morning. Could you please share if you are using Amazon.Lambda.Annotations.APIGateway.FromBodyAttribute attribute or Microsoft.AspNetCore.Mvc.FromBodyAttribute class? The one provided by EDIT: Ignore my comment. This has nothing to do with annotations. Will try to reproduce it at my end. Thanks, |
@MadSciencist Good afternoon. Somehow, I'm unable to reproduce the issue using the below code (used AWS Serverless Web API template): <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- Generate ready to run images during publishing to improve cold start time. -->
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="9.0.1" />
</ItemGroup>
</Project> Controllers\ValuesController.cs using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace AWSServerlessApiNET8.Controllers
{
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
[HttpPut("test")]
public IActionResult Test([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Body request = default)
{
return Accepted();
}
public class Body
{
public string Prop { get; set; }
}
}
} Below are the results:
Please advise if I'm missing anything in reproduction. Thanks, |
Good morning, |
@MadSciencist Thanks for the input. After decorating using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace AWSServerlessApiNET8.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpPut("test")]
public IActionResult Test([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Body request = default)
{
return Accepted();
}
public class Body
{
public string Prop { get; set; }
}
}
}
Just on the side note,
I'm unsure why model validation is not kicked in locally when executing under Kestrel, when using |
Version 9.0.3 of Amazon.Lambda.AspNetCoreServer and 1.7.3 of Amazon.Lambda.AspNetCoreServer.Hosting have been released with this fix. Thanks for letting us know about the issue. |
@MadSciencist Closing this issue. Please let us know if you still encounter the issue in the latest versions. |
Comments on closed issues are hard for our team to see. |
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: