stride3d / stride3d/stride

[AssemblyProcessor] Generic subclass of a generic class doesn't have a parent serializer

Open
#1,047 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-Build bug
Dominant language
C#
Stars
7.8k
Forks
1.2k
Avg merge
2d 17h
Merged PRs (30d)
49

Description

Version: any

Platform(s): N/A

Describe the bug
When a generic class is to be serialized, if it has a generic base class it will not have a serializer for the parent emitted.

[DataContract(Inherited = true)]
public class X0<T>
{
    public int A {get;set;}
}

public class X1<T> : X0<T>
{
    public int B {get;set;}
}

Generated serializer

public sealed class TestGame_X1Serializer<T> : ClassDataSerializer<X1<T>>
{
    private DataSerializer<int> System_Int32Serializer;

    public TestGame_X1Serializer() => base..Ector();

    public virtual void Initialize(SerializerSelector serializerSelector) => this.System_Int32Serializer = MemberSerializer<int>.Create(serializerSelector, true);

    public virtual void Serialize(ref X1<T> obj, ArchiveMode mode, SerializationStream stream)
    {
        //...
    }
}

Expected serializer

public sealed class TestGame_X1Serializer<T> : ClassDataSerializer<X1<T>>
{
    private DataSerializer<X0<T>> parentSerializer;
    private DataSerializer<int> System_Int32Serializer;

    public TestGame_X1Serializer() => base..ctor();

    public virtual void Initialize(SerializerSelector serializerSelector)
    {
        this.parentSerializer = serializerSelector.GetSerializer<X0<T>>();
        this.System_Int32Serializer = MemberSerializer<int>.Create(serializerSelector, true);
    }

    public virtual void Serialize(ref X1<T> obj, ArchiveMode mode, SerializationStream stream)
    {
        //...
    }
}

Additional context
The issue seems to be somewhere in the Cecil emit of the serializers. To prevent the issue from happening parent serializer is not set on classes with a parent that has a generic (non instantiated) argument.
https://github.com/stride3d/stride/blob/9219cbbb9de7a238060d97cf1cfbbf759120aa23/sources/core/Stride.Core.AssemblyProcessor/Serializers/CecilSerializerContext.cs#L200
Removing this check will cause the exception during
https://github.com/stride3d/stride/blob/a3e984aac1151f8c55f00035bd817ba6a5b4fe5b/sources/core/Stride.Core.AssemblyProcessor/AssemblyProcessorApp.cs#L143

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in sources/core/Stride.Core.AssemblyProcessor/Serializers/CecilSerializerContext.cs around line 200 and inspect the check that prevents setting a parent serializer for generic arguments. Then examine sources/core/Stride.Core.AssemblyProcessor/AssemblyProcessorApp.cs around line 143 and the generated serializer in the issue. Done means the generic subclass emits and initializes a parent serializer without causing the reported exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.