Repository navigation
fix: serialize non-finite event properties as null #348
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eab4631
fix: serialize non-finite event properties as null
marandaneto 0539058
fix: preserve number handling and isolate compression tests
marandaneto 3e8e7ca
fix: preserve member number handling over collection attributes
marandaneto 1dc25dc
fix: retain nested collection number handling attributes
marandaneto f9aae49
fix: simplify non-finite number serialization after review
marandaneto 183dcff
docs: revert package README serialization notes
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "PostHog": patch | ||
| "PostHog.AspNetCore": patch | ||
| "PostHog.AI": patch | ||
| --- | ||
|
|
||
| Serialize non-finite float and double event properties as JSON null so they do not prevent an event batch from being sent. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace PostHog.Json; | ||
|
|
||
| internal sealed class NonFiniteDoubleJsonConverter : JsonConverter<double> | ||
| { | ||
| static readonly JsonSerializerOptions[] BuiltInOptions = Enumerable.Range(0, 8) | ||
| .Select(value => new JsonSerializerOptions { NumberHandling = (JsonNumberHandling)value }).ToArray(); | ||
| public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| => reader.TokenType == JsonTokenType.Number | ||
| ? reader.GetDouble() | ||
| : JsonSerializer.Deserialize<double>(ref reader, BuiltInOptions[(int)options.NumberHandling]); | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) | ||
| { | ||
| if (double.IsNaN(value) || double.IsInfinity(value)) | ||
| { | ||
| writer.WriteNullValue(); | ||
| } | ||
| else if ((options.NumberHandling & JsonNumberHandling.WriteAsString) != 0) | ||
| { | ||
| JsonSerializer.Serialize(writer, value, BuiltInOptions[(int)options.NumberHandling]); | ||
| } | ||
| else | ||
| { | ||
| writer.WriteNumberValue(value); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace PostHog.Json; | ||
|
|
||
| internal sealed class NonFiniteSingleJsonConverter : JsonConverter<float> | ||
| { | ||
| static readonly JsonSerializerOptions[] BuiltInOptions = Enumerable.Range(0, 8) | ||
| .Select(value => new JsonSerializerOptions { NumberHandling = (JsonNumberHandling)value }).ToArray(); | ||
| public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| => reader.TokenType == JsonTokenType.Number | ||
| ? reader.GetSingle() | ||
| : JsonSerializer.Deserialize<float>(ref reader, BuiltInOptions[(int)options.NumberHandling]); | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOptions options) | ||
| { | ||
| if (float.IsNaN(value) || float.IsInfinity(value)) | ||
| { | ||
| writer.WriteNullValue(); | ||
| } | ||
| else if ((options.NumberHandling & JsonNumberHandling.WriteAsString) != 0) | ||
| { | ||
| JsonSerializer.Serialize(writer, value, BuiltInOptions[(int)options.NumberHandling]); | ||
| } | ||
| else | ||
| { | ||
| writer.WriteNumberValue(value); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using PostHog.Json; | ||
| using PostHog.Library; | ||
|
|
||
| namespace NonFiniteNumberCompatibilityTests; | ||
|
|
||
| public class TheSerializeMethod | ||
| { | ||
| [Fact] | ||
| public async Task FlattensExtensionDataOnNumberHandlingModels() | ||
| { | ||
| var model = new ExtensionDataModel | ||
| { | ||
| Extra = new Dictionary<string, object> { ["value"] = 1.25, ["nonFinite"] = double.NaN } | ||
| }; | ||
|
|
||
| var json = await JsonSerializerHelper.SerializeToCamelCaseJsonStringAsync(model); | ||
|
|
||
| JsonAssert.Equal("""{"value":1.25,"nonFinite":null}""", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SkipsIgnoredMembersWithInvalidMetadata() | ||
| { | ||
| var json = await JsonSerializerHelper.SerializeToCamelCaseJsonStringAsync(new IgnoredMemberModel()); | ||
|
|
||
| JsonAssert.Equal("""{"value":1.25}""", json); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SerializesPopulationCapableCollections() | ||
| { | ||
| var model = new PopulatedCollectionModel(); | ||
| model.Values.Add(1.25); | ||
| model.Values.Add(double.NaN); | ||
|
|
||
| var json = await JsonSerializerHelper.SerializeToCamelCaseJsonStringAsync(model); | ||
|
|
||
| JsonAssert.Equal("""{"values":[1.25,null]}""", json); | ||
| } | ||
|
|
||
| [JsonNumberHandling(JsonNumberHandling.WriteAsString)] | ||
| public sealed class ExtensionDataModel | ||
| { | ||
| [JsonExtensionData] | ||
| public Dictionary<string, object> Extra { get; init; } = []; | ||
| } | ||
|
|
||
| public sealed class IgnoredMemberModel | ||
| { | ||
| public double Value { get; init; } = 1.25; | ||
|
|
||
| [JsonIgnore] | ||
| public CollidingNamesModel? Ignored { get; init; } | ||
| } | ||
|
|
||
| public sealed class CollidingNamesModel | ||
| { | ||
| [JsonPropertyName("name")] | ||
| public string? First { get; init; } | ||
|
|
||
| [JsonPropertyName("name")] | ||
| public string? Second { get; init; } | ||
| } | ||
|
|
||
| #pragma warning disable CA1002 // Exercise the built-in population-capable collection contract. | ||
| public sealed class PopulatedCollectionModel | ||
| { | ||
| [JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] | ||
| [JsonNumberHandling(JsonNumberHandling.WriteAsString)] | ||
| public List<double> Values { get; } = []; | ||
| } | ||
| #pragma warning restore CA1002 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.