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

Serialization Exception When Using ToArrayAsync After GroupBy and CountGroupMembers in Redis OM .NET8 #511

Open
osleyhc opened this issue Jan 8, 2025 · 1 comment

Comments

@osleyhc
Copy link

osleyhc commented Jan 8, 2025

Encountering a serialization exception when executing an asynchronous aggregation query using GroupBy and CountGroupMembers, followed by ToArrayAsync() or ToListAsync(). The exception suggests that the serialization process is attempting to handle an unsupported type, leading to a failure.

Steps to Reproduce:

Define the Data Model:

using Redis.OM.Modeling;

[Document(StorageType = StorageType.Json)]
public class Car
{
    [RedisIdField]
    public string Id { get; set; }

    [Indexed]
    public string Mk { get; set; } // Make

    [Indexed]
    public string Md { get; set; } // Model

    // Additional properties...
}

Set Up the Aggregation Query:

var provider = new RedisConnectionProvider("redis://localhost:6379");
var aggregationSet = provider.AggregationSet<Car>();

var filteredAggregations = aggregationSet
    .GroupBy(x => new { x.Mk, x.Md })
    .CountGroupMembers();

Execute the Query Asynchronously:

var results = await filteredAggregations.ToArrayAsync();
Observed Behavior: Executing the above code results in the following exception:

System.InvalidOperationException: The type 'System.Threading.ExecutionContext&' of property 'Context' on type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[Namespace.ClassName,System.Runtime.CompilerServices.IAsyncStateMachine]' is invalid for serialization or deserialization because it is a pointer type, is a ref struct, or contains generic parameters that have not been replaced by specific types.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_CannotSerializeInvalidType(Type typeToConvert, Type declaringType, MemberInfo memberInfo)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.CreatePropertyInfo(JsonTypeInfo typeInfo, Type typeToConvert, MemberInfo memberInfo, JsonSerializerOptions options, Boolean shouldCheckForRequiredKeyword, Boolean hasJsonIncludeAttribute)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.AddMembersDeclaredBySuperType(JsonTypeInfo typeInfo, Type currentType, Boolean constructorHasSetsRequiredMembersAttribute, PropertyHierarchyResolutionState& state)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.PopulateProperties(JsonTypeInfo typeInfo)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.CreateTypeInfoCore(Type type, JsonConverter converter, JsonSerializerOptions options)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(Type type)
at System.Text.Json.JsonSerializerOptions.CachingContext.CreateCacheEntry(Type type, CachingContext context)
--- End of stack trace from previous location ---

@slorello89
Copy link
Member

Hi @osleyhc That should not even compile, you need to access the RecordShell in the GroupBy. The RecordShell is a pre-materilized copy of the record so you can use it in your LINQ queries. You need to access those records via the RecordShell and then call ToListAsync to materialize it.

this works:

var aggregationSet = provider.AggregationSet<Car>();
var filteredAggregations =  await aggregationSet
    .GroupBy(x => new { x.RecordShell.Mk, x.RecordShell.Md })
    .CountGroupMembers().ToListAsync();
    
Console.WriteLine(filteredAggregations.First()["COUNT"].ToString());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants