Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Slack Notifier: unfurl_links and unfurl_media option are stripped by array_filter when they are set to false #57100

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

Closed
TomCan opened this issue May 24, 2024 · 2 comments

Comments

@TomCan
Copy link

TomCan commented May 24, 2024

Symfony version(s) affected

7.x, 6.x, 5.x

Description

unfurlLinks and unfurlMedia are boolean values that end up in the $options array. When setting the explicitly to false (to prevent unfurling), they are stripped in the SlackTransport class from the $options array by filter_array, as a boolean false is considdered empty. This results in the key/value not being present in the json that is sent to Slack.

How to reproduce

Send a message:

    $slack = new Symfony\Component\Notifier\Bridge\Slack\SlackTransport('xoxb-some-token', 'some-channel');
    $opts = new \Symfony\Component\Notifier\Bridge\Slack\SlackOptions(['unfurl_links' => false]);
    $message = new \Symfony\Component\Notifier\Message\ChatMessage('Test message with a link to https://www.github.com/', $opts);
    $slack->send($message);

var_dump($options, filter_array($options))

array(3) {
  ["unfurl_links"]=>
  bool(false)
  ["channel"]=>
  string(17) "some-channel"
  ["text"]=>
  string(35) "Test message with a link to https://www.github.com/'"
}
array(3) {
  ["channel"]=>
  string(17) "some-channel"
  ["text"]=>
  string(35) "Test message with a link to https://www.github.com/'"
}

Possible Solution

Add custom callback to array_filter function that uses empty(), but explicitly keeps boolean false values

Additional Context

No response

@TomCan
Copy link
Author

TomCan commented May 24, 2024

Quick-fix would be

    'json' => array_filter($options, function($v){ return $v === false || !empty($v); }),

But I assume that this logic is used on other places as well (maybe even other Bridges), and that array_filter without callable might also remove other options (I could imagine there might be cases where you would set a field to a zero int or so).

@xabbuh
Copy link
Member

xabbuh commented May 25, 2024

Can you confirm that #57109 fixes this?

@fabpot fabpot closed this as completed May 25, 2024
fabpot added a commit that referenced this issue May 25, 2024
…(xabbuh)

This PR was merged into the 5.4 branch.

Discussion
----------

[Notifier] keep boolean options when their value is false

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #57100
| License       | MIT

Commits
-------

b664d75 keep boolean options when their value is false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants