Skip to content

Commit b9478d7

Browse files
committed
Ensure we don't serialize excluded dictionary/dynamic keys
1 parent f201f83 commit b9478d7

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

src/Exceptionless/Newtonsoft.Json/JsonWriter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,14 @@ public virtual void WriteEndConstructor()
464464
{
465465
InternalWriteEnd(JsonContainerType.Constructor);
466466
}
467+
468+
/// <summary>
469+
/// CUSTOM: Return two if the property an be written.
470+
/// </summary>
471+
public virtual bool ShouldWriteProperty(string name)
472+
{
473+
return true;
474+
}
467475

468476
/// <summary>
469477
/// Writes the property name of a name/value pair of a JSON object.

src/Exceptionless/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,11 @@ private void SerializeObject(JsonWriter writer, object value, JsonObjectContract
501501
propertyName = (contract.ExtensionDataNameResolver != null)
502502
? contract.ExtensionDataNameResolver(propertyName)
503503
: propertyName;
504-
504+
505+
// CUSTOM: check to see if property name can be written.
506+
if (!writer.ShouldWriteProperty(propertyName))
507+
continue;
508+
505509
if (ShouldWriteReference(e.Value, null, valueContract, contract, member))
506510
{
507511
writer.WritePropertyName(propertyName);
@@ -1074,6 +1078,10 @@ private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDict
10741078
? contract.DictionaryKeyResolver(propertyName)
10751079
: propertyName;
10761080

1081+
// CUSTOM: check to see if property name can be written.
1082+
if (!writer.ShouldWriteProperty(propertyName))
1083+
continue;
1084+
10771085
try
10781086
{
10791087
object value = entry.Value;

src/Exceptionless/Serializer/DefaultJsonSerializer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Exceptionless.Extensions;
99
using Exceptionless.Json;
1010
using Exceptionless.Json.Converters;
11+
using Exceptionless.Json.Linq;
1112
using Exceptionless.Json.Serialization;
1213

1314
namespace Exceptionless.Serializer {
@@ -42,14 +43,15 @@ public virtual string Serialize(object model, string[] exclusions = null, int ma
4243
if (model == null)
4344
return null;
4445

45-
JsonSerializer serializer = JsonSerializer.Create(_serializerSettings);
46+
var serializer = JsonSerializer.Create(_serializerSettings);
4647
if (maxDepth < 1)
4748
maxDepth = Int32.MaxValue;
4849

50+
var excludedPropertyNames = new HashSet<string>(exclusions ?? new string[0], StringComparer.OrdinalIgnoreCase);
4951
using (var sw = new StringWriter()) {
50-
using (var jw = new JsonTextWriterWithDepth(sw)) {
52+
using (var jw = new JsonTextWriterWithExclusions(sw, excludedPropertyNames)) {
5153
jw.Formatting = Formatting.None;
52-
Func<JsonProperty, object, bool> include = (property, value) => ShouldSerialize(jw, property, value, maxDepth, exclusions);
54+
Func<JsonProperty, object, bool> include = (property, value) => ShouldSerialize(jw, property, value, maxDepth, excludedPropertyNames);
5355
var resolver = new ExceptionlessContractResolver(include);
5456
serializer.ContractResolver = resolver;
5557
if (continueOnSerializationError)
@@ -69,7 +71,7 @@ public virtual object Deserialize(string json, Type type) {
6971
return JsonConvert.DeserializeObject(json, type, _serializerSettings);
7072
}
7173

72-
private bool ShouldSerialize(JsonTextWriterWithDepth jw, JsonProperty property, object obj, int maxDepth, IList<string> excludedPropertyNames) {
74+
private bool ShouldSerialize(JsonTextWriterWithDepth jw, JsonProperty property, object obj, int maxDepth, ISet<string> excludedPropertyNames) {
7375
try {
7476
if (excludedPropertyNames != null && (property.UnderlyingName.AnyWildcardMatches(excludedPropertyNames, true) || property.PropertyName.AnyWildcardMatches(excludedPropertyNames, true)))
7577
return false;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Exceptionless.Extensions;
5+
6+
namespace Exceptionless.Serializer
7+
{
8+
internal class JsonTextWriterWithExclusions : JsonTextWriterWithDepth {
9+
private readonly ISet<string> _excludedPropertyNames;
10+
11+
public JsonTextWriterWithExclusions(TextWriter textWriter, ISet<string> excludedPropertyNames) : base(textWriter) {
12+
_excludedPropertyNames = excludedPropertyNames;
13+
}
14+
15+
public override bool ShouldWriteProperty(string name) {
16+
return !name.AnyWildcardMatches(_excludedPropertyNames, true);
17+
}
18+
}
19+
}

test/Exceptionless.Tests/Configuration/DataExclusionTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public void CanHandleObject() {
6868
var ev = new Event();
6969
ev.SetProperty(nameof(order), order, excludedPropertyNames: new [] { nameof(order.CardLast4) });
7070
Assert.Single(ev.Data);
71-
72-
Assert.Equal("{\"id\":\"1234\"}", ev.Data.GetString(nameof(order)));
71+
Assert.Equal("{\"id\":\"1234\",\"data\":{}}", ev.Data.GetString(nameof(order)));
7372
}
7473

7574
[InlineData("Credit*", true)]

0 commit comments

Comments
 (0)