dsuryd/dotNetify

Wrong serialization on model updates

Paulskit opened this issue · 2 comments

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.

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()
}));

Closing this as resolved by using the above code.