Skip to content

Commit

Permalink
check IsDynamicCodeSupported to avoid TypeInfo in non AOT
Browse files Browse the repository at this point in the history
  • Loading branch information
hjgraca committed Oct 3, 2024
1 parent 5064e44 commit 1c2c997
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
*
* http://aws.amazon.com/apache2.0
*
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
Expand All @@ -18,6 +18,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using AWS.Lambda.Powertools.Common.Utils;

namespace AWS.Lambda.Powertools.Idempotency.Internal.Serializers;

Expand All @@ -43,12 +44,15 @@ private static void BuildDefaultOptions()
PropertyNameCaseInsensitive = true
};
#if NET8_0_OR_GREATER
_jsonOptions.TypeInfoResolverChain.Add(IdempotencySerializationContext.Default);
if (!RuntimeFeatureWrapper.IsDynamicCodeSupported)
{
_jsonOptions.TypeInfoResolverChain.Add(IdempotencySerializationContext.Default);
}

Check warning on line 50 in libraries/src/AWS.Lambda.Powertools.Idempotency/Internal/Serializers/IdempotencySerializer.cs

View check run for this annotation

Codecov / codecov/patch

libraries/src/AWS.Lambda.Powertools.Idempotency/Internal/Serializers/IdempotencySerializer.cs#L48-L50

Added lines #L48 - L50 were not covered by tests
#endif
}

#if NET8_0_OR_GREATER

/// <summary>
/// Adds a JsonTypeInfoResolver to the JsonSerializerOptions.
/// </summary>
Expand All @@ -61,7 +65,7 @@ internal static void AddTypeInfoResolver(JsonSerializerContext context)
BuildDefaultOptions();
_jsonOptions.TypeInfoResolverChain.Add(context);
}

/// <summary>
/// Gets the JsonTypeInfo for a given type.
/// </summary>
Expand Down Expand Up @@ -91,6 +95,12 @@ internal static string Serialize(object value, Type inputType)
#if NET6_0
return JsonSerializer.Serialize(value, _jsonOptions);
#else
if (RuntimeFeatureWrapper.IsDynamicCodeSupported)
{
#pragma warning disable
return JsonSerializer.Serialize(value, _jsonOptions);
}

return JsonSerializer.Serialize(value, GetTypeInfo(inputType));

Check warning on line 104 in libraries/src/AWS.Lambda.Powertools.Idempotency/Internal/Serializers/IdempotencySerializer.cs

View check run for this annotation

Codecov / codecov/patch

libraries/src/AWS.Lambda.Powertools.Idempotency/Internal/Serializers/IdempotencySerializer.cs#L104

Added line #L104 was not covered by tests
#endif
}
Expand All @@ -106,6 +116,12 @@ internal static T Deserialize<T>(string value)
#if NET6_0
return JsonSerializer.Deserialize<T>(value,_jsonOptions);
#else
if (RuntimeFeatureWrapper.IsDynamicCodeSupported)
{
#pragma warning disable
return JsonSerializer.Deserialize<T>(value, _jsonOptions);
}

return (T)JsonSerializer.Deserialize(value, GetTypeInfo(typeof(T)));

Check warning on line 125 in libraries/src/AWS.Lambda.Powertools.Idempotency/Internal/Serializers/IdempotencySerializer.cs

View check run for this annotation

Codecov / codecov/patch

libraries/src/AWS.Lambda.Powertools.Idempotency/Internal/Serializers/IdempotencySerializer.cs#L125

Added line #L125 was not covered by tests
#endif
}
Expand Down

0 comments on commit 1c2c997

Please sign in to comment.