aws / aws/aws-lambda-dotnet

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

Open
#1,567 5 comments 0 reactions 1 assignee Claimed by @normj View on GitHub
feature-request module/lambda-client-lib needs-investigation p2
Dominant language
C#
Stars
1.7k
Forks
503
Avg merge
1d 5h
Merged PRs (30d)
18

Description

### 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](https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.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 https://github.com/dotnet/runtime/issues/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:

```C#
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:
```C#
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)
```C#
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](https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/5.0/netcoreapp3_1-preprocessor-symbol-not-defined) seems to indicate it should use `#if NETCOREAPP`.

### Acknowledgements

- [X] 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.