[MM-30978] avoid parsing nil PostAction in SlackAttachments (#16556)
* avoid parsing nil PostAction in SlackAttachments * reflect review comments * improve tests Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
c54b262351
Коммит
bd5616557d
@@ -394,7 +394,7 @@ func (o *Post) StripActionIntegrations() {
|
||||
func (o *Post) GetAction(id string) *PostAction {
|
||||
for _, attachment := range o.Attachments() {
|
||||
for _, action := range attachment.Actions {
|
||||
if action.Id == id {
|
||||
if action != nil && action.Id == id {
|
||||
return action
|
||||
}
|
||||
}
|
||||
@@ -409,7 +409,7 @@ func (o *Post) GenerateActionIds() {
|
||||
if attachments, ok := o.GetProp("attachments").([]*SlackAttachment); ok {
|
||||
for _, attachment := range attachments {
|
||||
for _, action := range attachment.Actions {
|
||||
if action.Id == "" {
|
||||
if action != nil && action.Id == "" {
|
||||
action.Id = NewId()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -557,6 +557,14 @@ func (o *Post) Attachments() []*SlackAttachment {
|
||||
if enc, err := json.Marshal(attachment); err == nil {
|
||||
var decoded SlackAttachment
|
||||
if json.Unmarshal(enc, &decoded) == nil {
|
||||
i := 0
|
||||
for _, action := range decoded.Actions {
|
||||
if action != nil {
|
||||
decoded.Actions[i] = action
|
||||
i++
|
||||
}
|
||||
}
|
||||
decoded.Actions = decoded.Actions[:i]
|
||||
ret = append(ret, &decoded)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -884,3 +884,48 @@ func TestSearchParameterFromJson(t *testing.T) {
|
||||
require.Equal(t, "test", *params.Terms)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPostAttachments(t *testing.T) {
|
||||
p := &Post{
|
||||
Props: map[string]interface{}{
|
||||
"attachments": []byte(`[{
|
||||
"actions" : {null}
|
||||
}]
|
||||
`),
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("empty actions", func(t *testing.T) {
|
||||
p.Props["attachments"] = []interface{}{
|
||||
map[string]interface{}{"actions": []interface{}{}},
|
||||
}
|
||||
attachments := p.Attachments()
|
||||
require.Empty(t, attachments[0].Actions)
|
||||
})
|
||||
|
||||
t.Run("a couple of actions", func(t *testing.T) {
|
||||
p.Props["attachments"] = []interface{}{
|
||||
map[string]interface{}{"actions": []interface{}{
|
||||
map[string]interface{}{"id": "test1"}, map[string]interface{}{"id": "test2"}},
|
||||
},
|
||||
}
|
||||
|
||||
attachments := p.Attachments()
|
||||
require.Len(t, attachments[0].Actions, 2)
|
||||
require.Equal(t, attachments[0].Actions[0].Id, "test1")
|
||||
require.Equal(t, attachments[0].Actions[1].Id, "test2")
|
||||
})
|
||||
|
||||
t.Run("should ignore null actions", func(t *testing.T) {
|
||||
p.Props["attachments"] = []interface{}{
|
||||
map[string]interface{}{"actions": []interface{}{
|
||||
map[string]interface{}{"id": "test1"}, nil, map[string]interface{}{"id": "test2"}, nil, nil},
|
||||
},
|
||||
}
|
||||
|
||||
attachments := p.Attachments()
|
||||
require.Len(t, attachments[0].Actions, 2)
|
||||
require.Equal(t, attachments[0].Actions[0].Id, "test1")
|
||||
require.Equal(t, attachments[0].Actions[1].Id, "test2")
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user