From 3cd4f123f60d71df6f96534c5fe9ade75623ccb7 Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Wed, 30 Apr 2025 16:59:59 -0500 Subject: [PATCH 1/2] add JsonIgnore annotation to enrichment --- Analytics-CSharp/Segment/Analytics/Types.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Analytics-CSharp/Segment/Analytics/Types.cs b/Analytics-CSharp/Segment/Analytics/Types.cs index 5a67838..66cec72 100644 --- a/Analytics-CSharp/Segment/Analytics/Types.cs +++ b/Analytics-CSharp/Segment/Analytics/Types.cs @@ -1,6 +1,12 @@ using global::System; using Segment.Serialization; +#if NETSTANDARD2_0 +using System.Text.Json.Serialization; +#else +using Newtonsoft.Json; +#endif + namespace Segment.Analytics { public class DestinationMetadata @@ -18,6 +24,7 @@ public abstract class RawEvent public virtual string UserId { get; set; } public virtual string Timestamp { get; set; } + [JsonIgnore] public Func Enrichment { get; set; } // JSON types From aaccc6472fcb2ddaf2e5032e65286c4072075568 Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Wed, 30 Apr 2025 17:00:25 -0500 Subject: [PATCH 2/2] add unit tests to cover events with enrichment writing to disk --- Tests/Utilities/StorageTest.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Tests/Utilities/StorageTest.cs b/Tests/Utilities/StorageTest.cs index e8877d7..59ca670 100644 --- a/Tests/Utilities/StorageTest.cs +++ b/Tests/Utilities/StorageTest.cs @@ -254,6 +254,30 @@ public async Task TestStoreEvent() Assert.Null(exception); } + [Fact] + public async Task TestStoreEventWithEnrichment() + { + TrackEvent trackEvent = new TrackEvent("clicked", new JsonObject { ["foo"] = "bar" }); + trackEvent.Enrichment = @event => + { + return @event; + }; + string payloadWithEnrichment = JsonUtility.ToJson(trackEvent); + await _storage.Write(StorageConstants.Events, payloadWithEnrichment); + await _storage.Rollover(); + + string path = _dir + Path.DirectorySeparatorChar + _writeKey + "-0.json"; + string actual = File.ReadAllText(path); + Exception exception = Record.Exception(() => + { + JsonUtility.FromJson(actual); + }); + + Assert.True(File.Exists(path)); + Assert.Contains(_payload, actual); + Assert.Null(exception); + } + [Fact] public async Task TestRead() {