MM-13826 Webhooks: Allow "true"/"false" for bool values in payload body (#10114)
* Webhooks: Allow "true"/"false" for bool values in payload body Some slack integrations encode bool fields as "true"/"false", which was previously unsupported in mattermost due to how encoding/json works. This commit adds an aliased type for bool that implements json.Unmarshaler to maintain compatibility with Slack. * Add missing copyright header to added files
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
684e4cb007
Коммит
e961b4cd0d
30
model/slack_compatibility.go
Обычный файл
30
model/slack_compatibility.go
Обычный файл
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SlackCompatibleBool is an alias for bool that implements json.Unmarshaler
|
||||
type SlackCompatibleBool bool
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler
|
||||
//
|
||||
// Slack allows bool values to be represented as strings ("true"/"false") or
|
||||
// literals (true/false). To maintain compatibility, we define an Unmarshaler
|
||||
// that supports both.
|
||||
func (b *SlackCompatibleBool) UnmarshalJSON(data []byte) error {
|
||||
value := strings.ToLower(string(data))
|
||||
if value == "true" || value == `"true"` {
|
||||
*b = true
|
||||
} else if value == "false" || value == `"false"` {
|
||||
*b = false
|
||||
} else {
|
||||
return fmt.Errorf("unmarshal: unable to convert %s to bool", data)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Ссылка в новой задаче
Block a user