RavenDB: How to prevent $type being saved to a dynamic typed property?

309 views Asked by At

Is there a way to prevent a $type property being added when I save my dynamic type values?

When I save this:

new Activity {
    Name = "FormFieldDeleted",
    Body =  new {
        MyDeletedFormField(),
        MyCompleteForm()
    }
}

I get this

<>f__AnonymousType1`2[[MyProject.Domains.Forms.Models.FormField, MyProject.Domains.Forms],[MyProject.Domains.Forms.Entities.FormRegistration, MyProject.Domains.Forms]], MyProject.Api.Forms

But when I try to fetch this saved entity, it crashes with the exception below. I know it's missing a project reference, but I really don't want to add that reference (I don't want to reference an API from a console app). It's better for me to just prevent the $type property.

/usr/local/share/dotnet/dotnet path/MyProject/MyProject/src/MyProject.Tasks.MapActivities/bin/Debug/netcoreapp3.1/MyProject.Tasks.MapActivities.dll
Unhandled exception. System.InvalidOperationException: Could not convert document 31317d58-db9e-4f60-8dee-b8593f3e06c0 to entity of type MyProject.Domains.Core.Entities.Activity
 ---> Newtonsoft.Json.JsonSerializationException: Error resolving type specified in JSON '<>f__AnonymousType1`2[[MyProject.Domains.Forms.Models.FormField, MyProject.Domains.Forms],[MyProject.Domains.Forms.Entities.FormRegistration, MyProject.Domains.Forms]], MyProject.Api.Forms'. Path 'Body.$type'.
 ---> Newtonsoft.Json.JsonSerializationException: Could not load assembly 'MyProject.Api.Forms'
....
2

There are 2 answers

5
Michael On BEST ANSWER

Yes, there is a way.
You can customize the way serialization works using the following code:

store.Conventions.CustomizeJsonSerializer = serializer =>
{
   serializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
};

As an example, take a look at the code here : https://dotnetfiddle.net/voJ7US

If you execute the code at the dotnetfiddle, you can see the results here: http://live-test.ravendb.net/studio/index.html#databases/documents?collection=Activities&database=UniqueTestDB

1
Tom Aalbers On

For RavenDB 5 and higher it changed a bit.

var store = DocumentStore
{
    Urls = new[] { "your-endpoint" },
    Conventions = new DocumentConventions
    {
        Serialization = new NewtonsoftJsonSerializationConventions
        {
            CustomizeJsonSerializer = serializer =>
            {
                serializer.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.None;
            }
        }
    }
}.Initialize();

See https://ravendb.net/docs/article-page/5.0/file-header/migration/client-api/conventions for more information.