GithubHelp home page GithubHelp logo

Comments (6)

sync-by-unito avatar sync-by-unito commented on May 28, 2024

➤ PM Bot commented:

Jira ticket: RNET-1128

from realm-dotnet.

papafe avatar papafe commented on May 28, 2024

Hi @softlion, thanks for your report.
The exception that you're seeing should be raised when trying to get a SyncSession on a local realm. From the code snippets that you posted it seems you're indeed creating a local realm, but I don't see you accessing SyncSession anywhere, so I'm not sure how it could happen. I also took a look at our source code and we access SyncSession only with a synchronized realm (when opening the realm with FlexibleSyncConfiguration or PartitionSyncConfiguration), so it shouldn't be possible.

Is that the whole stack trace you're getting? From that I can't see where the SyncSession is accessed.
Would it be possible to for you to create a repro case and send it to us? This would be really helpful

from realm-dotnet.

softlion avatar softlion commented on May 28, 2024

That triggers the issue.

The key that triggers it is the json serializer (using System.Text.Json;)

    [Fact]
    public async Task TriggerRealmBug()
    {
        using var db = Realm.GetInstance(new InMemoryConfiguration("unittests"));

        var walletId = "someId";
        var wallet = new DbWallet { WalletId = walletId };
        await db.WriteAsync(() => { db.Add(wallet); });
        
        wallet = db.Find<DbWallet>(walletId);
        await db.WriteAsync(() =>  { wallet.Alias = "some new value"; });
        
        var dbWallets = db.All<DbWallet>().ToList();
        Assert.Single(dbWallets);

        //Crash here
        var jsonWallets = JsonSerializer.Serialize(db.All<DbWallet>().ToList());
    }
 public partial class DbWallet : IRealmObject
{
    [PrimaryKey]
    public string WalletId { get; set; }
    public string? Alias { get; set; }
}

I worked around using a new domain object and mapperly.
But that used to work, as I am only republishing an existing app with the latest nugets.

from realm-dotnet.

papafe avatar papafe commented on May 28, 2024

Ok, this makes sense. It seems that System.Text.Json is ignoring the IgnoreDataMember attribute we put on the source generated class properties so that they're ignored by the serialization. During the serialization System.Text.Json is trying to access DbWallet.Realm.SyncSession and so the exception is raised.

It seems we need to add the JsonIgnore attribute to be compatible with System.Text.Json. In the meanwhile, you can add special support for IgnoreDataMember as explained here:

//NOTE: This is copied from the link above
var options = new JsonSerializerOptions
{
    TypeInfoResolver = new DefaultJsonTypeInfoResolver
    {
        Modifiers = { DetectIgnoreDataMemberAttribute }
    }
};

JsonSerializer.Serialize(new MyPoco(), options); // {"Value":3}

static void DetectIgnoreDataMemberAttribute(JsonTypeInfo typeInfo)
{
    if (typeInfo.Kind != JsonTypeInfoKind.Object)
        return;

    foreach (JsonPropertyInfo propertyInfo in typeInfo.Properties)
    {
        if (propertyInfo.AttributeProvider is ICustomAttributeProvider provider &&
            provider.IsDefined(typeof(IgnoreDataMemberAttribute), inherit: true))
        {
            // Disable both serialization and deserialization 
            // by unsetting getter and setter delegates
            propertyInfo.Get = null;
            propertyInfo.Set = null;
        }
    }
}

To be honest I am surprised this was working before. You said you updated your nuget packages and this stopped working. What nuget packages did you exactly update?

from realm-dotnet.

nirinchev avatar nirinchev commented on May 28, 2024

Edit: Looks like @papafe beat me to this 😅 This is just repeating what Ferdinando is saying, but with less detail.

Looks like the json serializer is trying to serialize the Realm property on DBWallet, even though it's annotated with IgnoreDataMember. I don't think we can polyfill JsonIgnoreAttribute, so we'll probably need to apply it if possible based on the compilation target.

from realm-dotnet.

softlion avatar softlion commented on May 28, 2024

Ok, this makes sense. It seems that System.Text.Json is ignoring the IgnoreDataMember attribute we put on the source generated class properties so that they're ignored by the serialization. During the serialization System.Text.Json is trying to access DbWallet.Realm.SyncSession and so the exception is raised.

It seems we need to add the JsonIgnore attribute to be compatible with System.Text.Json. In the meanwhile, you can add special support for IgnoreDataMember as explained here:

//NOTE: This is copied from the link above
var options = new JsonSerializerOptions
{
    TypeInfoResolver = new DefaultJsonTypeInfoResolver
    {
        Modifiers = { DetectIgnoreDataMemberAttribute }
    }
};

JsonSerializer.Serialize(new MyPoco(), options); // {"Value":3}

static void DetectIgnoreDataMemberAttribute(JsonTypeInfo typeInfo)
{
    if (typeInfo.Kind != JsonTypeInfoKind.Object)
        return;

    foreach (JsonPropertyInfo propertyInfo in typeInfo.Properties)
    {
        if (propertyInfo.AttributeProvider is ICustomAttributeProvider provider &&
            provider.IsDefined(typeof(IgnoreDataMemberAttribute), inherit: true))
        {
            // Disable both serialization and deserialization 
            // by unsetting getter and setter delegates
            propertyInfo.Get = null;
            propertyInfo.Set = null;
        }
    }
}

To be honest I am surprised this was working before. You said you updated your nuget packages and this stopped working. What nuget packages did you exactly update?

maui, realm, json, and some others.
It may not have worked previously though, it may only not have reported the crash.
The tools have improved and now report the crash.

Anyway now we found the issue.

from realm-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.