Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CustomPojoSerializer adds lambda events pkg #2080

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws-lambda-events-serde/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.micronaut.function.aws.JsonMapperCustomPojoSerializer
io.micronaut.aws.lambda.events.serde.SerdeCustomPojoSerializer
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CustomPojoSerializer> customPojoSerializers() {
List<CustomPojoSerializer> services = new ArrayList<>()
ServiceLoader<CustomPojoSerializer> 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")
}
}
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading
Loading