GeezerVison is showing github.com — Strings are not interned on deserialization when the destination property type is object instead of string · Issue #1484 · MessagePack-CSharp/MessagePack-CSharp
Strings are not interned on deserialization when the destination property type is object instead of string #1484DescriptionBug description
We frequently have a List<Dictionary<string, object>> that we need to deserialize. This list of dictionaries often contains duplicate strings in the dictionary keys and values. The current version introduced a string interning feature that we want to take advantage of.
The dictionary keys are correctly interned on deserialization but, the dictionary object values (when they are strings) are not.
I guess this is due to the fact that the destination property type is object.
Repro steps
using FluentAssertions;
using MessagePack;
using MessagePack.Formatters;
using MessagePack.Resolvers;
using System;
using Xunit;
namespace Serialization.MessagePack.Test
{
[MessagePackObject]
public class TestStringInterningModel
{
[Key(0)]
public string Unique { get; set; }
[Key(1)]
public string SameString { get; set; }
[Key(2)]
public string SameStringAgain { get; set; }
[Key(3)]
public object AlsoTheSameStringButAsobject { get; set; }
}
public class UnitTest1
{
[Fact]
public void ShouldSerializeAndDeSerializeAsync_TestStringInterningModel()
{
var instance = new TestStringInterningModel
{
Unique = Guid.NewGuid().ToString(),
SameString = "AlwaysTheSameString",
SameStringAgain = "AlwaysTheSameString",
AlsoTheSameStringButAsobject = "AlwaysTheSameString"
};
var resolverWithInterning = CompositeResolver.Create(
new IMessagePackFormatter[] { new StringInterningFormatter() },
new IFormatterResolver[] { StandardResolver.Instance }
);
var messagePackOptions = MessagePackSerializerOptions.Standard.WithResolver(resolverWithInterning);
// Act
var binary = MessagePackSerializer.Serialize(instance, messagePackOptions);
var clone = MessagePackSerializer.Deserialize<TestStringInterningModel>(binary, messagePackOptions);
// Assert
clone.Should().BeEquivalentTo(instance);
ReferenceEquals(clone.SameString, clone.SameStringAgain).Should().BeTrue();
ReferenceEquals(clone.SameString, clone.AlsoTheSameStringButAsobject).Should().BeTrue(); //This should als be the same reference but it isn't.
}
}
}
Expected behavior
I expected that the property: 'AlsoTheSameStringButAsobject' also contained the interned string.
Actual behavior
The property: 'AlsoTheSameStringButAsobject' contains a newly allocated string.
- Version used: 2.4.35
- Runtime: .NET Core 3.1 and .NET 6.0
Additional context
Serialization.MessagePack.Test.UnitTest1.ShouldSerializeAndDeSerializeAsync_StringInterningModel
Source: UnitTest1.cs line 29
Duration: 247 ms
Message:
Expected ReferenceEquals(clone.SameString, clone.AlsoTheSameStringButAsobject) to be true, but found False.
Stack Trace:
XUnit2TestFramework.Throw(String message)
TestFrameworkProvider.Throw(String message)
DefaultAssertionStrategy.HandleFailure(String message)
AssertionScope.FailWith(Func`1 failReasonFunc)
AssertionScope.FailWith(Func`1 failReasonFunc)
AssertionScope.FailWith(String message, Object[] args)
BooleanAssertions`1.BeTrue(String because, Object[] becauseArgs)
UnitTest1.ShouldSerializeAndDeSerializeAsync_StringInterningModel() line 53
ActivityAArnott commented on Aug 19, 2022 sa-vg commented on Aug 21, 2022 Hi, can I take this one? I'm going to add the change you suggested and add few checks to MessagePack.Tests.StringInterningTests. AArnott commented on Aug 23, 2022
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|