proudmonkey / AutoWrapper

A simple, yet customizable global exception handler and Http response wrapper for ASP.NET Core APIs.
MIT License
677 stars 82 forks source link

Body reserialization loses custom JSON formatting #87

Open rohatsu opened 3 years ago

rohatsu commented 3 years ago

Hey, many thanks for the useful tool but unfortunately we've recently discovered a showstopper - AW middleware deserializes Action's body result into a dynamic object and ten serializes it again which loses any custom JSON.NET serialization rules/attributes from the original (including serialization attributes since these get lost on a dynamic object), in our case it has changed datetime formats which confused clients. Passing-through the original response as-is (assuming it's a representation of a valid JSON object which could be wrapped by AW response) or using reserialization that is 1:1 with the original is something that our use case would need.

To illustrate the issue, the result of the below call is {"t":"2020-11-30T01:02:03Z"}: (milliseconds part gets truncated) Newtonsoft.Json.JsonConvert.SerializeObject(Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>("{\"t\":\"2020-11-30T01:02:03.000Z\"}"))

One quick fix that would help in our particular case would be to set DateParseHandling to None which will treat dates as strings, the reserialization issue could still happen on floating numbers and formatting but these aren't as pressing.