From 9cd35ec63a255da7d8a0dfc0c7af51d0c8183634 Mon Sep 17 00:00:00 2001 From: Sergio del Amo Date: Fri, 8 Mar 2024 15:48:28 +0100 Subject: [PATCH] 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: https://github.com/micronaut-projects/micronaut-aws/issues/2048 * override method * Remove license from tests and fix return type --------- Co-authored-by: Tim Yates --- aws-lambda-events-serde/build.gradle.kts | 1 + .../serde/SerdeCustomPojoSerializer.java | 43 +++++++++++++++++++ ...rvices.lambda.runtime.CustomPojoSerializer | 2 +- ...APIGatewayCustomAuthorizerEventSpec.groovy | 27 +----------- .../APIGatewayProxyRequestEventSpec.groovy | 11 +---- .../serde/AllAwsLambdaEventsSpec.groovy | 1 - .../CloudFrontSimpleRemoteCallSpec.groovy | 28 +----------- .../serde/CloudWatchLogsEventSpec.groovy | 12 +----- .../CodeCommitRepositoryEventSpec.groovy | 28 +----------- .../events/serde/CognitoEventSerdeSpec.groovy | 29 +------------ .../serde/CustomPojoSerializerUtils.groovy | 32 ++++++++++++++ .../events/serde/DynamodbEventSpec.groovy | 28 +----------- .../serde/S3EventNotificationSpec.groovy | 20 +++------ .../events/serde/SQSReceiveMessageSpec.groovy | 28 +----------- .../SerializeMapsAndCollectionsSpec.groovy | 28 +----------- .../aws/JsonMapperCustomPojoSerializer.java | 17 +++++++- 16 files changed, 117 insertions(+), 218 deletions(-) create mode 100644 aws-lambda-events-serde/src/main/java/io/micronaut/aws/lambda/events/serde/SerdeCustomPojoSerializer.java create mode 100644 aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CustomPojoSerializerUtils.groovy diff --git a/aws-lambda-events-serde/build.gradle.kts b/aws-lambda-events-serde/build.gradle.kts index d4c328ccf9..e2682054b0 100644 --- a/aws-lambda-events-serde/build.gradle.kts +++ b/aws-lambda-events-serde/build.gradle.kts @@ -3,6 +3,7 @@ plugins { } dependencies { + implementation(projects.micronautFunctionAws) annotationProcessor(mnSerde.micronaut.serde.processor) api(mnSerde.micronaut.serde.api) api(libs.managed.aws.lambda.events) diff --git a/aws-lambda-events-serde/src/main/java/io/micronaut/aws/lambda/events/serde/SerdeCustomPojoSerializer.java b/aws-lambda-events-serde/src/main/java/io/micronaut/aws/lambda/events/serde/SerdeCustomPojoSerializer.java new file mode 100644 index 0000000000..47e40e3a0b --- /dev/null +++ b/aws-lambda-events-serde/src/main/java/io/micronaut/aws/lambda/events/serde/SerdeCustomPojoSerializer.java @@ -0,0 +1,43 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micronaut.aws.lambda.events.serde; + +import com.amazonaws.services.lambda.runtime.CustomPojoSerializer; +import io.micronaut.core.annotation.NonNull; +import io.micronaut.function.aws.JsonMapperCustomPojoSerializer; +import io.micronaut.json.JsonMapper; +import io.micronaut.serde.ObjectMapper; + +import java.util.Collections; + +/** + * Provides an implementation of {@link CustomPojoSerializer} which is loaded via SPI. + * This implementation avoids paying a double hit on performance when using a serialization library inside the Lambda function. + * 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. + * @author Sergio del Amo + * @since 4.0.0 + */ +public class SerdeCustomPojoSerializer extends JsonMapperCustomPojoSerializer { + + private static final String PACKAGE_IO_MICRONAUT_AWS_LAMBDA_EVENTS_SERDE = "io.micronaut.aws.lambda.events.serde"; + + @Override + @NonNull + protected JsonMapper createDefault() { + return ObjectMapper.create(Collections.emptyMap(), PACKAGE_IO_MICRONAUT_AWS_LAMBDA_EVENTS_SERDE); + } + +} diff --git a/aws-lambda-events-serde/src/main/resources/META-INF/services/com.amazonaws.services.lambda.runtime.CustomPojoSerializer b/aws-lambda-events-serde/src/main/resources/META-INF/services/com.amazonaws.services.lambda.runtime.CustomPojoSerializer index 7dd3e35d52..1ada5332a5 100644 --- a/aws-lambda-events-serde/src/main/resources/META-INF/services/com.amazonaws.services.lambda.runtime.CustomPojoSerializer +++ b/aws-lambda-events-serde/src/main/resources/META-INF/services/com.amazonaws.services.lambda.runtime.CustomPojoSerializer @@ -1 +1 @@ -io.micronaut.function.aws.JsonMapperCustomPojoSerializer +io.micronaut.aws.lambda.events.serde.SerdeCustomPojoSerializer diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayCustomAuthorizerEventSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayCustomAuthorizerEventSpec.groovy index d782207c7c..44eba7aeb5 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayCustomAuthorizerEventSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayCustomAuthorizerEventSpec.groovy @@ -1,35 +1,12 @@ -/* - * Copyright 2022 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.APIGatewayCustomAuthorizerEvent -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import spock.lang.Specification -@MicronautTest(startApplication = false) class APIGatewayCustomAuthorizerEventSpec extends Specification { - @Inject - ObjectMapper objectMapper - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "APIGatewayCustomAuthorizerEvent can be serialized"() { given: diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayProxyRequestEventSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayProxyRequestEventSpec.groovy index 7e652d894f..928df8a5e1 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayProxyRequestEventSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayProxyRequestEventSpec.groovy @@ -1,19 +1,12 @@ package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import spock.lang.Specification -@MicronautTest(startApplication = false) class APIGatewayProxyRequestEventSpec extends Specification { - @Inject - ObjectMapper objectMapper - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "APIGatewayProxyRequestEvent can be serialized"() { given: diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/AllAwsLambdaEventsSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/AllAwsLambdaEventsSpec.groovy index dde5522a73..af1a93be37 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/AllAwsLambdaEventsSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/AllAwsLambdaEventsSpec.groovy @@ -19,7 +19,6 @@ package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.* import com.amazonaws.services.lambda.runtime.events.models.dynamodb.AttributeValue import com.amazonaws.services.lambda.runtime.events.models.dynamodb.StreamRecord -import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification import com.amazonaws.services.lambda.runtime.serialization.events.LambdaEventSerializers import com.amazonaws.services.lambda.runtime.serialization.events.mixins.* import com.amazonaws.services.lambda.runtime.serialization.events.modules.DateModule diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudFrontSimpleRemoteCallSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudFrontSimpleRemoteCallSpec.groovy index 78f92952af..7c2397f939 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudFrontSimpleRemoteCallSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudFrontSimpleRemoteCallSpec.groovy @@ -1,36 +1,12 @@ -/* - * Copyright 2022 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.CloudFrontEvent -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import spock.lang.Specification -@MicronautTest(startApplication = false) class CloudFrontSimpleRemoteCallSpec extends Specification { - @Inject - ObjectMapper objectMapper - - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "test deserialization of cloudfront simple remote call event"() { given: diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudWatchLogsEventSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudWatchLogsEventSpec.groovy index 6440434d10..92754ec093 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudWatchLogsEventSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudWatchLogsEventSpec.groovy @@ -1,20 +1,12 @@ package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.CloudWatchLogsEvent -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import spock.lang.Specification -@MicronautTest(startApplication = false) class CloudWatchLogsEventSpec extends Specification { - @Inject - ObjectMapper objectMapper - - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "test deserialization of cloud watch scheduled event"() { given: diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CodeCommitRepositoryEventSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CodeCommitRepositoryEventSpec.groovy index f181e98562..aa2c3528e3 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CodeCommitRepositoryEventSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CodeCommitRepositoryEventSpec.groovy @@ -1,38 +1,14 @@ -/* - * Copyright 2022 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.CodeCommitEvent -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import org.joda.time.DateTime import org.joda.time.DateTimeZone import spock.lang.Specification -@MicronautTest(startApplication = false) class CodeCommitRepositoryEventSpec extends Specification { - @Inject - ObjectMapper objectMapper - - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "test deserialization of cloud watch scheduled event"() { given: diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CognitoEventSerdeSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CognitoEventSerdeSpec.groovy index 6f49c06c43..434e43d621 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CognitoEventSerdeSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CognitoEventSerdeSpec.groovy @@ -1,37 +1,12 @@ -/* - * Copyright 2022 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.micronaut.aws.lambda.events.serde - import com.amazonaws.services.lambda.runtime.events.CognitoEvent -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import spock.lang.Specification -@MicronautTest(startApplication = false) class CognitoEventSerdeSpec extends Specification { - @Inject - ObjectMapper objectMapper - - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "test deserialization of cloud watch scheduled event"() { given: diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CustomPojoSerializerUtils.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CustomPojoSerializerUtils.groovy new file mode 100644 index 0000000000..fe1dc015a8 --- /dev/null +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CustomPojoSerializerUtils.groovy @@ -0,0 +1,32 @@ +package io.micronaut.aws.lambda.events.serde + +import com.amazonaws.services.lambda.runtime.CustomPojoSerializer +import io.micronaut.json.JsonMapper +import io.micronaut.serde.ObjectMapper + +import javax.naming.ConfigurationException + +class CustomPojoSerializerUtils { + + private static List customPojoSerializers() { + List services = new ArrayList<>() + ServiceLoader loader = ServiceLoader.load(CustomPojoSerializer.class) + loader.forEach(services::add) + return services + } + + static CustomPojoSerializer customPojoSerializer() { + return customPojoSerializers() + .stream() + .findFirst() + .orElseThrow(() -> new RuntimeException("No CustomPojoSerializer found")) + } + + static JsonMapper getJsonMapper() { + CustomPojoSerializer pojoSerializer = customPojoSerializer() + if (pojoSerializer instanceof SerdeCustomPojoSerializer) { + return pojoSerializer.jsonMapper + } + throw new ConfigurationException("CustomPojoSerializer is not a type of SerdeCustomPojoSerializer") + } +} diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/DynamodbEventSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/DynamodbEventSpec.groovy index b90d233388..7e6d386bd7 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/DynamodbEventSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/DynamodbEventSpec.groovy @@ -1,36 +1,12 @@ -/* - * Copyright 2022 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.DynamodbEvent -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import spock.lang.Specification -@MicronautTest(startApplication = false) class DynamodbEventSpec extends Specification { - @Inject - ObjectMapper objectMapper - - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "test deserialization of sqs recieve message event"() { given: diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/S3EventNotificationSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/S3EventNotificationSpec.groovy index 046483cd05..f1d84c303a 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/S3EventNotificationSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/S3EventNotificationSpec.groovy @@ -2,20 +2,14 @@ package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.S3Event import com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject -import spock.lang.PendingFeature +import io.micronaut.json.JsonMapper +import spock.lang.Shared import spock.lang.Specification -@MicronautTest(startApplication = false) class S3EventNotificationSpec extends Specification { - @Inject - ObjectMapper objectMapper - @Inject - BeanContext beanContext + @Shared + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "S3EventNotification can be serialized with s3-put"() { given: @@ -48,7 +42,7 @@ class S3EventNotificationSpec extends Specification { when: String json = f.text - S3EventNotification event = objectMapper.readValue(json, S3EventNotification) + S3EventNotification event = getObjectMapper().readValue(json, S3EventNotification) then: assertionsS3Event(event) @@ -63,7 +57,7 @@ class S3EventNotificationSpec extends Specification { when: String json = f.text - S3Event event = objectMapper.readValue(json, S3Event) + S3Event event = getObjectMapper().readValue(json, S3Event) then: assertionsS3Put(event) @@ -78,7 +72,7 @@ class S3EventNotificationSpec extends Specification { when: String json = f.text - S3Event event = objectMapper.readValue(json, S3Event) + S3Event event = getObjectMapper().readValue(json, S3Event) then: assertionsS3Event(event) diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/SQSReceiveMessageSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/SQSReceiveMessageSpec.groovy index d1d285cb10..b5d3886c2b 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/SQSReceiveMessageSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/SQSReceiveMessageSpec.groovy @@ -1,36 +1,12 @@ -/* - * Copyright 2022 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.micronaut.aws.lambda.events.serde import com.amazonaws.services.lambda.runtime.events.SQSEvent -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import spock.lang.Specification -@MicronautTest(startApplication = false) class SQSReceiveMessageSpec extends Specification { - @Inject - ObjectMapper objectMapper - - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "test deserialization of sqs recieve message event"() { given: diff --git a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/SerializeMapsAndCollectionsSpec.groovy b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/SerializeMapsAndCollectionsSpec.groovy index 4f92fe1c89..fc4cf77800 100644 --- a/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/SerializeMapsAndCollectionsSpec.groovy +++ b/aws-lambda-events-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/SerializeMapsAndCollectionsSpec.groovy @@ -1,35 +1,11 @@ -/* - * Copyright 2022 original authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package io.micronaut.aws.lambda.events.serde -import io.micronaut.context.BeanContext -import io.micronaut.serde.ObjectMapper -import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import io.micronaut.json.JsonMapper import spock.lang.Specification -@MicronautTest(startApplication = false) class SerializeMapsAndCollectionsSpec extends Specification { - @Inject - ObjectMapper objectMapper - - @Inject - BeanContext beanContext + JsonMapper objectMapper = CustomPojoSerializerUtils.getJsonMapper() void "Serialize map with values that are of type ArrayList"() { given: diff --git a/function-aws/src/main/java/io/micronaut/function/aws/JsonMapperCustomPojoSerializer.java b/function-aws/src/main/java/io/micronaut/function/aws/JsonMapperCustomPojoSerializer.java index 0141b48268..1f670f9dca 100644 --- a/function-aws/src/main/java/io/micronaut/function/aws/JsonMapperCustomPojoSerializer.java +++ b/function-aws/src/main/java/io/micronaut/function/aws/JsonMapperCustomPojoSerializer.java @@ -35,14 +35,19 @@ * @since 4.0.0 */ public class JsonMapperCustomPojoSerializer implements CustomPojoSerializer { - private JsonMapper jsonMapper; + protected JsonMapper jsonMapper; public JsonMapperCustomPojoSerializer() { this.jsonMapper = createDefault(); } + /** + * + * @return A JsonMapper + */ // https://github.com/micronaut-projects/micronaut-core/pull/9445 - private @NonNull JsonMapper createDefault() { + @NonNull + protected JsonMapper createDefault() { return ServiceLoader.load(JsonMapperSupplier.class).stream() .flatMap(p -> { try { @@ -64,6 +69,14 @@ public JsonMapperCustomPojoSerializer() { .orElseThrow(() -> new IllegalStateException("No JsonMapper implementation found")); } + /** + * + * @return The Json Mapper + */ + public JsonMapper getJsonMapper() { + return jsonMapper; + } + @Override public T fromJson(InputStream input, Type type) { try {