Skip to content

Commit 5dde801

Browse files
authored
fix(block): marshal context elements from values (#1706)
Encode `ContextBlock` values correctly when passed through the `Block` interface. Previously, pointer blocks produced the required `"elements":[...]` array, while value blocks produced `"elements":{"Elements":[...]}`. Use a value receiver for `ContextElements.MarshalJSON` so both forms use the existing array encoder. Add a regression test covering value and pointer blocks and an Unreleased changelog entry. Validation: the regression reproduced the incorrect JSON before the fix. Focused block/context race tests, formatting, lint, module tidy, and whitespace checks passed. The expected shape matches [Slack's context block reference](https://docs.slack.dev/reference/block-kit/blocks/context-block/).
1 parent 0b8dc72 commit 5dde801

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Prevent repeated escaping and data races when reusing a `MsgOptionText` option.
1313
- Preserve plus signs and percent signs in slacktest message blocks and attachments.
14+
- Encode context block elements as an array when a block is passed by value.
1415
- Keep slacktest bot identities independent when generating RTM connection information.
1516
- Preserve huddle state, expiration, and call ID in Events API user profiles.
1617
- Fix retry jitter panics for large durations on 32-bit platforms.

‎block_context_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package slack
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
79
)
810

11+
func TestContextBlockMarshalJSON(t *testing.T) {
12+
block := NewContextBlock("context", NewTextBlockObject(PlainTextType, "hello", true, false))
13+
for _, b := range []Block{block, *block} {
14+
data, err := json.Marshal(Blocks{BlockSet: []Block{b}})
15+
require.NoError(t, err)
16+
assert.JSONEq(t, `[{"type":"context","block_id":"context","elements":[{"type":"plain_text","text":"hello","emoji":true}]}]`, string(data))
17+
}
18+
}
19+
920
func TestNewContextBlock(t *testing.T) {
1021
locationPinImage := NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/tripAgentLocationMarker.png", "Location Pin Icon")
1122
textExample := NewTextBlockObject("plain_text", "Location: Central Business District", true, false)

‎block_conv.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func toBlockElement(element *Accessory) BlockElement {
412412
}
413413

414414
// MarshalJSON encodes the underlying context element slice.
415-
func (e *ContextElements) MarshalJSON() ([]byte, error) {
415+
func (e ContextElements) MarshalJSON() ([]byte, error) {
416416
return json.Marshal(e.Elements)
417417
}
418418

0 commit comments

Comments
 (0)