MM-21481: Fixed several issues from user marshalling (#13627)
* MM-21481: Fixed several issues from user marshalling - Fixed the root cause for panic by properly converting the map to a User struct. - Added a check for type conversion for extra safety. - Fixed a somewhat unrelated issue of a pointer to pointer reference. * Fix tests
Этот коммит содержится в:
коммит произвёл
Claudio Costa
родитель
87eb7697f9
Коммит
2a28edcd93
@@ -4,6 +4,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -194,6 +195,15 @@ func WebSocketEventFromJson(data io.Reader) *WebSocketEvent {
|
||||
return nil
|
||||
}
|
||||
ev.Event = o.Event
|
||||
if u, ok := o.Data["user"]; ok {
|
||||
// We need to convert to and from JSON again
|
||||
// because the user is in the form of a map[string]interface{}.
|
||||
buf, err := json.Marshal(u)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
o.Data["user"] = UserFromJson(bytes.NewReader(buf))
|
||||
}
|
||||
ev.Data = o.Data
|
||||
ev.Broadcast = o.Broadcast
|
||||
ev.Sequence = o.Sequence
|
||||
|
||||
@@ -12,14 +12,20 @@ import (
|
||||
)
|
||||
|
||||
func TestWebSocketEvent(t *testing.T) {
|
||||
m := NewWebSocketEvent("some_event", NewId(), NewId(), NewId(), nil)
|
||||
userId := NewId()
|
||||
m := NewWebSocketEvent("some_event", NewId(), NewId(), userId, nil)
|
||||
m.Add("RootId", NewId())
|
||||
user := &User{
|
||||
Id: userId,
|
||||
}
|
||||
m.Add("user", user)
|
||||
json := m.ToJson()
|
||||
result := WebSocketEventFromJson(strings.NewReader(json))
|
||||
|
||||
require.True(t, m.IsValid(), "should be valid")
|
||||
require.Equal(t, m.GetBroadcast().TeamId, result.GetBroadcast().TeamId, "Ids do not match")
|
||||
require.Equal(t, m.GetData()["RootId"], result.GetData()["RootId"], "Ids do not match")
|
||||
require.Equal(t, m.GetBroadcast().TeamId, result.GetBroadcast().TeamId, "Team ids do not match")
|
||||
require.Equal(t, m.GetData()["RootId"], result.GetData()["RootId"], "Root ids do not match")
|
||||
require.Equal(t, m.GetData()["user"].(*User).Id, result.GetData()["user"].(*User).Id, "User ids do not match")
|
||||
}
|
||||
|
||||
func TestWebSocketEventImmutable(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user