-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Allow null message in SlackMessage #31288
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,16 @@ public class SlackMessage implements MessageElement { | |
final String from; | ||
final String[] to; | ||
final String icon; | ||
final String text; | ||
final Attachment[] attachments; | ||
String text; | ||
Attachment[] attachments; | ||
|
||
public SlackMessage(String from, String[] to, String icon) { | ||
this(from, to, icon, null, null); // Empty text and empty attachments | ||
} | ||
|
||
public SlackMessage(String from, String[] to, String icon, String text) { | ||
this(from, to, icon, text, null); // Empty attachments | ||
} | ||
|
||
public SlackMessage(String from, String[] to, String icon, String text, Attachment[] attachments) { | ||
this.from = from; | ||
|
@@ -57,6 +65,10 @@ public Attachment[] getAttachments() { | |
return attachments; | ||
} | ||
|
||
public void setText(String text) { this.text = text;} | ||
|
||
public void setAttachments(Attachment[] attachments) { this.attachments = attachments;} | ||
|
||
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. see previous comment about 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. Will fix. |
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
|
@@ -202,7 +214,7 @@ public SlackMessage render(String watchId, String actionId, TextTemplateEngine e | |
attachments.addAll(dynamicAttachments.render(engine, model, defaults.attachment)); | ||
} | ||
if (attachments == null) { | ||
return new SlackMessage(from, to, icon, text, null); | ||
return new SlackMessage(from, to, icon, text); | ||
} | ||
return new SlackMessage(from, to, icon, text, attachments.toArray(new Attachment[attachments.size()])); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -575,7 +575,7 @@ public void testUrlPathIsFiltered() throws Exception { | |
HttpResponse response = new HttpResponse(500); | ||
String path = randomAlphaOfLength(20); | ||
HttpRequest request = HttpRequest.builder("localhost", 1234).path(path).build(); | ||
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text", null); | ||
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text"); | ||
SentMessages sentMessages = new SentMessages("foo", | ||
Arrays.asList(SentMessages.SentMessage.responded("recipient", slackMessage, request, response))); | ||
|
||
|
@@ -602,6 +602,11 @@ public void testUrlPathIsFiltered() throws Exception { | |
} | ||
} | ||
|
||
public void canHaveNullTextAndAttachment() throws Exception { | ||
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. this will not get executed as a test if the method does not begin with |
||
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon"); | ||
assertThat(slackMessage.getText(), is(null)); | ||
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. This is causing a compile error for me, pls use 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. Will fix. |
||
assertThat(slackMessage.getAttachments(), is(null)); | ||
} | ||
private static void writeFieldIfNotNull(XContentBuilder builder, String field, Object value) throws IOException { | ||
if (value != null) { | ||
builder.field(field, value); | ||
|
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 is the reason for making these non final? Our objects are typically final. And assigning null to these still counts as making sure they are final. It is very atypical for us to assign setters to objects like this to set these values after construction of the object.
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.
I can correct this. There's no reason for them not to be final.