Skip to content

fix: null deserialization of stageVariables for sam local #366

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

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ pub enum LambdaRequest<'a> {
cookies: Option<Vec<Cow<'a, str>>>,
#[serde(deserialize_with = "deserialize_headers")]
headers: http::HeaderMap,
#[serde(default)]
#[serde(default, deserialize_with = "nullable_default")]
query_string_parameters: StrMap,
#[serde(default)]
#[serde(default, deserialize_with = "nullable_default")]
path_parameters: StrMap,
#[serde(default)]
#[serde(default, deserialize_with = "nullable_default")]
stage_variables: StrMap,
body: Option<Cow<'a, str>>,
#[serde(default)]
Expand All @@ -55,7 +55,7 @@ pub enum LambdaRequest<'a> {
/// the `lambda.multi_value_headers.enabled` target group setting turned on
#[serde(default, deserialize_with = "deserialize_multi_value_headers")]
multi_value_headers: http::HeaderMap,
#[serde(deserialize_with = "nullable_default")]
#[serde(default, deserialize_with = "nullable_default")]
query_string_parameters: StrMap,
/// For alb events these are only present when
/// the `lambda.multi_value_headers.enabled` target group setting turned on
Expand All @@ -75,7 +75,7 @@ pub enum LambdaRequest<'a> {
headers: http::HeaderMap,
#[serde(default, deserialize_with = "deserialize_multi_value_headers")]
multi_value_headers: http::HeaderMap,
#[serde(deserialize_with = "nullable_default")]
#[serde(default, deserialize_with = "nullable_default")]
query_string_parameters: StrMap,
#[serde(default, deserialize_with = "nullable_default")]
multi_value_query_string_parameters: StrMap,
Expand Down Expand Up @@ -736,6 +736,29 @@ mod tests {
);
}

#[test]
fn deserialize_apigw_v2_sam_local() {
// manually generated from AWS SAM CLI
// Steps to recreate:
// * sam init
// * Use, Zip Python 3.9, and Hello World example
// * Change the template to use HttpApi instead of Api
// * Change the function code to return the Lambda event serialized
// * sam local start-api
// * Invoke the API
let input = include_str!("../tests/data/apigw_v2_sam_local.json");
let result = from_str(input);
assert!(
result.is_ok(),
"event was not parsed as expected {:?} given {}",
result,
input
);
let req = result.expect("failed to parse request");
assert_eq!(req.method(), "GET");
assert_eq!(req.uri(), "http://127.0.0.1:3000/hello");
}

#[test]
fn deserialize_with_null() {
#[derive(Debug, PartialEq, Deserialize)]
Expand All @@ -749,4 +772,18 @@ mod tests {
Test { foo: HashMap::new() }
)
}

#[test]
fn deserialize_with_missing() {
#[derive(Debug, PartialEq, Deserialize)]
struct Test {
#[serde(default, deserialize_with = "nullable_default")]
foo: HashMap<String, String>,
}

assert_eq!(
serde_json::from_str::<Test>(r#"{}"#).expect("failed to deserialize"),
Test { foo: HashMap::new() }
)
}
}
46 changes: 46 additions & 0 deletions lambda-http/tests/data/apigw_v2_sam_local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": "2.0",
"routeKey": "GET /hello",
"rawPath": "/hello",
"rawQueryString": "",
"cookies": [],
"headers": {
"Host": "127.0.0.1:3000",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Cache-Control": "max-age=0",
"X-Forwarded-Proto": "http",
"X-Forwarded-Port": "3000"
},
"queryStringParameters": {},
"requestContext": {
"accountId": "123456789012",
"apiId": "1234567890",
"http": {
"method": "GET",
"path": "/hello",
"protocol": "HTTP/1.1",
"sourceIp": "127.0.0.1",
"userAgent": "Custom User Agent String"
},
"requestId": "1ac06eee-f687-44ec-9036-dfd49d0be0a3",
"routeKey": "GET /hello",
"stage": "$default",
"time": "16/Nov/2021:11:54:33 +0000",
"timeEpoch": 1637063673,
"domainName": "localhost",
"domainPrefix": "localhost"
},
"body": "",
"pathParameters": {},
"stageVariables": null,
"isBase64Encoded": false
}