dsuryd / dotNetify

Simple, lightweight, yet powerful way to build real-time web apps.
https://dotnetify.net
Other
1.17k stars 164 forks source link

Wrong serialization on model updates #295

Closed Paulskit closed 3 years ago

Paulskit commented 3 years ago

Hello @dsuryd! Thank you for the library, it saves me a lot of time. Recently I noticed a strange behavior on the serialization.

I have a custom contract resolver:

internal class CamelCaseContractResolver : VMContractResolver
    {
        public CamelCaseContractResolver()
        {
            NamingStrategy = new CamelCaseNamingStrategy();
        }
    }

It's registered in Startup.cs

            app.UseDotNetify(o =>
            {
                o.UseJsonSerializerSettings(j =>
                {
                    j.ContractResolver = new CamelCaseContractResolver();
                });
            });

Then there is a model (simplified for sake of readability)

public class ServicesVM : BaseVM
    {
        private readonly ILoader _loader;

        public ServicesVM(ILoader loader)
        {
            _loader= loader;
        }

        public IEnumerable<ServiceDto> Services
        {
            get => Get<IEnumerable<ServiceDto>>();
            set => Set(value);
        }        

        public override async Task OnCreatedAsync()
        {
            Services = await _loader.LoadAsync();
        }

        public async Task Update()
        {
            Services = await _loader.LoadAsync();
        }
    }

When the model is created and data is loaded in OnCreatedAsync I get nice camelCased properties in the response. However, after Update() it seems to return PascalCased properties instead.

image

Can you please take a look? Thank you. Appreciate your effort.

dsuryd commented 3 years ago

I was able to reproduce the issue with that custom resolver, but no issue when using the built-in Newtonsoft resolver:

app.UseDotNetify(cfg => cfg.UseJsonSerializerSettings(new Newtonsoft.Json.JsonSerializerSettings
{
    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
}));
dsuryd commented 3 years ago

Closing this as resolved by using the above code.