Skip to content

Bug: Deserialization fails when a field default value is of a different value type than the one being deserialized #111

@Epicguru

Description

@Epicguru

It doesn't look like this repo is actively maintained but I will post this in the hope that someone can fix this at some point.

Describe the bug
When boxed types are deserialized into a field, if that field has an existing (or default) value that is not of the same type, an exception is thrown. Please see the minimal example below.
Expected: The object with the field should be deserialized correctly.
Actual: An exception is thrown. See stack trace below.

How to reproduce the bug
SerializerConfig is the default one.
I have also tried 'Serialize References' with the same result.

[Fact]
public void ShowError()
{
    var toSerialize = new MyClass
    {
        obj = 555, // Box this integer.
    };

    // Use default config. Using serialize references also throws the same error.
    var serializer = new CerasSerializer(); 
    byte[] bytes = serializer.Serialize(toSerialize);

    var deserialized = serializer.Deserialize<MyClass>(bytes);
    deserialized.Should().BeEquivalentTo(toSerialize);
}

public class MyClass
{
    public object? obj = "Some string";
}

Result:

System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'.

Thrown on line 288 in ReferenceFormatter.cs:
https://github.com/rikimaru0345/Ceras/blob/de8798b368ea3983e9113be9a8db51ee6e9eda26/src/Ceras/Formatters/ReferenceFormatter.cs#L288C7-L288C7

Platform

  • Which version of Ceras are you using? Ceras 4.1.7 from NuGet, running on .NET 8.0
  • Are you using Unity? No

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions