Skip to content

Update serialization support for Amazon.Lambda.CognitoEvents & Code Generated JsonSerlizationContext's #1567

Open
@Simonl9l

Description

@Simonl9l

Describe the feature

Per https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.CognitoEvents

It would likely be valuable to the dev community if the packages such that this support system.text.json code generated serialization and provided JsonSerlizationContext(s).

Use Case

The value proposition, specifically in regard to Amazon.Lambda.CognitoEvents is that Lambda's implementing Cognito Trigger, say via the Annotations library or raw Lambda Handler implementation have specific response timing constraints/requirements that make AoT a requirement, that in turn requires code generated serialization.

This likely applies to all the Lambda Event packages, that in some cases is more applicable to Minimal API than Annotations.

Whist one can can define one's own code generated serialization contexts for type sin other packages, it can have undesirable effects with any trimmed assemblies as the metadata to generate the serializer is not available - see dotnet/runtime#78029.

Proposed Solution

These JsonSerlizationContext(s) can be added into the package and can then be used explicit, or configured into Lambda by chaining the IJsonTypeInfoResolver resolvers. For example:

using System.Text.Json.Serialization;
using Amazon.Lambda.CognitoEvents;

namespace Amazon.Lambda.CognitoEvents
{
    [JsonSerializable(typeof(CognitoCreateAuthChallengeEvent))]
    [JsonSerializable(typeof(CognitoDefineAuthChallengeEvent))]
    ... // each of the events
    public partial class CognitoEventsSerializationContext : JsonSerializerContext
    {
    }
}

The system.text.json support to combination of serialization contexts:

var combinedResolver = JsonTypeInfoResolver.Combine(
    MySerializationContext.Default, // my code gen serializer context for my other types.
    CognitoEventsSerializationContext.Default);

and configured in say a Minimal API context with (.Net7)

 builder.Services.ConfigureHttpJsonOptions(options =>
    {
        options.SerializerOptions.TypeInfoResolver = combinedResolver;
    });

However the SourceGeneratorLambdaJsonSerializer would need some updates to accept the IJsonTypeInfoResolver from the combine.

Other Information

Related there see to be numerous ongoing conversation regarding System.Text.Json support for pre-existing contracts such as [DataMember] etc. but expect that ton of these solutions support System.Text.Json code generation and JsonSerlizationContext's.

Whilst this code mostly was updated a year ago, it seem to make the premise that #if NETCOREAPP3_1 be the correct define to determine if to use [DataMember] or [JsonPropertyName] attributes in support of System.Text.Json.

Per the MSFT docs NETCOREAPP3_1 preprocessor symbol is not defined when targeting .NET 5 - Recommended Action seems to indicate it should use #if NETCOREAPP.

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS .NET SDK and/or Package version used

Amazon.Lambda.CognitoEvents 2.1.1

Targeted .NET Platform

.Net 7 and above

Operating System and version

All

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions