Skip to content

Commit a21b199

Browse files
authored
fix: CaptureFeedback now supports multiple attachments (#5077)
* add all attachments * updated test * feedback
1 parent 2a791e1 commit a21b199

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

src/Sentry/Protocol/Envelopes/Envelope.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,17 @@ public static Envelope FromFeedback(
313313

314314
if (attachments is { Count: > 0 })
315315
{
316-
if (attachments.Count > 1)
316+
foreach (var attachment in attachments)
317317
{
318-
logger?.LogWarning("Feedback can only contain one attachment. Discarding {0} additional attachments.",
319-
attachments.Count - 1);
320-
}
318+
// Safety check, in case the user forcefully added a null attachment.
319+
if (attachment.IsNull())
320+
{
321+
logger?.LogWarning("Encountered a null attachment. Skipping.");
322+
continue;
323+
}
321324

322-
AddEnvelopeItemFromAttachment(items, attachments.First(), logger);
325+
AddEnvelopeItemFromAttachment(items, attachment, logger);
326+
}
323327
}
324328

325329
if (sessionUpdate is not null)

test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ public void FromFeedback_NoFeedbackContext_Throws()
845845
}
846846

847847
[Fact]
848-
public void FromFeedback_MultipleAttachments_LogsWarning()
848+
public void FromFeedback_MultipleAttachments_AddsAll()
849849
{
850850
// Arrange
851851
var feedback = new SentryFeedback(
@@ -864,23 +864,46 @@ public void FromFeedback_MultipleAttachments_LogsWarning()
864864
Feedback = feedback
865865
}
866866
};
867-
var logger = Substitute.For<IDiagnosticLogger>();
868-
logger.IsEnabled(Arg.Any<SentryLevel>()).Returns(true);
869-
870867
List<SentryAttachment> attachments = [
871868
AttachmentHelper.FakeAttachment("file1.txt"), AttachmentHelper.FakeAttachment("file2.txt")
872869
];
873870

874871
// Act
875-
using var envelope = Envelope.FromFeedback(evt, logger, attachments);
872+
using var envelope = Envelope.FromFeedback(evt, attachments: attachments);
873+
874+
// Assert
875+
envelope.Items.Count(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment).Should().Be(2);
876+
}
877+
878+
[Fact]
879+
public void FromFeedback_NullAttachment_Skipped()
880+
{
881+
// Arrange
882+
var feedback = new SentryFeedback(
883+
"Everything is great!",
884+
"foo@bar.com",
885+
"Someone Nice",
886+
"fake-replay-id",
887+
"https://www.example.com",
888+
SentryId.Create()
889+
);
890+
var evt = new SentryEvent
891+
{
892+
Level = SentryLevel.Info,
893+
Contexts =
894+
{
895+
Feedback = feedback
896+
}
897+
};
898+
List<SentryAttachment> attachments = [
899+
null!, AttachmentHelper.FakeAttachment("file1.txt")
900+
];
901+
902+
// Act
903+
using var envelope = Envelope.FromFeedback(evt, attachments: attachments);
876904

877905
// Assert
878-
logger.Received(1).Log(
879-
SentryLevel.Warning,
880-
Arg.Is<string>(m => m.Contains("Feedback can only contain one attachment")),
881-
null,
882-
Arg.Any<object[]>());
883-
envelope.Items.Should().ContainSingle(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment);
906+
envelope.Items.Count(item => item.TryGetType() == EnvelopeItem.TypeValueAttachment).Should().Be(1);
884907
}
885908

886909
[Fact]

0 commit comments

Comments
 (0)