Skip to content

Commit 9cd35ec

Browse files
sdelamotimyates
andauthored
fix: CustomPojoSerializer adds lambda events pkg (#2080)
* fix: CustomPojoSerializer adds lambda events pkg Add a CustomPojoSerializer implementation which includes the package io.micronaut.aws.lambda.events.serde. This package contains `@SerdeImport` for the AWS Lambda Events classes. Close: #2048 * override method * Remove license from tests and fix return type --------- Co-authored-by: Tim Yates <tim.yates@gmail.com>
1 parent 9a439d1 commit 9cd35ec

16 files changed

+117
-218
lines changed

aws-lambda-events-serde/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ plugins {
33
}
44

55
dependencies {
6+
implementation(projects.micronautFunctionAws)
67
annotationProcessor(mnSerde.micronaut.serde.processor)
78
api(mnSerde.micronaut.serde.api)
89
api(libs.managed.aws.lambda.events)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2017-2024 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.aws.lambda.events.serde;
17+
18+
import com.amazonaws.services.lambda.runtime.CustomPojoSerializer;
19+
import io.micronaut.core.annotation.NonNull;
20+
import io.micronaut.function.aws.JsonMapperCustomPojoSerializer;
21+
import io.micronaut.json.JsonMapper;
22+
import io.micronaut.serde.ObjectMapper;
23+
24+
import java.util.Collections;
25+
26+
/**
27+
* Provides an implementation of {@link CustomPojoSerializer} which is loaded via SPI.
28+
* This implementation avoids paying a double hit on performance when using a serialization library inside the Lambda function.
29+
* This implementations adds the package {@value #PACKAGE_IO_MICRONAUT_AWS_LAMBDA_EVENTS_SERDE} which contains {@link io.micronaut.serde.annotation.SerdeImport} for the AWS Lambda Events classes to the ObjectMapper creation.
30+
* @author Sergio del Amo
31+
* @since 4.0.0
32+
*/
33+
public class SerdeCustomPojoSerializer extends JsonMapperCustomPojoSerializer {
34+
35+
private static final String PACKAGE_IO_MICRONAUT_AWS_LAMBDA_EVENTS_SERDE = "io.micronaut.aws.lambda.events.serde";
36+
37+
@Override
38+
@NonNull
39+
protected JsonMapper createDefault() {
40+
return ObjectMapper.create(Collections.emptyMap(), PACKAGE_IO_MICRONAUT_AWS_LAMBDA_EVENTS_SERDE);
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
io.micronaut.function.aws.JsonMapperCustomPojoSerializer
1+
io.micronaut.aws.lambda.events.serde.SerdeCustomPojoSerializer

aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayCustomAuthorizerEventSpec.groovy

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
1-
/*
2-
* Copyright 2022 original authors
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
171
package io.micronaut.aws.lambda.events.serde
182

193
import com.amazonaws.services.lambda.runtime.events.APIGatewayCustomAuthorizerEvent
20-
import io.micronaut.context.BeanContext
21-
import io.micronaut.serde.ObjectMapper
22-
import io.micronaut.test.extensions.spock.annotation.MicronautTest
23-
import jakarta.inject.Inject
4+
import io.micronaut.json.JsonMapper
245
import spock.lang.Specification
256

26-
@MicronautTest(startApplication = false)
277
class APIGatewayCustomAuthorizerEventSpec extends Specification {
28-
@Inject
29-
ObjectMapper objectMapper
308

31-
@Inject
32-
BeanContext beanContext
9+
JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper()
3310

3411
void "APIGatewayCustomAuthorizerEvent can be serialized"() {
3512
given:

aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayProxyRequestEventSpec.groovy

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package io.micronaut.aws.lambda.events.serde
22

33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
4-
import io.micronaut.context.BeanContext
5-
import io.micronaut.serde.ObjectMapper
6-
import io.micronaut.test.extensions.spock.annotation.MicronautTest
7-
import jakarta.inject.Inject
4+
import io.micronaut.json.JsonMapper
85
import spock.lang.Specification
96

10-
@MicronautTest(startApplication = false)
117
class APIGatewayProxyRequestEventSpec extends Specification {
12-
@Inject
13-
ObjectMapper objectMapper
148

15-
@Inject
16-
BeanContext beanContext
9+
JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper()
1710

1811
void "APIGatewayProxyRequestEvent can be serialized"() {
1912
given:

aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/AllAwsLambdaEventsSpec.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package io.micronaut.aws.lambda.events.serde
1919
import com.amazonaws.services.lambda.runtime.events.*
2020
import com.amazonaws.services.lambda.runtime.events.models.dynamodb.AttributeValue
2121
import com.amazonaws.services.lambda.runtime.events.models.dynamodb.StreamRecord
22-
import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification
2322
import com.amazonaws.services.lambda.runtime.serialization.events.LambdaEventSerializers
2423
import com.amazonaws.services.lambda.runtime.serialization.events.mixins.*
2524
import com.amazonaws.services.lambda.runtime.serialization.events.modules.DateModule

aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudFrontSimpleRemoteCallSpec.groovy

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
1-
/*
2-
* Copyright 2022 original authors
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
171
package io.micronaut.aws.lambda.events.serde
182

193
import com.amazonaws.services.lambda.runtime.events.CloudFrontEvent
20-
import io.micronaut.context.BeanContext
21-
import io.micronaut.serde.ObjectMapper
22-
import io.micronaut.test.extensions.spock.annotation.MicronautTest
23-
import jakarta.inject.Inject
4+
import io.micronaut.json.JsonMapper
245
import spock.lang.Specification
256

26-
@MicronautTest(startApplication = false)
277
class CloudFrontSimpleRemoteCallSpec extends Specification {
288

29-
@Inject
30-
ObjectMapper objectMapper
31-
32-
@Inject
33-
BeanContext beanContext
9+
JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper()
3410

3511
void "test deserialization of cloudfront simple remote call event"() {
3612
given:

aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudWatchLogsEventSpec.groovy

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package io.micronaut.aws.lambda.events.serde
22

33
import com.amazonaws.services.lambda.runtime.events.CloudWatchLogsEvent
4-
import io.micronaut.context.BeanContext
5-
import io.micronaut.serde.ObjectMapper
6-
import io.micronaut.test.extensions.spock.annotation.MicronautTest
7-
import jakarta.inject.Inject
4+
import io.micronaut.json.JsonMapper
85
import spock.lang.Specification
96

10-
@MicronautTest(startApplication = false)
117
class CloudWatchLogsEventSpec extends Specification {
128

13-
@Inject
14-
ObjectMapper objectMapper
15-
16-
@Inject
17-
BeanContext beanContext
9+
JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper()
1810

1911
void "test deserialization of cloud watch scheduled event"() {
2012
given:

aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CodeCommitRepositoryEventSpec.groovy

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
1-
/*
2-
* Copyright 2022 original authors
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
171
package io.micronaut.aws.lambda.events.serde
182

193
import com.amazonaws.services.lambda.runtime.events.CodeCommitEvent
20-
import io.micronaut.context.BeanContext
21-
import io.micronaut.serde.ObjectMapper
22-
import io.micronaut.test.extensions.spock.annotation.MicronautTest
23-
import jakarta.inject.Inject
4+
import io.micronaut.json.JsonMapper
245
import org.joda.time.DateTime
256
import org.joda.time.DateTimeZone
267
import spock.lang.Specification
278

28-
@MicronautTest(startApplication = false)
299
class CodeCommitRepositoryEventSpec extends Specification {
3010

31-
@Inject
32-
ObjectMapper objectMapper
33-
34-
@Inject
35-
BeanContext beanContext
11+
JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper()
3612

3713
void "test deserialization of cloud watch scheduled event"() {
3814
given:

aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CognitoEventSerdeSpec.groovy

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
1-
/*
2-
* Copyright 2022 original authors
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
171
package io.micronaut.aws.lambda.events.serde
182

19-
203
import com.amazonaws.services.lambda.runtime.events.CognitoEvent
21-
import io.micronaut.context.BeanContext
22-
import io.micronaut.serde.ObjectMapper
23-
import io.micronaut.test.extensions.spock.annotation.MicronautTest
24-
import jakarta.inject.Inject
4+
import io.micronaut.json.JsonMapper
255
import spock.lang.Specification
266

27-
@MicronautTest(startApplication = false)
287
class CognitoEventSerdeSpec extends Specification {
298

30-
@Inject
31-
ObjectMapper objectMapper
32-
33-
@Inject
34-
BeanContext beanContext
9+
JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper()
3510

3611
void "test deserialization of cloud watch scheduled event"() {
3712
given:

0 commit comments

Comments
 (0)