-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
16 changed files
with
117 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...s-serde/src/main/java/io/micronaut/aws/lambda/events/serde/SerdeCustomPojoSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...in/resources/META-INF/services/com.amazonaws.services.lambda.runtime.CustomPojoSerializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
io.micronaut.function.aws.JsonMapperCustomPojoSerializer | ||
io.micronaut.aws.lambda.events.serde.SerdeCustomPojoSerializer |
27 changes: 2 additions & 25 deletions
27
...st/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayCustomAuthorizerEventSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 2 additions & 9 deletions
11
...c/test/groovy/io/micronaut/aws/lambda/events/serde/APIGatewayProxyRequestEventSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 2 additions & 26 deletions
28
...rc/test/groovy/io/micronaut/aws/lambda/events/serde/CloudFrontSimpleRemoteCallSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 2 additions & 10 deletions
12
...serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CloudWatchLogsEventSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 2 additions & 26 deletions
28
...src/test/groovy/io/micronaut/aws/lambda/events/serde/CodeCommitRepositoryEventSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 2 additions & 27 deletions
29
...s-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CognitoEventSerdeSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...rde/src/test/groovy/io/micronaut/aws/lambda/events/serde/CustomPojoSerializerUtils.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
28 changes: 2 additions & 26 deletions
28
...vents-serde/src/test/groovy/io/micronaut/aws/lambda/events/serde/DynamodbEventSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.