Skip to content

Commit 6923144

Browse files
authored
Fix enrichment serialization error (#135)
* add JsonIgnore annotation to enrichment * add unit tests to cover events with enrichment writing to disk
1 parent 856052f commit 6923144

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Analytics-CSharp/Segment/Analytics/Types.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
using global::System;
22
using Segment.Serialization;
33

4+
#if NETSTANDARD2_0
5+
using System.Text.Json.Serialization;
6+
#else
7+
using Newtonsoft.Json;
8+
#endif
9+
410
namespace Segment.Analytics
511
{
612
public class DestinationMetadata
@@ -18,6 +24,7 @@ public abstract class RawEvent
1824
public virtual string UserId { get; set; }
1925
public virtual string Timestamp { get; set; }
2026

27+
[JsonIgnore]
2128
public Func<RawEvent, RawEvent> Enrichment { get; set; }
2229

2330
// JSON types

Tests/Utilities/StorageTest.cs

+24
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,30 @@ public async Task TestStoreEvent()
254254
Assert.Null(exception);
255255
}
256256

257+
[Fact]
258+
public async Task TestStoreEventWithEnrichment()
259+
{
260+
TrackEvent trackEvent = new TrackEvent("clicked", new JsonObject { ["foo"] = "bar" });
261+
trackEvent.Enrichment = @event =>
262+
{
263+
return @event;
264+
};
265+
string payloadWithEnrichment = JsonUtility.ToJson(trackEvent);
266+
await _storage.Write(StorageConstants.Events, payloadWithEnrichment);
267+
await _storage.Rollover();
268+
269+
string path = _dir + Path.DirectorySeparatorChar + _writeKey + "-0.json";
270+
string actual = File.ReadAllText(path);
271+
Exception exception = Record.Exception(() =>
272+
{
273+
JsonUtility.FromJson<JsonObject>(actual);
274+
});
275+
276+
Assert.True(File.Exists(path));
277+
Assert.Contains(_payload, actual);
278+
Assert.Null(exception);
279+
}
280+
257281
[Fact]
258282
public async Task TestRead()
259283
{

0 commit comments

Comments
 (0)