NancyFx / Nancy.Serialization.JsonNet

NewtonSoft.Json serializer for Nancy
MIT License
40 stars 32 forks source link

FormatterExtensions.jsonSerializer keeps wrong serializer with Diagnostics #37

Closed xmedeko closed 8 years ago

xmedeko commented 8 years ago

I have Nancy 1.4.3, Nancy.Serialization.JsonNet 1.4.1. When start my app with Nancy diagnostics and go first to the http://localhost:8080/_Nancy/interactive#Testing%20Diagnostic%20Provider and then run my request, the request is formatted by the DefaultJsonSerializer and not JsonNetSerializer since the FormatterExtensions.jsonSerializer keeps the DefaultJsonSerializer from the first request.

I guess both jsonSerializer and xmlSerializer fields should be removed.

khellang commented 8 years ago

Isn't this a duplicate of #36?

xmedeko commented 8 years ago

No it's not a duplicate. These are two different problems.

khellang commented 8 years ago

How are these two different problems? By your description it seems to be exactly the same. It's picking the wrong serializer?

xmedeko commented 8 years ago

The #36 is that the formatter.Serializer has different order depending on the machine/environment. This bug is about wrong caching of FormatterExtensions.jsonSerializer on the same machine/environment.

khellang commented 8 years ago

"wrong caching"? I'm very confused :confused:

xmedeko commented 8 years ago

Look at the FormatterExtensions.AsJson code:

var serializer = jsonSerializer ?? (jsonSerializer = formatter.Serializers.FirstOrDefault(s => s.CanSerialize("application/json")));

It takes the first JSON serializer from the formatter from the first call and caches it in the jsonSerializer. So, when the next call has different formatters, it still uses the one from jsonSerializer.

khellang commented 8 years ago

How can the next call have "different formatters"? The container configuration is set up once. It won't change at runtime.

xmedeko commented 8 years ago

Ask the Nancy Diagnostics author. It's either a bug of the FormatterExtensions.jsonSerializer or the Nancy.Diagnostics.

Note: IMO FormatterExtensions.AsJson has the IResponseFormatter parameter and should work well with all IResponseFormatter objects, even if I create my own IResponseFormatter independent of the container configuration.

khellang commented 8 years ago

Ask the Nancy Diagnostics author. It's either a bug of the FormatterExtensions.jsonSerializer or the Nancy.Diagnostics.

Either way, this issue has nothing to do with the Newtonsoft.Json serializer :smile:

Note: IMO FormatterExtensions.AsJson has the IResponseFormatter parameter and should work well with all IResponseFormatter objects, even if I create my own IResponseFormatter independent of the container configuration.

It doesn't?

xmedeko commented 8 years ago

OK, created https://github.com/NancyFx/Nancy/issues/2399