You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Deserializing an object of a class that contains an initialized HashSet with DefaultLambdaJsonSerializer throws a Amazon.Lambda.Serialization.SystemTextJson.JsonSerializerException.
I'm expecting no exception to be thrown. Deserializing the exact same thing using regular System.Text.Json.JsonSerializer works fine.
Reproduction Steps
Run the following program:
publicclassExample{publicHashSet<int>Integers{get;set;}=new();}publicclassProgram{publicstaticasyncTaskMain(){varstream=newMemoryStream();varexample=newExample();awaitJsonSerializer.SerializeAsync(stream,example);// This throws. Same error whichever constructor is used.stream.Position=0;vardefaultLambdaJsonSerializer=newDefaultLambdaJsonSerializer();Example?awsExample=defaultLambdaJsonSerializer.Deserialize<Example>(stream);// This works fine.stream.Position=0;Example?regularStjExample=awaitJsonSerializer.DeserializeAsync<Example>(stream);}}
Stacktrace:
Amazon.Lambda.Serialization.SystemTextJson.JsonSerializerException
HResult=0x80131500
Message=Error converting the Lambda event JSON payload to type TestingDefaultSystemTextJsonSerializer.Example: Unable to cast object of type 'System.Collections.Generic.HashSet`1[System.Int32]' to type 'System.Collections.IList'.
Source=Amazon.Lambda.Serialization.SystemTextJson
StackTrace:
at Amazon.Lambda.Serialization.SystemTextJson.AbstractLambdaJsonSerializer.Deserialize[T](Stream requestStream)
at TestingDefaultSystemTextJsonSerializer.Program.<Main>d__0.MoveNext() in C:\Users\work\source\repos\TestingDefaultSystemTextJsonSerializer\TestingDefaultSystemTextJsonSerializer\Program.cs:line 21
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
InvalidCastException: Unable to cast object of type 'System.Collections.Generic.HashSet`1[System.Int32]' to type 'System.Collections.IList'.
The same error occurs if using any collection that cannot be cast into an IList, such as System.Net.CookieCollection.
Removing the initialization of the HashSet/Collection fixes the issue, i.e:
Reproducible with target framework netcoreapp3.1 using the below .csproj file (kindly note the use of <LangVersion>9.0</LangVersion> since the target types object creation is only available in C# 9.0 or later):
Even though the issue is reproducible in .NET 3.1 Core, .NET Core 3.1 had reached end of life per https://dotnet.microsoft.com/en-us/download/dotnet/3.1. Based on discussion with team, closing this issue since it is not reproducible in .NET 6 (or later).
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
Description
Deserializing an object of a class that contains an initialized
HashSet
withDefaultLambdaJsonSerializer
throws aAmazon.Lambda.Serialization.SystemTextJson.JsonSerializerException
.I'm expecting no exception to be thrown. Deserializing the exact same thing using regular
System.Text.Json.JsonSerializer
works fine.Reproduction Steps
Run the following program:
Stacktrace:
The same error occurs if using any collection that cannot be cast into an
IList
, such asSystem.Net.CookieCollection
.Removing the initialization of the
HashSet
/Collection
fixes the issue, i.e:Nb!
Tested on dotnetcore3.1, net5.0 and net6.0.
The error ONLY occurs on dotnetcore3.1.
This does not seem to be an issue on net5.0 or net6.0.
Environment
Resolution
This is a 🐛 bug-report
The text was updated successfully, but these errors were encountered: