Skip to content

Commit

Permalink
avoid camel casing serialized render request data
Browse files Browse the repository at this point in the history
  • Loading branch information
pofider committed Jun 2, 2020
1 parent 73fca83 commit c315414
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
70 changes: 70 additions & 0 deletions jsreport.Shared.Test/SerializerHelperTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using jsreport.Types;
using NUnit.Framework;
using Shouldly;
using System.Collections.Generic;
Expand Down Expand Up @@ -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);
}
}
}
46 changes: 43 additions & 3 deletions jsreport.Shared/SerializerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Runtime.Serialization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Dynamic;
using System.ComponentModel;

namespace jsreport.Shared
{
Expand Down Expand Up @@ -82,6 +84,10 @@ private static string MetaValue(this IDictionary<string, string> 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(),
Expand All @@ -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));

Expand All @@ -105,15 +117,43 @@ 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<string, object> 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,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
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)
Expand Down Expand Up @@ -205,5 +245,5 @@ private static void InnerSerializeConfiguration(object obj, IDictionary<string,
}
}
}
}
}
}
4 changes: 2 additions & 2 deletions jsreport.Shared/jsreport.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="jsreport.Types" Version="2.6.1" />
<PackageReference Include="jsreport.Types" Version="2.7.0" />
</ItemGroup>
</Otherwise>
</Choose>

<ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
</ItemGroup>
Expand Down

2 comments on commit c315414

@hgGeorg
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great... this change broke all our reports.

Our .net models are PascalCased and we assumed that all properties in jsreport would be camelCased.

@huysentruitw
Copy link

@huysentruitw huysentruitw commented on c315414 Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we had to explicitly pass a CamelCasePropertyNamesContractResolver to the ReportingService:

new ReportingService(options.ReportingServiceUri.ToString())
{
    ContractResolverForDataProperty = new CamelCasePropertyNamesContractResolver(),
};

Please sign in to comment.