15
15
16
16
using System ;
17
17
using System . Collections . Generic ;
18
+ using System . Runtime . CompilerServices ;
18
19
using System . Text . Encodings . Web ;
19
20
using System . Text . Json ;
20
21
using System . Text . Json . Serialization ;
21
22
using Amazon . Lambda . Serialization . SystemTextJson ;
23
+ using AWS . Lambda . Powertools . Common . Utils ;
22
24
using AWS . Lambda . Powertools . Logging . Internal ;
23
25
using AWS . Lambda . Powertools . Logging . Internal . Converters ;
24
26
using AWS . Lambda . Powertools . Logging . Serializers ;
@@ -48,8 +50,10 @@ public void SerializerOptions_ShouldNotBeNull()
48
50
[ Fact ]
49
51
public void SerializerOptions_ShouldHaveCorrectDefaultSettings ( )
50
52
{
51
- var options = PowertoolsLoggingSerializer . GetSerializerOptions ( ) ;
52
-
53
+ RuntimeFeatureWrapper . SetIsDynamicCodeSupported ( false ) ;
54
+
55
+ var options = PowertoolsLoggingSerializer . GetSerializerOptions ( ) ;
56
+
53
57
Assert . Collection ( options . Converters ,
54
58
converter => Assert . IsType < ByteArrayConverter > ( converter ) ,
55
59
converter => Assert . IsType < ExceptionConverter > ( converter ) ,
@@ -70,6 +74,33 @@ public void SerializerOptions_ShouldHaveCorrectDefaultSettings()
70
74
resolver => Assert . IsType < PowertoolsLoggingSerializationContext > ( resolver ) ) ;
71
75
#endif
72
76
}
77
+
78
+ [ Fact ]
79
+ public void SerializerOptions_ShouldHaveCorrectDefaultSettings_WhenDynamic ( )
80
+ {
81
+ RuntimeFeatureWrapper . SetIsDynamicCodeSupported ( true ) ;
82
+
83
+ var options = PowertoolsLoggingSerializer . GetSerializerOptions ( ) ;
84
+
85
+ Assert . Collection ( options . Converters ,
86
+ converter => Assert . IsType < ByteArrayConverter > ( converter ) ,
87
+ converter => Assert . IsType < ExceptionConverter > ( converter ) ,
88
+ converter => Assert . IsType < MemoryStreamConverter > ( converter ) ,
89
+ converter => Assert . IsType < ConstantClassConverter > ( converter ) ,
90
+ converter => Assert . IsType < DateOnlyConverter > ( converter ) ,
91
+ converter => Assert . IsType < TimeOnlyConverter > ( converter ) ,
92
+ #if NET8_0_OR_GREATER
93
+ converter => Assert . IsType < JsonStringEnumConverter < LogLevel > > ( converter ) ) ;
94
+ #elif NET6_0
95
+ converter => Assert . IsType < JsonStringEnumConverter > ( converter ) ) ;
96
+ #endif
97
+
98
+ Assert. Equal ( JavaScriptEncoder . UnsafeRelaxedJsonEscaping , options . Encoder ) ;
99
+
100
+ #if NET8_0_OR_GREATER
101
+ Assert . Empty ( options . TypeInfoResolverChain ) ;
102
+ #endif
103
+ }
73
104
74
105
[ Fact ]
75
106
public void SerializerOptions_ShouldUseSnakeCaseByDefault ( )
@@ -143,13 +174,28 @@ public void Serialize_UnknownType_ThrowsInvalidOperationException()
143
174
// Arrange
144
175
var unknownObject = new UnknownType ( ) ;
145
176
177
+ RuntimeFeatureWrapper . SetIsDynamicCodeSupported ( false ) ;
146
178
// Act & Assert
147
179
var exception = Assert . Throws < JsonSerializerException > ( ( ) =>
148
180
PowertoolsLoggingSerializer . Serialize ( unknownObject , typeof ( UnknownType ) ) ) ;
149
181
150
182
Assert . Contains ( "is not known to the serializer" , exception . Message ) ;
151
183
Assert . Contains ( typeof ( UnknownType ) . ToString ( ) , exception . Message ) ;
152
184
}
185
+
186
+ [ Fact ]
187
+ public void Serialize_UnknownType_Should_Not_Throw_InvalidOperationException_When_Dynamic ( )
188
+ {
189
+ // Arrange
190
+ var unknownObject = new UnknownType { SomeProperty = "Hello" } ;
191
+
192
+ RuntimeFeatureWrapper . SetIsDynamicCodeSupported ( true ) ;
193
+ // Act & Assert
194
+ var expected =
195
+ PowertoolsLoggingSerializer . Serialize ( unknownObject , typeof ( UnknownType ) ) ;
196
+
197
+ Assert . Equal ( "{\" some_property\" :\" Hello\" }" , expected ) ;
198
+ }
153
199
154
200
private class UnknownType
155
201
{
@@ -175,5 +221,6 @@ public void Dispose()
175
221
PowertoolsLoggingSerializer . ClearContext ( ) ;
176
222
#endif
177
223
PowertoolsLoggingSerializer . ClearOptions ( ) ;
224
+ RuntimeFeatureWrapper . Reset ( ) ;
178
225
}
179
226
}
0 commit comments