Skip to content

Commit

Permalink
Fixed issue "(EmptyBodyBehavior = EmptyBodyBehavior.Allow)" not being…
Browse files Browse the repository at this point in the history
… honored when request body was empty (#1885)
  • Loading branch information
normj authored Nov 26, 2024
1 parent 22382ba commit 71a9ce6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .autover/changes/7db9a194-0cc2-43e7-81da-e2f468d3fc90.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.AspNetCoreServer",
"Type": "Patch",
"ChangelogMessages": [
"Fixed issue \u0027(EmptyBodyBehavior = EmptyBodyBehavior.Allow)\u0027 not being honored when request body was empty"
]
}
]
}
11 changes: 11 additions & 0 deletions .autover/changes/f8f316e8-1393-4185-bfb1-07ccfa2e68bf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.AspNetCoreServer.Hosting",
"Type": "Patch",
"ChangelogMessages": [
"Fixed issue \u0027(EmptyBodyBehavior = EmptyBodyBehavior.Allow)\u0027 not being honored when request body was empty"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ bool IHttpRequestBodyDetectionFeature.CanHaveBody
get
{
var requestFeature = (IHttpRequestFeature)this;
return requestFeature.Body != null;
return requestFeature.Body != null && requestFeature.Body.Length > 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ public async Task TestPutWithBody()
Assert.Equal("text/plain; charset=utf-8", response.MultiValueHeaders["Content-Type"][0]);
}

[Fact]
public async Task TestPutNoBody()
{
var response = await this.InvokeAPIGatewayRequest("values-put-no-body-request.json");

Assert.Equal(string.Empty, response.Body);
Assert.Equal(202, response.StatusCode);
}

[Fact]
public async Task TestDefaultResponseErrorCode()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"resource": "/{proxy+}",
"path": "/api/values/no-body",
"httpMethod": "PUT",
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-ASN": "20055",
"CloudFront-Viewer-Country": "US",
"Host": "example.execute-api.us-west-2.amazonaws.com",
"User-Agent": "unittests",
"Via": "1.1 3dd55c11c72ef969f5e46a679c8244ba.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "BA8bXHQfFrXacZsRsTUqvFLc849kUgSURcu8IkPIIadtNO2k6gMbtg==",
"X-Amzn-Trace-Id": "Root=1-67451657-4fdc56e47b7240b358e7ef09",
"X-Forwarded-For": "50.35.61.49, 3.172.20.16",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
"ContentLength": "0",
"Content-Type": "application/json"
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": {
"proxy": "api/values/no-body"
},
"stageVariables": null,
"requestContext": {
"resourceId": "3r2bn3",
"resourcePath": "/{proxy+}",
"httpMethod": "PUT",
"extendedRequestId": "B1BtwG20vHcEp-g=",
"requestTime": "26/Nov/2024:00:29:11 +0000",
"path": "/Prod/api/values/test",
"accountId": "111122223333",
"protocol": "HTTP/1.1",
"stage": "Prod",
"domainPrefix": "example",
"requestTimeEpoch": 1732580951687,
"requestId": "b5738763-be0a-4ded-945b-a92895dbc5de",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"sourceIp": "50.35.61.49",
"principalOrgId": null,
"accessKey": null,
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "PostmanRuntime/7.42.0",
"user": null
},
"domainName": "example.execute-api.us-west-2.amazonaws.com",
"deploymentId": "example",
"apiId": "example"
},
"body": null,
"isBase64Encoded": false
}
12 changes: 12 additions & 0 deletions Libraries/test/TestWebApp/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace TestWebApp.Controllers
{
Expand Down Expand Up @@ -49,5 +50,16 @@ public async Task<IActionResult> ChectContentLength()
return Content(sb.ToString());
}
}

[HttpPut("no-body")]
public IActionResult Test([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Body request = default)
{
return Accepted();
}

public class Body
{
public string Prop { get; set; }
}
}
}

0 comments on commit 71a9ce6

Please sign in to comment.