Giter VIP home page Giter VIP logo

Comments (6)

tduncan avatar tduncan commented on August 17, 2024 1

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.

msailes avatar msailes commented on August 17, 2024

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.

tduncan avatar tduncan commented on August 17, 2024

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.

msailes avatar msailes commented on August 17, 2024

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.

tduncan avatar tduncan commented on August 17, 2024

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.

mo7ty avatar mo7ty commented on August 17, 2024

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 names
  • lowerCamelCase 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.