1515
1616using System ;
1717using System . Collections . Generic ;
18+ using System . Runtime . CompilerServices ;
1819using System . Text . Encodings . Web ;
1920using System . Text . Json ;
2021using System . Text . Json . Serialization ;
2122using Amazon . Lambda . Serialization . SystemTextJson ;
23+ using AWS . Lambda . Powertools . Common . Utils ;
2224using AWS . Lambda . Powertools . Logging . Internal ;
2325using AWS . Lambda . Powertools . Logging . Internal . Converters ;
2426using AWS . Lambda . Powertools . Logging . Serializers ;
@@ -48,8 +50,10 @@ public void SerializerOptions_ShouldNotBeNull()
4850 [ Fact ]
4951 public void SerializerOptions_ShouldHaveCorrectDefaultSettings ( )
5052 {
51- var options = PowertoolsLoggingSerializer . GetSerializerOptions ( ) ;
52-
53+ RuntimeFeatureWrapper . SetIsDynamicCodeSupported ( false ) ;
54+
55+ var options = PowertoolsLoggingSerializer . GetSerializerOptions ( ) ;
56+
5357 Assert . Collection ( options . Converters ,
5458 converter => Assert . IsType < ByteArrayConverter > ( converter ) ,
5559 converter => Assert . IsType < ExceptionConverter > ( converter ) ,
@@ -70,6 +74,33 @@ public void SerializerOptions_ShouldHaveCorrectDefaultSettings()
7074 resolver => Assert . IsType < PowertoolsLoggingSerializationContext > ( resolver ) ) ;
7175#endif
7276 }
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+ }
73104
74105 [ Fact ]
75106 public void SerializerOptions_ShouldUseSnakeCaseByDefault ( )
@@ -143,13 +174,28 @@ public void Serialize_UnknownType_ThrowsInvalidOperationException()
143174 // Arrange
144175 var unknownObject = new UnknownType ( ) ;
145176
177+ RuntimeFeatureWrapper . SetIsDynamicCodeSupported ( false ) ;
146178 // Act & Assert
147179 var exception = Assert . Throws < JsonSerializerException > ( ( ) =>
148180 PowertoolsLoggingSerializer . Serialize ( unknownObject , typeof ( UnknownType ) ) ) ;
149181
150182 Assert . Contains ( "is not known to the serializer" , exception . Message ) ;
151183 Assert . Contains ( typeof ( UnknownType ) . ToString ( ) , exception . Message ) ;
152184 }
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+ }
153199
154200 private class UnknownType
155201 {
@@ -175,5 +221,6 @@ public void Dispose()
175221 PowertoolsLoggingSerializer . ClearContext ( ) ;
176222#endif
177223 PowertoolsLoggingSerializer . ClearOptions ( ) ;
224+ RuntimeFeatureWrapper . Reset ( ) ;
178225 }
179226}
0 commit comments