Skip to content

handle nil attachments in a safe way - #239

Closed
jmanian wants to merge 4 commits into
slack-ruby:masterfrom
jmanian:master
Closed

jmanian wants to merge 4 commits into
slack-ruby:masterfrom
jmanian:master

Conversation

@jmanian

@jmanian jmanian commented Dec 4, 2018 •

Copy link
Copy Markdown
Collaborator

In our app we started receiving some invalid_attachments_format errors for certain workspaces, and I tracked it down to a few spots where we pass attachments: nil into chat_postMessage. I took a look in here to see what happens when you pass attachments: nil and the answer is that the JSON string for attachments is null. I was able to get around this error by simply omitting attachments instead.

We've actually have been doing this for a long time and it's never been a problem before, so I contacted Slack about it and they've identified it as a bug related to a beta feature for certain workspaces.

But there's really no good reason to ever send null for attachments, so I thought it might make sense to avoid that.

The change to chat_update is perhaps less self-explanatory than chat_postMessage and chat_ephemeral. Here's how chat_update behaves with attachments:

  • omit attachments:
    • If there are existing attachments leave them alone
    • If there are no existing attachments don't add any
  • pass attachments as "[]" or null:
    • Remove any existing attachments, if they exist
    • If there are no existing attachments don't add any

Anyway, I'm curious if you think this is an appropriate safeguard to add to this library, or not.

@jmanian
jmanian requested review from alexagranov and dblock December 4, 2018 00:34
@alexagranov

Copy link
Copy Markdown
Contributor

hi @jmanian - I think this makes sense to add here. My only quibble is that the same comment may not really be appropriate for chat_update - it may be enough to simply mention that a chat_update caller may actually want to be updating with an empty set of attachments.

But I have to defer to @dblock as he's the real owner here and I'm just helping out when I can so far.

attachments = options[:attachments]
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
options = options.merge(attachments: attachments)
if options[:attachments].nil?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This file is generated by a rake task (see the top line), you have to apply this as a patch.

if options[:attachments].nil?
# sometimes sending "null" for "attachments" leads to an error
# when it works it has the same effect as sending no attachments at all
options.delete(:attachments)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You should not be modifying the incoming options Hash, modify a copy, options = options.except(:attachments) for example.

@dblock dblock left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Any such changes should be in patches, https://github.com/slack-ruby/slack-ruby-client/tree/018dcfdea5a4bfb63310ac16e0dfceb1827816f4/lib/slack/web/api/patches.

Generally it would maybe make sense to have a kind of option in templates that takes standard treatment such as being removed if null or blank?.

@jmanian

jmanian commented Dec 5, 2018

Copy link
Copy Markdown
Collaborator Author

I got word from Slack that the bug causing "null" attachments to yield invalid_attachments_format has been fixed. So now I'm not so sure if these changes are warranted. Maybe we should allow sending "null" via passing nil, in case some day that's meaningful.

@dblock

dblock commented Dec 6, 2018

Copy link
Copy Markdown
Collaborator

Want to just close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants