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

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

Open
Epicguru opened this issue Jan 5, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@Epicguru
Copy link

Epicguru commented Jan 5, 2024

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
@Epicguru Epicguru added the bug Something isn't working label Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants