Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 478
Attachments can be manipulated via hints#2046
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
0daecc826e3bbc8fa62bbf916def04edda0815bf531aa927560dd87ac3d84d9feea24cd7d4e72f752f8f22871ce34a695e84ab09f357f494File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,108 @@ | ||
| package io.sentry.hints; | ||
| import io.sentry.Attachment; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| public final class Hints { | ||
| private static final @NotNull Map<String, Class<?>> PRIMITIVE_MAPPINGS; | ||
| static { | ||
| PRIMITIVE_MAPPINGS = new HashMap<>(); | ||
| PRIMITIVE_MAPPINGS.put("boolean", Boolean.class); | ||
| PRIMITIVE_MAPPINGS.put("char", Character.class); | ||
| PRIMITIVE_MAPPINGS.put("byte", Byte.class); | ||
| PRIMITIVE_MAPPINGS.put("short", Short.class); | ||
| PRIMITIVE_MAPPINGS.put("int", Integer.class); | ||
| PRIMITIVE_MAPPINGS.put("long", Long.class); | ||
| PRIMITIVE_MAPPINGS.put("float", Float.class); | ||
| PRIMITIVE_MAPPINGS.put("double", Double.class); | ||
| } | ||
| private final @NotNull Map<String, Object> internalStorage = new HashMap<String, Object>(); | ||
| private final @NotNull List<Attachment> attachments = new ArrayList<>(); | ||
| private @Nullable Attachment screenshot = null; | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need some kind of synchronization around this field (looking at MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah no, I don't think it needs to be synchronized, can replace with a different implementation. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated | ||
| public void set(@NotNull String hintType, @Nullable Object hint) { | ||
| internalStorage.put(hintType, hint); | ||
| public static @NotNull Hints withAttachment(@Nullable Attachment attachment) { | ||
| @NotNull final Hints hints = new Hints(); | ||
| hints.addAttachment(attachment); | ||
| return hints; | ||
| } | ||
| public @Nullable Object get(@NotNull String hintType) { | ||
| return internalStorage.get(hintType); | ||
| public static @NotNull Hints withAttachments(@Nullable List<Attachment> attachments) { | ||
| @NotNull final Hints hints = new Hints(); | ||
| hints.addAttachments(attachments); | ||
| return hints; | ||
| } | ||
| // TODO maybe not public | ||
| public void remove(@NotNull String hintType) { | ||
| internalStorage.remove(hintType); | ||
| public void set(@NotNull String name, @Nullable Object hint) { | ||
| internalStorage.put(name, hint); | ||
| } | ||
| // TODO addAttachment(one) | ||
philipphofmann marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // TODO getAttachments(): List | ||
| // TODO setAttachments(list) | ||
| // TODO clearAttachments() | ||
| public @Nullable Object get(@NotNull String name) { | ||
| return internalStorage.get(name); | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| public <T extends Object> @Nullable T getAs(@NotNull String name, @NotNull Class<T> clazz) { | ||
| Object hintValue = internalStorage.get(name); | ||
| if (clazz.isInstance(hintValue)) { | ||
adinauer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return (T) hintValue; | ||
| } else if (isCastablePrimitive(hintValue, clazz)) { | ||
| return (T) hintValue; | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| public void remove(@NotNull String name) { | ||
| internalStorage.remove(name); | ||
| } | ||
| public void addAttachment(@Nullable Attachment attachment) { | ||
| if (attachment != null) { | ||
| attachments.add(attachment); | ||
| } | ||
| } | ||
| public void addAttachments(@Nullable List<Attachment> attachments) { | ||
| if (attachments != null) { | ||
| this.attachments.addAll(attachments); | ||
| } | ||
| } | ||
| public @NotNull List<Attachment> getAttachments() { | ||
| return new ArrayList<>(attachments); | ||
| } | ||
| public void replaceAttachments(@Nullable List<Attachment> attachments) { | ||
| clearAttachments(); | ||
| addAttachments(attachments); | ||
| } | ||
| public void clearAttachments() { | ||
| attachments.clear(); | ||
philipphofmann marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| public void setScreenshot(@Nullable Attachment screenshot) { | ||
| this.screenshot = screenshot; | ||
| } | ||
| public @Nullable Attachment getScreenshot() { | ||
| return screenshot; | ||
| } | ||
| private boolean isCastablePrimitive(@Nullable Object hintValue, @NotNull Class<?> clazz) { | ||
| Class<?> nonPrimitiveClass = PRIMITIVE_MAPPINGS.get(clazz.getCanonicalName()); | ||
| return hintValue != null | ||
| && clazz.isPrimitive() | ||
| && nonPrimitiveClass != null | ||
| && nonPrimitiveClass.isInstance(hintValue); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to prevent manipulation of attachments if
!shouldApplyScopeData?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you did makes sense to only get the attachments from the scope into the hints with this condition.
If we're not applying the scope to the event/hint, an attachment still could show up in the hint because the user added it directly while calling capture. And that's totally fine.