Skip to content
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

Prevent duplicate package reporting #1197

Merged
merged 8 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- Prevent duplicate package reporting ([#1197](https://github.com/getsentry/sentry-dotnet/pull/1197))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this goes under the ### Fixes


### Fixes

Expand Down
25 changes: 25 additions & 0 deletions src/Sentry/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,30 @@ public static Package FromJson(JsonElement json)

return new Package(name, version);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (Name.GetHashCode() * 397) ^ Version.GetHashCode();
josh-degraw marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}

if (obj is Package package)
{
return Name == package.Name && Version == package.Version;
}

return false;
}
}
}
2 changes: 1 addition & 1 deletion src/Sentry/SdkVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void WriteTo(Utf8JsonWriter writer)
{
writer.WriteStartObject();

writer.WriteArrayIfNotEmpty("packages", InternalPackages);
writer.WriteArrayIfNotEmpty("packages", InternalPackages.Distinct());
writer.WriteStringIfNotWhiteSpace("name", Name);
writer.WriteStringIfNotWhiteSpace("version", Version);

Expand Down
37 changes: 37 additions & 0 deletions test/Sentry.Tests/Protocol/SdkVersionTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Sentry.Tests.Helpers;
using Xunit;

Expand Down Expand Up @@ -57,5 +58,41 @@ public static IEnumerable<object[]> TestCases()
sdk.AddPackage("a", "1");
yield return new object[] { (sdk, "{\"packages\":[{\"name\":\"a\",\"version\":\"1\"},{\"name\":\"b\",\"version\":\"2\"}]}") };
}

[Fact]
public void SerializeObject_IgnoresDuplicatePackages()
{
var sdkVersion = new SdkVersion
{
Name = "Sentry.Test.SDK",
Version = "3.9.2"
};
sdkVersion.AddPackage("Foo", "Alpha");
sdkVersion.AddPackage("Bar", "Beta");
josh-degraw marked this conversation as resolved.
Show resolved Hide resolved
var actual = sdkVersion.ToJsonString();
var expected = TrimJson(@"
{
""packages"": [
{
""name"": ""Bar"",
""version"": ""Beta""
},
{
""name"": ""Foo"",
""version"": ""Alpha""
}
],
""name"": ""Sentry.Test.SDK"",
""version"": ""3.9.2""
}");
Assert.Equal(expected, actual);

}

private static string TrimJson(string json)
{
return Regex.Replace(json, @"\s", "");
}

josh-degraw marked this conversation as resolved.
Show resolved Hide resolved
}
}