Skip to content

Commit 71a9ce6

Browse files
authored
Fixed issue "(EmptyBodyBehavior = EmptyBodyBehavior.Allow)" not being honored when request body was empty (#1885)
1 parent 22382ba commit 71a9ce6

File tree

6 files changed

+109
-1
lines changed

6 files changed

+109
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.AspNetCoreServer",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Fixed issue \u0027(EmptyBodyBehavior = EmptyBodyBehavior.Allow)\u0027 not being honored when request body was empty"
8+
]
9+
}
10+
]
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.AspNetCoreServer.Hosting",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Fixed issue \u0027(EmptyBodyBehavior = EmptyBodyBehavior.Allow)\u0027 not being honored when request body was empty"
8+
]
9+
}
10+
]
11+
}

Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/InvokeFeatures.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ bool IHttpRequestBodyDetectionFeature.CanHaveBody
392392
get
393393
{
394394
var requestFeature = (IHttpRequestFeature)this;
395-
return requestFeature.Body != null;
395+
return requestFeature.Body != null && requestFeature.Body.Length > 0;
396396
}
397397
}
398398

Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestCallingWebAPI.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ public async Task TestPutWithBody()
126126
Assert.Equal("text/plain; charset=utf-8", response.MultiValueHeaders["Content-Type"][0]);
127127
}
128128

129+
[Fact]
130+
public async Task TestPutNoBody()
131+
{
132+
var response = await this.InvokeAPIGatewayRequest("values-put-no-body-request.json");
133+
134+
Assert.Equal(string.Empty, response.Body);
135+
Assert.Equal(202, response.StatusCode);
136+
}
137+
129138
[Fact]
130139
public async Task TestDefaultResponseErrorCode()
131140
{
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"resource": "/{proxy+}",
3+
"path": "/api/values/no-body",
4+
"httpMethod": "PUT",
5+
"headers": {
6+
"Accept": "*/*",
7+
"Accept-Encoding": "gzip, deflate, br",
8+
"CloudFront-Forwarded-Proto": "https",
9+
"CloudFront-Is-Desktop-Viewer": "true",
10+
"CloudFront-Is-Mobile-Viewer": "false",
11+
"CloudFront-Is-SmartTV-Viewer": "false",
12+
"CloudFront-Is-Tablet-Viewer": "false",
13+
"CloudFront-Viewer-ASN": "20055",
14+
"CloudFront-Viewer-Country": "US",
15+
"Host": "example.execute-api.us-west-2.amazonaws.com",
16+
"User-Agent": "unittests",
17+
"Via": "1.1 3dd55c11c72ef969f5e46a679c8244ba.cloudfront.net (CloudFront)",
18+
"X-Amz-Cf-Id": "BA8bXHQfFrXacZsRsTUqvFLc849kUgSURcu8IkPIIadtNO2k6gMbtg==",
19+
"X-Amzn-Trace-Id": "Root=1-67451657-4fdc56e47b7240b358e7ef09",
20+
"X-Forwarded-For": "50.35.61.49, 3.172.20.16",
21+
"X-Forwarded-Port": "443",
22+
"X-Forwarded-Proto": "https",
23+
"ContentLength": "0",
24+
"Content-Type": "application/json"
25+
},
26+
"queryStringParameters": null,
27+
"multiValueQueryStringParameters": null,
28+
"pathParameters": {
29+
"proxy": "api/values/no-body"
30+
},
31+
"stageVariables": null,
32+
"requestContext": {
33+
"resourceId": "3r2bn3",
34+
"resourcePath": "/{proxy+}",
35+
"httpMethod": "PUT",
36+
"extendedRequestId": "B1BtwG20vHcEp-g=",
37+
"requestTime": "26/Nov/2024:00:29:11 +0000",
38+
"path": "/Prod/api/values/test",
39+
"accountId": "111122223333",
40+
"protocol": "HTTP/1.1",
41+
"stage": "Prod",
42+
"domainPrefix": "example",
43+
"requestTimeEpoch": 1732580951687,
44+
"requestId": "b5738763-be0a-4ded-945b-a92895dbc5de",
45+
"identity": {
46+
"cognitoIdentityPoolId": null,
47+
"accountId": null,
48+
"cognitoIdentityId": null,
49+
"caller": null,
50+
"sourceIp": "50.35.61.49",
51+
"principalOrgId": null,
52+
"accessKey": null,
53+
"cognitoAuthenticationType": null,
54+
"cognitoAuthenticationProvider": null,
55+
"userArn": null,
56+
"userAgent": "PostmanRuntime/7.42.0",
57+
"user": null
58+
},
59+
"domainName": "example.execute-api.us-west-2.amazonaws.com",
60+
"deploymentId": "example",
61+
"apiId": "example"
62+
},
63+
"body": null,
64+
"isBase64Encoded": false
65+
}

Libraries/test/TestWebApp/Controllers/ValuesController.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.Mvc.ModelBinding;
89

910
namespace TestWebApp.Controllers
1011
{
@@ -49,5 +50,16 @@ public async Task<IActionResult> ChectContentLength()
4950
return Content(sb.ToString());
5051
}
5152
}
53+
54+
[HttpPut("no-body")]
55+
public IActionResult Test([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Body request = default)
56+
{
57+
return Accepted();
58+
}
59+
60+
public class Body
61+
{
62+
public string Prop { get; set; }
63+
}
5264
}
5365
}

0 commit comments

Comments
 (0)