Skip to content
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

DefaultLambdaJsonSerializer throws when deserializing object with initialized HashSet #1037

Closed
1 task
emahnetmore opened this issue Dec 30, 2021 · 3 comments
Closed
1 task
Assignees
Labels
bug This issue is a bug. module/lambda-client-lib p2 This is a standard priority issue queued

Comments

@emahnetmore
Copy link

Description

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:

public class Example
{
    public HashSet<int> Integers { get; set; } = new();
}

public class Program
{
    public static async Task Main()
    {
        var stream = new MemoryStream();
        var example = new Example();
        await JsonSerializer.SerializeAsync(stream, example);

        // This throws. Same error whichever constructor is used.
        stream.Position = 0;
        var defaultLambdaJsonSerializer = new DefaultLambdaJsonSerializer();
        Example? awsExample = defaultLambdaJsonSerializer.Deserialize<Example>(stream);

        // This works fine.
        stream.Position = 0;
        Example? regularStjExample = await JsonSerializer.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:

public class Example
{
    public HashSet<int> Integers { get; set; }
}

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

  • Build Version: 2.2.0
  • OS Info: Windows 11 Pro
  • Build Environment: Visual Studio 2022
  • Targeted .NET Platform: dotnetcore3.1

Resolution

  • 👋 I can/would-like-to implement a fix for this problem myself

This is a 🐛 bug-report

@emahnetmore emahnetmore added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 30, 2021
@ashishdhingra
Copy link
Contributor

ashishdhingra commented Jan 6, 2022

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):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>9.0</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="Amazon.Lambda.Serialization.SystemTextJson" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.2.0" />
  </ItemGroup>
</Project>

@ashishdhingra ashishdhingra added needs-review B and removed needs-triage This issue or PR still needs to be triaged. labels Jan 6, 2022
@ashishdhingra ashishdhingra added queued p2 This is a standard priority issue and removed B labels Nov 2, 2022
@ashishdhingra ashishdhingra self-assigned this Aug 21, 2024
@ashishdhingra
Copy link
Contributor

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).

@ashishdhingra ashishdhingra closed this as not planned Won't fix, can't repro, duplicate, stale Aug 21, 2024
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/lambda-client-lib p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

3 participants