Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions src/AwsLambda.Host.Abstractions/Options/EnvelopeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,6 @@ namespace AwsLambda.Host.Options;
/// <seealso cref="IResponseEnvelope" />
public class EnvelopeOptions
{
/// <summary>
/// Gets the default JSON serialization options that match those used by
/// <see cref="DefaultLambdaJsonSerializer" />.
/// </summary>
/// <remarks>
/// <para>
/// Provides AWS Lambda-specific JSON settings for deserialization. This is used for complex
/// envelope payloads such as SNS to SQS and CloudWatch Logs.
/// </para>
/// <para>
/// The <see cref="JsonSerializerOptions.TypeInfoResolver" /> from <see cref="JsonOptions" />
/// will be added to these options during post-configuration.
/// </para>
/// </remarks>
public readonly Lazy<JsonSerializerOptions> LambdaDefaultJsonOptions = new(() =>
{
var options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = new AwsNamingPolicy(),
};
options.Converters.Add(new DateTimeConverter());
options.Converters.Add(new MemoryStreamConverter());
options.Converters.Add(new ConstantClassConverter());
options.Converters.Add(new ByteArrayConverter());

return options;
});

/// <summary>
/// Gets or sets a dictionary for storing custom extension data associated with envelope
/// processing.
Expand All @@ -72,6 +42,44 @@ public class EnvelopeOptions
/// </remarks>
public JsonSerializerOptions JsonOptions { get; set; } = new();

/// <summary>
/// Gets the default JSON serialization options that match those used by
/// <see cref="DefaultLambdaJsonSerializer" />.
/// </summary>
/// <remarks>
/// <para>
/// Provides AWS Lambda-specific JSON settings for deserialization. This is used for complex
/// envelope payloads such as SNS to SQS and CloudWatch Logs.
/// </para>
/// <para>
/// During post-configuration, the <see cref="JsonSerializerOptions.TypeInfoResolver" /> from
/// <see cref="JsonOptions" /> will be copied to these options if <c>TypeInfoResolver</c> has
/// not been explicitly configured.
/// </para>
/// </remarks>
public JsonSerializerOptions LambdaDefaultJsonOptions
{
get
{
if (field is null)
{
field = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = new AwsNamingPolicy(),
};
field.Converters.Add(new DateTimeConverter());
field.Converters.Add(new MemoryStreamConverter());
field.Converters.Add(new ConstantClassConverter());
field.Converters.Add(new ByteArrayConverter());
}

return field;
}
set;
}

/// <summary>Gets or sets the XML reader settings used when deserializing Lambda event payloads.</summary>
/// <remarks>
/// <para>Options include DTD processing, whitespace handling, and conformance level.</para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ namespace AwsLambda.Host.Options;
internal class EnvelopeOptionsPostConfiguration : IPostConfigureOptions<EnvelopeOptions>
{
public void PostConfigure(string? name, EnvelopeOptions options) =>
options.LambdaDefaultJsonOptions.Value.TypeInfoResolver = options
.JsonOptions
.TypeInfoResolver;
options.LambdaDefaultJsonOptions.TypeInfoResolver ??= options.JsonOptions.TypeInfoResolver;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public virtual void ExtractPayload(EnvelopeOptions options)
AwslogsContent =
JsonSerializer.Deserialize<AwsLogsEnvelope>(
decodedData,
options.LambdaDefaultJsonOptions.Value
options.LambdaDefaultJsonOptions
) ?? throw new InvalidOperationException("Invalid CloudWatch Logs data.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override void ExtractPayload(EnvelopeOptions options)
{
record.BodyContent = JsonSerializer.Deserialize<SnsEnvelopeBase<T>.SnsMessageEnvelope>(
record.Body,
options.LambdaDefaultJsonOptions.Value
options.LambdaDefaultJsonOptions
);

record.BodyContent!.MessageContent = JsonSerializer.Deserialize<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,7 @@ object awslogsData
{
var envelopeOptions = new EnvelopeOptions();

var json = JsonSerializer.Serialize(
awslogsData,
envelopeOptions.LambdaDefaultJsonOptions.Value
);
var json = JsonSerializer.Serialize(awslogsData, envelopeOptions.LambdaDefaultJsonOptions);
var jsonBytes = Encoding.UTF8.GetBytes(json);

using var outputStream = new MemoryStream();
Expand Down Expand Up @@ -534,10 +531,7 @@ private CloudWatchLogsEnvelope CreateNonGenericEnvelopeWithAwslogsData(object aw
{
var envelopeOptions = new EnvelopeOptions();

var json = JsonSerializer.Serialize(
awslogsData,
envelopeOptions.LambdaDefaultJsonOptions.Value
);
var json = JsonSerializer.Serialize(awslogsData, envelopeOptions.LambdaDefaultJsonOptions);
var jsonBytes = Encoding.UTF8.GetBytes(json);

using var outputStream = new MemoryStream();
Expand Down
Loading