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

Unable to patch dictionary with a property #18465

Open
loraderon opened this issue May 2, 2024 · 1 comment
Open

Unable to patch dictionary with a property #18465

loraderon opened this issue May 2, 2024 · 1 comment

Comments

@loraderon
Copy link

When patching a dictionary with a value from a property instead of a value in a variable a InvalidCastException or NotSupportedException is thrown.

// ✅
[RavenTheory(RavenTestCategory.ClientApi | RavenTestCategory.Patching)]
[RavenData(DatabaseMode = RavenDatabaseMode.All)]
public void PatchOnDictionaryShouldWork(Options options)
{
    using (var store = GetDocumentStore(options))
    {
        string id;
        using (var session = store.OpenSession())
        {
            var entity = new Foo { Dict = new() };
            session.Store(entity);
            session.SaveChanges();
            id = session.Advanced.GetDocumentId(entity);

        }

        var key = "key";
        using (var session = store.OpenSession())
        {
            session.Advanced.Patch<Foo, string>(id, x => x.Dict[key], string.Empty);
            session.SaveChanges();
            Assert.Equal(string.Empty, session.Load<Foo>(id).Dict[key]);
        }
    }
}

// ❌ System.InvalidCastException : Unable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.ConstantExpression'.
[RavenTheory(RavenTestCategory.ClientApi | RavenTestCategory.Patching)]
[RavenData(DatabaseMode = RavenDatabaseMode.All)]
public void PatchOnDictionaryWithAnonymousPropertyShouldWork(Options options)
{
    using (var store = GetDocumentStore(options))
    {
        string id;
        using (var session = store.OpenSession())
        {
            var entity = new Foo { Dict = new(), };
            session.Store(entity);
            session.SaveChanges();
            id = session.Advanced.GetDocumentId(entity);

        }

        var request = new { Key = "key" };
        using (var session = store.OpenSession())
        {
            session.Advanced.Patch<Foo, string>(id, x => x.Dict[request.Key], string.Empty);
            session.SaveChanges();
            Assert.Equal(string.Empty, session.Load<Foo>(id).Dict[request.Key]);
        }
    }
}

// ❌ System.NotSupportedException : The used constant value is not supported: `value(FastTests.Client.Documents.Patch+FooRequest)` (FooRequest)
[RavenTheory(RavenTestCategory.ClientApi | RavenTestCategory.Patching)]
[RavenData(DatabaseMode = RavenDatabaseMode.All)]
public void PatchOnDictionaryWithTypePropertyShouldWork(Options options)
{
    using (var store = GetDocumentStore(options))
    {
        string id;
        using (var session = store.OpenSession())
        {
            var entity = new Foo { Dict = new(), };
            session.Store(entity);
            session.SaveChanges();
            id = session.Advanced.GetDocumentId(entity);

        }

        var request = new FooRequest { Key = "key" };
        using (var session = store.OpenSession())
        {
            session.Advanced.Patch<Foo, string>(id, x => x.Dict[request.Key], string.Empty);
            session.SaveChanges();
            Assert.Equal(string.Empty, session.Load<Foo>(id).Dict[request.Key]);
        }
    }
}

public class Foo
{
    public Dictionary<string, string> Dict { get; set; }
}
public class FooRequest
{
    public string Key { get; set; }
}
@maciejaszyk
Copy link
Member

Hi, thanks for the tests. I've created a ticket for it: https://issues.hibernatingrhinos.com/issue/RavenDB-22347/Unable-to-patch-dictionary-with-a-property

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants