Skip to content

Commit

Permalink
Merge pull request #657 from hjgraca/fix(logging)-typeinforesolver-no…
Browse files Browse the repository at this point in the history
…n-aot

chore: Remove TypeInfoResolver from non AOT mode.
  • Loading branch information
hjgraca authored Oct 2, 2024
2 parents a1275b4 + 83ff555 commit 4ab0fea
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ body:
label: AWS Lambda function runtime
options:
- dotnet6
- dotnet8
- dotnet8 (AOT)
validations:
required: true
- type: textarea
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Runtime.CompilerServices;

namespace AWS.Lambda.Powertools.Common.Utils;

/// <summary>
/// Wrapper for RuntimeFeature
/// </summary>
public static class RuntimeFeatureWrapper
{
private static Func<bool> _isDynamicCodeSupportedFunc = () => RuntimeFeature.IsDynamicCodeSupported;

/// <summary>
/// Check to see if IsDynamicCodeSupported
/// </summary>
public static bool IsDynamicCodeSupported => _isDynamicCodeSupportedFunc();

// For testing purposes
internal static void SetIsDynamicCodeSupported(bool value)
{
_isDynamicCodeSupportedFunc = () => value;
}

// To reset after tests
internal static void Reset()
{
_isDynamicCodeSupportedFunc = () => RuntimeFeature.IsDynamicCodeSupported;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Text.Json.Serialization.Metadata;
using Amazon.Lambda.Serialization.SystemTextJson;
using AWS.Lambda.Powertools.Common;
using AWS.Lambda.Powertools.Common.Utils;
using AWS.Lambda.Powertools.Logging.Internal.Converters;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -69,6 +70,13 @@ internal static string Serialize(object value, Type inputType)
var options = GetSerializerOptions();
return JsonSerializer.Serialize(value, options);
#else
if (RuntimeFeatureWrapper.IsDynamicCodeSupported)
{
var options = GetSerializerOptions();
#pragma warning disable
return JsonSerializer.Serialize(value, options);
}

var typeInfo = GetTypeInfo(inputType);
if (typeInfo == null)
{
Expand Down Expand Up @@ -154,10 +162,15 @@ private static JsonSerializerOptions BuildJsonSerializerOptions()
_jsonOptions.PropertyNameCaseInsensitive = true;

#if NET8_0_OR_GREATER
_jsonOptions.TypeInfoResolverChain.Add(PowertoolsLoggingSerializationContext.Default);
foreach (var context in AdditionalContexts)

// Only add TypeInfoResolver if AOT mode
if (!RuntimeFeatureWrapper.IsDynamicCodeSupported)
{
_jsonOptions.TypeInfoResolverChain.Add(context);
_jsonOptions.TypeInfoResolverChain.Add(PowertoolsLoggingSerializationContext.Default);
foreach (var context in AdditionalContexts)
{
_jsonOptions.TypeInfoResolverChain.Add(context);
}
}
#endif
return _jsonOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using Amazon.Lambda.Serialization.SystemTextJson;
using AWS.Lambda.Powertools.Common.Utils;
using AWS.Lambda.Powertools.Logging.Internal;
using AWS.Lambda.Powertools.Logging.Internal.Converters;
using AWS.Lambda.Powertools.Logging.Serializers;
Expand Down Expand Up @@ -48,8 +50,10 @@ public void SerializerOptions_ShouldNotBeNull()
[Fact]
public void SerializerOptions_ShouldHaveCorrectDefaultSettings()
{
var options = PowertoolsLoggingSerializer.GetSerializerOptions();

RuntimeFeatureWrapper.SetIsDynamicCodeSupported(false);

var options = PowertoolsLoggingSerializer.GetSerializerOptions();

Assert.Collection(options.Converters,
converter => Assert.IsType<ByteArrayConverter>(converter),
converter => Assert.IsType<ExceptionConverter>(converter),
Expand All @@ -70,6 +74,33 @@ public void SerializerOptions_ShouldHaveCorrectDefaultSettings()
resolver => Assert.IsType<PowertoolsLoggingSerializationContext>(resolver));
#endif
}

[Fact]
public void SerializerOptions_ShouldHaveCorrectDefaultSettings_WhenDynamic()
{
RuntimeFeatureWrapper.SetIsDynamicCodeSupported(true);

var options = PowertoolsLoggingSerializer.GetSerializerOptions();

Assert.Collection(options.Converters,
converter => Assert.IsType<ByteArrayConverter>(converter),
converter => Assert.IsType<ExceptionConverter>(converter),
converter => Assert.IsType<MemoryStreamConverter>(converter),
converter => Assert.IsType<ConstantClassConverter>(converter),
converter => Assert.IsType<DateOnlyConverter>(converter),
converter => Assert.IsType<TimeOnlyConverter>(converter),
#if NET8_0_OR_GREATER
converter => Assert.IsType<JsonStringEnumConverter<LogLevel>>(converter));
#elif NET6_0
converter => Assert.IsType<JsonStringEnumConverter>(converter));
#endif

Assert.Equal(JavaScriptEncoder.UnsafeRelaxedJsonEscaping, options.Encoder);

#if NET8_0_OR_GREATER
Assert.Empty(options.TypeInfoResolverChain);
#endif
}

[Fact]
public void SerializerOptions_ShouldUseSnakeCaseByDefault()
Expand Down Expand Up @@ -143,13 +174,28 @@ public void Serialize_UnknownType_ThrowsInvalidOperationException()
// Arrange
var unknownObject = new UnknownType();

RuntimeFeatureWrapper.SetIsDynamicCodeSupported(false);
// Act & Assert
var exception = Assert.Throws<JsonSerializerException>(() =>
PowertoolsLoggingSerializer.Serialize(unknownObject, typeof(UnknownType)));

Assert.Contains("is not known to the serializer", exception.Message);
Assert.Contains(typeof(UnknownType).ToString(), exception.Message);
}

[Fact]
public void Serialize_UnknownType_Should_Not_Throw_InvalidOperationException_When_Dynamic()
{
// Arrange
var unknownObject = new UnknownType{ SomeProperty = "Hello"};

RuntimeFeatureWrapper.SetIsDynamicCodeSupported(true);
// Act & Assert
var expected =
PowertoolsLoggingSerializer.Serialize(unknownObject, typeof(UnknownType));

Assert.Equal("{\"some_property\":\"Hello\"}", expected);
}

private class UnknownType
{
Expand All @@ -175,5 +221,6 @@ public void Dispose()
PowertoolsLoggingSerializer.ClearContext();
#endif
PowertoolsLoggingSerializer.ClearOptions();
RuntimeFeatureWrapper.Reset();
}
}
4 changes: 2 additions & 2 deletions version.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"Core": {
"Logging": "1.6.0",
"Logging": "1.6.1",
"Metrics": "1.7.1",
"Tracing": "1.5.1"
},
"Utilities": {
"Parameters": "1.3.0",
"Idempotency": "1.2.2",
"BatchProcessing": "1.1.2"
"BatchProcessing": "1.2.0"
}
}

0 comments on commit 4ab0fea

Please sign in to comment.