Comments (6)
Hi @mo7ty,
Thanks for the suggestion of using LambdaEventSerializers
to obtain PojoSerializer
. This approach does indeed do what I need (with an extra step of re-converting the JSON string back into a Map
) and I no longer need to use the mixin classes directly.
from aws-lambda-java-libs.
Hi @tduncan,
Are you creating SNSEvent
for a test case? If you are, I would recommend using the unit testing support. This will use the same serialization logic as the managed runtime. So that you won't run into serialization problems.
from aws-lambda-java-libs.
Hi @msailes,
The need is for testing but the use case is actually turning SNSEvent
(and friends) into their Map
representation using Jackson. We're building a Lambda layer intended to process any incoming event and have tests send specific event types to verify the expected behavior is observed.
I briefly looked at the unit testing support library but it didn't seem to help with the translating the AWS event classes back into JSON which is what we are doing.
Is there perhaps another preferred alternative to using the *Mixin
classes directly?
from aws-lambda-java-libs.
I think I understand. You can add a test dependency on the serialization module.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-serialization</artifactId>
<version>1.1.2</version>
</dependency>
Then get the serializer for the event.
PojoSerializer<SNSEvent> serializer = JacksonFactory.getInstance().getSerializer(SNSEvent.class);
serializer.toJson(event, outputStream);
from aws-lambda-java-libs.
Unfortunately I am finding the same thing using the built in serializers as well.
Using:
PojoSerializer<SNSEvent> serializer = JacksonFactory.getInstance().getSerializer(SNSEvent.class);
serializer.toJson(event, System.out);
Produces this json (some fields excluded for brevity):
{
"records": [
{
"eventSource": "aws:sns",
"SNS": {
"messageAttributes": {},
"messageId": "a9d6754d-f284-4328-aee7-cedb88d78ea5",
"topicArn": "arn:aws:sns:123456789012:us-west-2:my-notification"
}
}
]
}
My understanding of the data coming into Lambda is that for SNSEvent
these fields will follow the CamelCase naming convention, so Records
, EventSource
, MessageAttributes
, MessageId, TopicArn
, etc.
Am I understanding the JSON spec for this input correctly? I have been using this example as a reference.
from aws-lambda-java-libs.
Hi @tduncan,
If you use the returned serializer from aws-lambda-java-serialization - LambdaEventSerializers.serializerFor() to create your JSON string, it will produce your expected result:
SNSRecord record = new SNSRecord();
record.setEventSource("aws:sns");
SNSEvent event = new SNSEvent();
event.setRecords(List.of(record));
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
LambdaEventSerializers.serializerFor(SNSEvent.class,
ClassLoader.getSystemClassLoader())
.toJson(event, stream);
String json = stream.toString();
System.out.println(json);
}
Output:
{
"Records": [
{
"EventSource": "aws:sns"
}
]
}
Indeed AWS documents and reports Lambda Events fields in UpperCamelCase
, but when using Java AWS SDKs the Java conventions are followed:
UpperCamelCase
for class nameslowerCamelCase
for method, variable and parameter names
For this reason, in order to be able to properly use Jackson ObjectMapper
, is necessary to use the aws-lambda-java-serialization custom mixins.
Refer to aws-lambda-java-tests - EventLoaderTest.testLoadSNSEvent(), that loads the sns_event.json and successfully converts the AWS UpperCamelCase
fields into the Java objects lowerCamelCase
ones.
from aws-lambda-java-libs.
Related Issues (20)
- Simulate AWS runtime environment for lambda in local run. HOT 3
- KinesisEvent not parsed correctly HOT 2
- Incomplete and/or missing API Gateway Lambda Authorizer events HOT 5
- RequestStreamHandler not working as expect HOT 2
- AWS lamba core reactive types HOT 2
- EventLoader unable to correctly deserialize posted json body data for APIGateway events HOT 2
- Failed to serialize CognitoUserPoolPreTokenGenerationEvent HOT 7
- Support VPC Lattice event structure
- APIGatewayProxyRequestEvent$RequestIdentity missing "cognitoAmr" field HOT 2
- Unable to upgrade with newer Jackson - conflicting `com.fasterxml.jackson.annotation.JsonFormat` HOT 5
- Failed to deserialize DynamodbEvent.DynamodbStreamRecord#dynamodb HOT 6
- Support v2 of Pre Token Generation Lambda HOT 1
- AWS documentation says LambdaLogger will default to the INFO log level, but it is actually defaulting to UNDEFINED HOT 2
- resolved
- Why does amazon not support a unified naming standard for java libs? HOT 1
- Response streaming support for java lambda HOT 1
- APIGatewayV2HTTPEvent does not properly deserialize event HOT 1
- CognitoUserPoolPreTokenGenerationEvent Only supports V1 of the event HOT 1
- Scheduled Event (V2) contains non-object detail parameter (it is a string)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-lambda-java-libs.