Skip to content

Commit 656975e

Browse files
authored
Merge pull request #48626 from geoand/#14513
Ensure that DynamodbEvent can be properly deserialized in Lambda
2 parents a9cad58 + b9db8a0 commit 656975e

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

extensions/amazon-lambda/common-runtime/src/main/java/io/quarkus/amazon/lambda/runtime/AmazonLambdaMapperRecorder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public void initObjectMapper() {
2424
objectMapper = getObjectMapper()
2525
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
2626
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
27-
.registerModule(new JodaModule());
27+
.registerModule(new JodaModule())
28+
.registerModule(new DateModule());
2829
}
2930

3031
public void initContextReaders() {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
2+
3+
package io.quarkus.amazon.lambda.runtime;
4+
5+
import java.io.IOException;
6+
import java.util.Date;
7+
8+
import com.fasterxml.jackson.core.JsonGenerator;
9+
import com.fasterxml.jackson.core.JsonParser;
10+
import com.fasterxml.jackson.core.json.PackageVersion;
11+
import com.fasterxml.jackson.databind.DeserializationContext;
12+
import com.fasterxml.jackson.databind.JsonDeserializer;
13+
import com.fasterxml.jackson.databind.JsonSerializer;
14+
import com.fasterxml.jackson.databind.SerializerProvider;
15+
import com.fasterxml.jackson.databind.module.SimpleModule;
16+
17+
/**
18+
* Copied from: <a href=
19+
* "https://raw.githubusercontent.com/aws/aws-lambda-java-libs/95dc035b2a3200be04eaa48cef6053404250e547/aws-lambda-java-serialization/src/main/java/com/amazonaws/services/lambda/runtime/serialization/events/modules/DateModule.java">here</a>
20+
*
21+
* The AWS API represents a date as a double, which specifies the fractional
22+
* number of seconds since the epoch. Java's Date, however, represents a date as
23+
* a long, which specifies the number of milliseconds since the epoch. This
24+
* class is used to translate between these two formats.
25+
*
26+
* This class is copied from LambdaEventBridgeservice
27+
* com.amazon.aws.lambda.stream.ddb.DateModule
28+
*/
29+
class DateModule extends SimpleModule {
30+
private static final long serialVersionUID = 1L;
31+
32+
public static final class Serializer extends JsonSerializer<Date> {
33+
@Override
34+
public void serialize(Date date, JsonGenerator generator, SerializerProvider serializers) throws IOException {
35+
if (date != null) {
36+
generator.writeNumber(millisToSeconds(date.getTime()));
37+
}
38+
}
39+
}
40+
41+
public static final class Deserializer extends JsonDeserializer<Date> {
42+
@Override
43+
public Date deserialize(JsonParser parser, DeserializationContext context) throws IOException {
44+
double dateSeconds = parser.getValueAsDouble();
45+
if (dateSeconds == 0.0) {
46+
return null;
47+
} else {
48+
return new Date((long) secondsToMillis(dateSeconds));
49+
}
50+
}
51+
}
52+
53+
private static double millisToSeconds(double millis) {
54+
return millis / 1000.0;
55+
}
56+
57+
private static double secondsToMillis(double seconds) {
58+
return seconds * 1000.0;
59+
}
60+
61+
public DateModule() {
62+
super(PackageVersion.VERSION);
63+
addSerializer(Date.class, new Serializer());
64+
addDeserializer(Date.class, new Deserializer());
65+
}
66+
}

0 commit comments

Comments
 (0)