GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #1484

New issue
Copy link
New issue
Copy link
Closed
#1487
Closed
Strings are not interned on deserialization when the destination property type is object instead of string#1484
#1487
Copy link
Assignees
sa-vg
Labels
good first issue
Milestone
v2.5

Description

@Phoenix359
Phoenix359
opened on Aug 18, 2022
Issue body actions

Bug 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

Activity

  1. AArnott commented on Aug 19, 2022

    @AArnott
    AArnott
    on Aug 19, 2022
    Collaborator
    More actions

    That's an interesting case. I think this can be done by updating this code:

    https://github.com/neuecc/MessagePack-CSharp/blob/94d86190bc8c30e98dae0220c0bce54cdfe48494/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/PrimitiveObjectFormatter.cs#L265-L266

    It would need to call options.Resolver.GetFormatter<string>().Deserialize(ref reader, options), I think.

    Anyone want to pick this up?

  2. AArnott
    added
    help wanted
    good first issue
    on Aug 19, 2022
  3. sa-vg commented on Aug 21, 2022

    @sa-vg
    sa-vg
    on Aug 21, 2022
    Contributor
    More actions

    Hi, can I take this one? I'm going to add the change you suggested and add few checks to MessagePack.Tests.StringInterningTests.

    Reacted by Andrew Arnott
  4. AArnott
    assigned
    sa-vg
    on Aug 23, 2022
  5. AArnott
    removed
    help wanted
    on Aug 23, 2022
  6. AArnott commented on Aug 23, 2022

    @AArnott
    AArnott
    on Aug 23, 2022
    Collaborator
    More actions

    Sure. Thanks, @sa-vg

  7. AArnott
    linked a pull request that will close this issueSupport string interning when deserializing string as object. #1487on Sep 6, 2022
  8. AArnott
    added this to the v2.5 milestone on Sep 6, 2022
  9. AArnott
    closed this as completedon Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @sa-vg
    sa-vg

Labels

good first issue

Type

No type

Projects

No projects

    Milestone

    • v2.5

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.