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: Deserializing object with multiple private fields with the same name fails. #91

Open
SJMakin opened this issue Jan 29, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@SJMakin
Copy link

SJMakin commented Jan 29, 2021

Describe the bug
Deserialization fails when processing an object with multiple private fields with the same name.

    public class Base
    {
        private bool _dirty;
    }

    public class Child : Base
    {
        private bool _dirty;
    }

    [Test]
    public void InhitanceWorks()
    {
        var config = new SerializerConfig {DefaultTargets = TargetMember.AllFields, VersionTolerance = {Mode = VersionToleranceMode.Standard}};
        var ceras = new CerasSerializer(config);
        var data = ceras.Serialize(new Child());
        var result = ceras.Deserialize<Child>(data);
    }

Additional info

Message: 
    System.ArgumentException : An item with the same key has already been added.
  Stack Trace: 
    ThrowHelper.ThrowArgumentException(ExceptionResource resource)
    Dictionary`2.Insert(TKey key, TValue value, Boolean add)
    Dictionary`2.Add(TKey key, TValue value)
    DynamicFormatter`1.GenerateDeserializer(CerasSerializer ceras, Schema schema, Boolean isSchemaFormatter, Boolean isStatic) line 251
    SchemaDynamicFormatter`1.ActivateSchema(Schema schema) line 150
    ISchemaTaintedFormatter.OnSchemaChanged(TypeMetaData meta) line 83
    CerasSerializer.ActivateSchemaOverride(Type type, Schema schema) line 1029
    SchemaDynamicFormatter`1.Deserialize(Byte[] buffer, Int32& offset, T& value) line 67
    ReferenceFormatter`1.Deserialize(Byte[] buffer, Int32& offset, T& value) line 288
    CerasSerializer.Deserialize[T](T& value, Byte[] buffer, Int32& offset, Int32 expectedReadLength) line 524
    CerasSerializer.Deserialize[T](Byte[] buffer) line 478
    SerializationTests.InhitanceWorks() line 47

Platform

  • Which version of Ceras are you using? net4.5
@SJMakin SJMakin added the bug Something isn't working label Jan 29, 2021
@rikimaru0345
Copy link
Owner

IIRC this bug comes from an oversight in the design of Ceras. (read: I just didn't think of that case :P)
But it is possible to fix it in multiple ways.
You could rename the field by using attributes. But I imagine that would become a hassle pretty fast.

It would probably be better to use config.OnConfigNewType, then go through each member, and if you ever encounter a member named _dirty, you'd rename it to something like "ContainingClass._dirty", for example: Base._dirty or Child._dirty.

@SJMakin
Copy link
Author

SJMakin commented Apr 11, 2021 via email

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