diff --git a/src/Sentry/Protocol/Envelopes/Envelope.cs b/src/Sentry/Protocol/Envelopes/Envelope.cs index e3e85da713..227c572797 100644 --- a/src/Sentry/Protocol/Envelopes/Envelope.cs +++ b/src/Sentry/Protocol/Envelopes/Envelope.cs @@ -313,13 +313,17 @@ public static Envelope FromFeedback( if (attachments is { Count: > 0 }) { - if (attachments.Count > 1) + foreach (var attachment in attachments) { - logger?.LogWarning("Feedback can only contain one attachment. Discarding {0} additional attachments.", - attachments.Count - 1); - } + // Safety check, in case the user forcefully added a null attachment. + if (attachment.IsNull()) + { + logger?.LogWarning("Encountered a null attachment. Skipping."); + continue; + } - AddEnvelopeItemFromAttachment(items, attachments.First(), logger); + AddEnvelopeItemFromAttachment(items, attachment, logger); + } } if (sessionUpdate is not null) diff --git a/test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs b/test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs index 8db1187afe..1835016cf9 100644 --- a/test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs +++ b/test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs @@ -845,7 +845,7 @@ public void FromFeedback_NoFeedbackContext_Throws() } [Fact] - public void FromFeedback_MultipleAttachments_LogsWarning() + public void FromFeedback_MultipleAttachments_AddsAll() { // Arrange var feedback = new SentryFeedback( @@ -864,23 +864,46 @@ public void FromFeedback_MultipleAttachments_LogsWarning() Feedback = feedback } }; - var logger = Substitute.For(); - logger.IsEnabled(Arg.Any()).Returns(true); - List attachments = [ AttachmentHelper.FakeAttachment("file1.txt"), AttachmentHelper.FakeAttachment("file2.txt") ]; // Act - using var envelope = Envelope.FromFeedback(evt, logger, attachments); + using var envelope = Envelope.FromFeedback(evt, attachments: attachments); + + // Assert + envelope.Items.Count(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment).Should().Be(2); + } + + [Fact] + public void FromFeedback_NullAttachment_Skipped() + { + // Arrange + var feedback = new SentryFeedback( + "Everything is great!", + "foo@bar.com", + "Someone Nice", + "fake-replay-id", + "https://www.example.com", + SentryId.Create() + ); + var evt = new SentryEvent + { + Level = SentryLevel.Info, + Contexts = + { + Feedback = feedback + } + }; + List attachments = [ + null!, AttachmentHelper.FakeAttachment("file1.txt") + ]; + + // Act + using var envelope = Envelope.FromFeedback(evt, attachments: attachments); // Assert - logger.Received(1).Log( - SentryLevel.Warning, - Arg.Is(m => m.Contains("Feedback can only contain one attachment")), - null, - Arg.Any()); - envelope.Items.Should().ContainSingle(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment); + envelope.Items.Count(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment).Should().Be(1); } [Fact]