Skip to content

chore: Remove TypeInfoResolver from non AOT mode. #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
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 @@
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 @@
_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);
}

Check warning on line 173 in libraries/src/AWS.Lambda.Powertools.Logging/Serializers/PowertoolsLoggingSerializer.cs

View check run for this annotation

Codecov / codecov/patch

libraries/src/AWS.Lambda.Powertools.Logging/Serializers/PowertoolsLoggingSerializer.cs#L171-L173

Added lines #L171 - L173 were not covered by tests
}
#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"
}
}
Loading