From c315414824ba03ccad8b826888a14a2a415f5046 Mon Sep 17 00:00:00 2001 From: pofider Date: Tue, 2 Jun 2020 23:55:11 +0200 Subject: [PATCH] avoid camel casing serialized render request data --- jsreport.Shared.Test/SerializerHelperTest.cs | 70 ++++++++++++++++++++ jsreport.Shared/SerializerHelper.cs | 46 ++++++++++++- jsreport.Shared/jsreport.Shared.csproj | 4 +- 3 files changed, 115 insertions(+), 5 deletions(-) diff --git a/jsreport.Shared.Test/SerializerHelperTest.cs b/jsreport.Shared.Test/SerializerHelperTest.cs index ba15f9e..2bac875 100644 --- a/jsreport.Shared.Test/SerializerHelperTest.cs +++ b/jsreport.Shared.Test/SerializerHelperTest.cs @@ -1,3 +1,4 @@ +using jsreport.Types; using NUnit.Framework; using Shouldly; using System.Collections.Generic; @@ -28,5 +29,74 @@ public void TestParseReportMeta() report.ContentType.ShouldBe("text/html"); } + + [Test] + public void TestShouldntChangeDataPropsCasing() + { + var serialized = SerializerHelper.SerializeRenderRequest(new RenderRequest + { + Template = new Template + { + Name = "foo" + }, + Data = new + { + aA = 1, + Bb = 2 + } + }); + + serialized.ShouldContain("aA", Case.Sensitive); + serialized.ShouldContain("Bb", Case.Sensitive); + serialized.ShouldContain("\"data\": {", Case.Sensitive); + } + + [Test] + public void TestShouldntFailWhenSerializingRenderRequestWithNullData() + { + var serialized = SerializerHelper.SerializeRenderRequest(new RenderRequest + { + Template = new Template + { + Name = "foo" + } + }); + + serialized.ShouldContain("template", Case.Sensitive); + } + + [Test] + public void TestShouldntChangeDataPropsCasingForAnonymousObject() + { + var serialized = SerializerHelper.SerializeRenderRequest(new { + Template = new + { + name = "foo" + }, + data = new + { + aA = 1, + Bb = 2 + } + }); + + serialized.ShouldContain("aA", Case.Sensitive); + serialized.ShouldContain("Bb", Case.Sensitive); + serialized.ShouldContain("\"data\": {", Case.Sensitive); + + serialized.ShouldContain("template", Case.Sensitive); + } + + [Test] + public void TestShouldntFailWhenSerializingAnonymousRenderRequestWithNullData() + { + var serialized = SerializerHelper.SerializeRenderRequest(new { + template = new { + Name = "foo" + } + }); + + serialized.ShouldContain("template", Case.Sensitive); + } } } diff --git a/jsreport.Shared/SerializerHelper.cs b/jsreport.Shared/SerializerHelper.cs index ffc0c0e..b6cceed 100644 --- a/jsreport.Shared/SerializerHelper.cs +++ b/jsreport.Shared/SerializerHelper.cs @@ -8,6 +8,8 @@ using System.Runtime.Serialization; using System.Linq; using System.Text.RegularExpressions; +using System.Dynamic; +using System.ComponentModel; namespace jsreport.Shared { @@ -82,6 +84,10 @@ private static string MetaValue(this IDictionary meta, string ke public static string SerializeRenderRequest(RenderRequest rr) { + // a hack to avoid camel casing the data prop values + var data = rr.Data; + rr.Data = null; + var js = new JsonSerializer() { ContractResolver = new CamelCasePropertyNamesContractResolver(), @@ -92,6 +98,12 @@ public static string SerializeRenderRequest(RenderRequest rr) var jo = JObject.FromObject(rr, js); + if (data != null) + { + js.ContractResolver = new DefaultContractResolver(); + jo["data"] = JObject.FromObject(data, js); + } + jo["overwrites"]?["template"]?.Values().ToList() .ForEach((val => jo["template"][val.Path.Replace("overwrites.template.", "")] = val)); @@ -105,7 +117,27 @@ public static string SerializeRenderRequest(RenderRequest rr) public static string SerializeRenderRequest(object r) { - var js = new JsonSerializerSettings() + // a hack to avoid camel casing the data prop values + IDictionary expando = new ExpandoObject(); + foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(r.GetType())) + { + expando.Add(property.Name, property.GetValue(r)); + } + + object data = null; + if (expando.ContainsKey("Data")) + { + data = expando["Data"]; + expando["Data"] = null; + } + + if (expando.ContainsKey("data")) + { + data = expando["data"]; + expando["data"] = null; + } + + var js = new JsonSerializer() { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore, @@ -113,7 +145,15 @@ public static string SerializeRenderRequest(object r) PreserveReferencesHandling = PreserveReferencesHandling.Objects }; - return JsonConvert.SerializeObject(r, js); + var jo = JObject.FromObject(expando as ExpandoObject,js); + + js.ContractResolver = new DefaultContractResolver(); + if (data != null) + { + jo["data"] = JObject.FromObject(data, js); + } + + return jo.ToString(); } public static string SerializeRenderRequest(string templateShortid, object data) @@ -205,5 +245,5 @@ private static void InnerSerializeConfiguration(object obj, IDictionary - + - +