Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Fix undefined field httpMethod. (#53)
Browse files Browse the repository at this point in the history
* Fix undefined field httpMethod.

Signed-off-by: David Calavera <david.calavera@gmail.com>
(cherry picked from commit 9365ba49090ff0e8976415e93919cced6f3bd599)

* Make alb tests run only when the feature is enabled.

Signed-off-by: David Calavera <david.calavera@gmail.com>
  • Loading branch information
calavera authored Feb 5, 2022
1 parent 32a3939 commit eac2396
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 5 deletions.
37 changes: 37 additions & 0 deletions aws_lambda_events/src/generated/alb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,40 @@ pub struct AlbTargetGroupResponse {
#[serde(rename = "isBase64Encoded")]
pub is_base64_encoded: bool,
}

#[cfg(test)]
mod test {
use super::*;

extern crate serde_json;

#[test]
#[cfg(feature = "alb")]
fn alb_lambda_target_request_headers_only() {
let data = include_bytes!("fixtures/alb-lambda-target-request-headers-only.json");
let parsed: AlbTargetGroupRequest = serde_json::from_slice(data).unwrap();
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: AlbTargetGroupRequest = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}

#[test]
#[cfg(feature = "alb")]
fn alb_lambda_target_request_multivalue_headers() {
let data = include_bytes!("fixtures/alb-lambda-target-request-multivalue-headers.json");
let parsed: AlbTargetGroupRequest = serde_json::from_slice(data).unwrap();
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: AlbTargetGroupRequest = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}

#[test]
#[cfg(feature = "alb")]
fn alb_lambda_target_response() {
let data = include_bytes!("fixtures/alb-lambda-target-response.json");
let parsed: AlbTargetGroupResponse = serde_json::from_slice(data).unwrap();
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: AlbTargetGroupResponse = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
}
6 changes: 4 additions & 2 deletions aws_lambda_events/src/generated/apigw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,11 @@ where
pub resource_path: Option<String>,
#[serde(bound = "")]
pub authorizer: Option<T1>,
#[serde(with = "http_method")]
#[serde(deserialize_with = "http_method::deserialize_optional")]
#[serde(serialize_with = "http_method::serialize_optional")]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "httpMethod")]
pub http_method: Method,
pub http_method: Option<Method>,
/// The API Gateway rest API Id
#[serde(deserialize_with = "deserialize_lambda_string")]
#[serde(default)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"requestContext": {
"elb": {
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/lambda-target/abcdefg"
}
},
"httpMethod": "GET",
"path": "/",
"queryStringParameters": {
"key": "hello"
},
"headers": {
"accept": "*/*",
"connection": "keep-alive",
"host": "lambda-test-alb-1334523864.us-east-1.elb.amazonaws.com",
"user-agent": "curl/7.54.0",
"x-amzn-trace-id": "Root=1-5c34e93e-4dea0086f9763ac0667b115a",
"x-forwarded-for": "25.12.198.67",
"x-forwarded-port": "80",
"x-forwarded-proto": "http",
"x-imforwards": "20",
"x-myheader": "123"
},
"isBase64Encoded": false
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"requestContext": {
"elb": {
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/lambda-target/abcdefgh"
}
},
"httpMethod": "GET",
"path": "/",
"multiValueQueryStringParameters": {
"key": [
"hello"
]
},
"multiValueHeaders": {
"accept": [
"*/*"
],
"connection": [
"keep-alive"
],
"host": [
"lambda-test-alb-1234567.us-east-1.elb.amazonaws.com"
],
"user-agent": [
"curl/7.54.0"
],
"x-amzn-trace-id": [
"Root=1-5c34e7d4-00ca239424b68028d4c56d68"
],
"x-forwarded-for": [
"72.21.198.67"
],
"x-forwarded-port": [
"80"
],
"x-forwarded-proto": [
"http"
],
"x-imforwards": [
"20"
],
"x-myheader": [
"123"
]
},
"body": "Some text",
"isBase64Encoded": false
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"isBase64Encoded": false,
"statusCode": 200,
"statusDescription": "200 OK",
"headers": {
"Set-cookie": "cookies",
"Content-Type": "application/json"
},
"multiValueHeaders": {
"Set-cookie": ["cookie-name=cookie-value;Domain=myweb.com;Secure;HttpOnly","cookie-name=cookie-value;Expires=May 8, 2019"],
"Content-Type": ["application/json"]
},
"body": "Hello from Lambda"
}

15 changes: 13 additions & 2 deletions aws_lambda_events_codegen/go_to_rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,13 @@ fn translate_go_type_to_rust_type<'a>(
libraries.insert("crate::custom_serde::*".to_string());
libraries.insert("http::Method".to_string());

let mut value = "Method";
let mut annotations = vec!["#[serde(with = \"http_method\")]".to_string()];
if let Some(def) = member_def {
if def.struct_name == "ApiGatewayWebsocketProxyRequest" {
if def
.struct_name
.starts_with("ApiGatewayWebsocketProxyRequest")
{
annotations = vec![
"#[serde(deserialize_with = \"http_method::deserialize_optional\")]"
.to_string(),
Expand All @@ -752,10 +756,17 @@ fn translate_go_type_to_rust_type<'a>(
"#[serde(skip_serializing_if = \"Option::is_none\")]".to_string(),
];
}

// Make this method optional in the context explicitly because the Go
// bindings don't mark it as omitempty, but it might not appear in the payload.
// See: https://github.com/LegNeato/aws-lambda-events/issues/33#issuecomment-792050434
if def.struct_name == "ApiGatewayWebsocketProxyRequestContext" {
value = "Option<Method>";
}
}

RustType {
value: "Method".into(),
value: value.into(),
annotations,
libraries,
generics: vec![],
Expand Down
13 changes: 12 additions & 1 deletion aws_lambda_events_codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn find_custom_examples(
fuzzy_files: &HashMap<String, PathBuf>,
example_event_path: &Path,
) -> Option<Vec<ExampleEvent>> {
let files = match service_name {
let files: &[(&str, &str)] = match service_name {
"apigw" => &[
(
"apigw-custom-auth-request-type-request.json",
Expand Down Expand Up @@ -267,6 +267,17 @@ fn find_custom_examples(
"ApiGatewayWebsocketProxyRequest",
),
],
"alb" => &[
(
"alb-lambda-target-request-headers-only.json",
"AlbTargetGroupRequest",
),
(
"alb-lambda-target-request-multivalue-headers.json",
"AlbTargetGroupRequest",
),
("alb-lambda-target-response.json", "AlbTargetGroupResponse"),
],
_ => return None,
};

Expand Down

0 comments on commit eac2396

Please sign in to comment.