-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathdeserializer.rs
167 lines (145 loc) · 6.3 KB
/
deserializer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::request::LambdaRequest;
#[cfg(feature = "alb")]
use aws_lambda_events::alb::AlbTargetGroupRequest;
#[cfg(feature = "apigw_rest")]
use aws_lambda_events::apigw::ApiGatewayProxyRequest;
#[cfg(feature = "apigw_http")]
use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
#[cfg(feature = "apigw_websockets")]
use aws_lambda_events::apigw::ApiGatewayWebsocketProxyRequest;
use serde::{de::Error, Deserialize};
use serde_json::value::RawValue;
const ERROR_CONTEXT: &str = "this function expects a JSON payload from Amazon API Gateway, Amazon Elastic Load Balancer, or AWS Lambda Function URLs, but the data doesn't match any of those services' events";
#[cfg(feature = "pass_through")]
const PASS_THROUGH_ENABLED: bool = true;
impl<'de> Deserialize<'de> for LambdaRequest {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let raw_value: Box<RawValue> = Box::<RawValue>::deserialize(deserializer)?;
let data = raw_value.get();
#[cfg(feature = "apigw_rest")]
if let Ok(res) = serde_json::from_str::<ApiGatewayProxyRequest>(data) {
return Ok(LambdaRequest::ApiGatewayV1(res));
}
#[cfg(feature = "apigw_http")]
if let Ok(res) = serde_json::from_str::<ApiGatewayV2httpRequest>(data) {
return Ok(LambdaRequest::ApiGatewayV2(res));
}
#[cfg(feature = "alb")]
if let Ok(res) = serde_json::from_str::<AlbTargetGroupRequest>(data) {
return Ok(LambdaRequest::Alb(res));
}
#[cfg(feature = "apigw_websockets")]
if let Ok(res) = serde_json::from_str::<ApiGatewayWebsocketProxyRequest>(data) {
return Ok(LambdaRequest::WebSocket(res));
}
#[cfg(feature = "pass_through")]
if PASS_THROUGH_ENABLED {
return Ok(LambdaRequest::PassThrough(data.to_string()));
}
Err(Error::custom(ERROR_CONTEXT))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_deserialize_apigw_rest() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-apigw-request.json");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize apigw rest data");
match req {
LambdaRequest::ApiGatewayV1(req) => {
assert_eq!("12345678912", req.request_context.account_id.unwrap());
}
other => panic!("unexpected request variant: {:?}", other),
}
}
#[test]
fn test_deserialize_apigw_http() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-apigw-v2-request-iam.json");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize apigw http data");
match req {
LambdaRequest::ApiGatewayV2(req) => {
assert_eq!("123456789012", req.request_context.account_id.unwrap());
}
other => panic!("unexpected request variant: {:?}", other),
}
}
#[test]
fn test_deserialize_sam_rest() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-apigw-sam-rest-request.json");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize SAM rest data");
match req {
LambdaRequest::ApiGatewayV1(req) => {
assert_eq!("123456789012", req.request_context.account_id.unwrap());
}
other => panic!("unexpected request variant: {:?}", other),
}
}
#[test]
fn test_deserialize_sam_http() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-apigw-sam-http-request.json");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize SAM http data");
match req {
LambdaRequest::ApiGatewayV2(req) => {
assert_eq!("123456789012", req.request_context.account_id.unwrap());
}
other => panic!("unexpected request variant: {:?}", other),
}
}
#[test]
fn test_deserialize_alb() {
let data = include_bytes!(
"../../lambda-events/src/fixtures/example-alb-lambda-target-request-multivalue-headers.json"
);
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize alb rest data");
match req {
LambdaRequest::Alb(req) => {
assert_eq!(
"arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/lambda-target/abcdefgh",
req.request_context.elb.target_group_arn.unwrap()
);
}
other => panic!("unexpected request variant: {:?}", other),
}
}
#[test]
fn test_deserialize_apigw_websocket() {
let data =
include_bytes!("../../lambda-events/src/fixtures/example-apigw-websocket-request-without-method.json");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize apigw websocket data");
match req {
LambdaRequest::WebSocket(req) => {
assert_eq!("CONNECT", req.request_context.event_type.unwrap());
}
other => panic!("unexpected request variant: {:?}", other),
}
}
#[test]
#[cfg(feature = "pass_through")]
fn test_deserialize_bedrock_agent() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-bedrock-agent-runtime-event.json");
let req: LambdaRequest =
serde_json::from_slice(data).expect("failed to deserialize bedrock agent request data");
match req {
LambdaRequest::PassThrough(req) => {
assert_eq!(String::from_utf8_lossy(data), req);
}
other => panic!("unexpected request variant: {:?}", other),
}
}
#[test]
#[cfg(feature = "pass_through")]
fn test_deserialize_sqs() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-sqs-event.json");
let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize sqs event data");
match req {
LambdaRequest::PassThrough(req) => {
assert_eq!(String::from_utf8_lossy(data), req);
}
other => panic!("unexpected request variant: {:?}", other),
}
}
}