Skip to content

Fix enrichment serialization error #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Analytics-CSharp/Segment/Analytics/Types.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,6 +24,7 @@ public abstract class RawEvent
public virtual string UserId { get; set; }
public virtual string Timestamp { get; set; }

[JsonIgnore]
public Func<RawEvent, RawEvent> Enrichment { get; set; }

// JSON types
Expand Down
24 changes: 24 additions & 0 deletions Tests/Utilities/StorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonObject>(actual);
});

Assert.True(File.Exists(path));
Assert.Contains(_payload, actual);
Assert.Null(exception);
}

[Fact]
public async Task TestRead()
{
Expand Down
Loading