lab4 done
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package dto
|
||||
|
||||
type CreateMessageRequest struct {
|
||||
ChatID string `json:"-"`
|
||||
Text string `json:"text"`
|
||||
ChatID string `json:"-"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type CreateMessageResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt Timestamp `json:"created_at"`
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
CreatedAt Timestamp `json:"created_at"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Keep absent attachment_ids equivalent to [], while rejecting JSON null and
|
||||
// wrong types rather than quietly treating them as an empty message body.
|
||||
func (r *CreateMessageRequest) UnmarshalJSON(data []byte) error {
|
||||
type payload CreateMessageRequest
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &fields); err != nil || fields == nil {
|
||||
return errors.New("Ожидается JSON-объект")
|
||||
}
|
||||
text, exists := fields["text"]
|
||||
if !exists || bytes.Equal(bytes.TrimSpace(text), []byte("null")) {
|
||||
return errors.New("Требуется строка text")
|
||||
}
|
||||
if ids, exists := fields["attachment_ids"]; exists && bytes.Equal(bytes.TrimSpace(ids), []byte("null")) {
|
||||
return errors.New("attachment_ids должен быть массивом")
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.DisallowUnknownFields()
|
||||
var decoded payload
|
||||
if err := decoder.Decode(&decoded); err != nil {
|
||||
return err
|
||||
}
|
||||
decoded.ChatID = r.ChatID
|
||||
*r = CreateMessageRequest(decoded)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMessageStrictFields(t *testing.T) {
|
||||
for _, body := range []string{`null`, `[]`, `{}`, `{"text":null}`, `{"text":"x","attachment_ids":null}`, `{"text":"x","attachment_ids":"x"}`, `{"text":"x","extra":1}`} {
|
||||
var r CreateMessageRequest
|
||||
if err := json.Unmarshal([]byte(body), &r); err == nil {
|
||||
t.Fatalf("accepted %s", body)
|
||||
}
|
||||
}
|
||||
for _, body := range []string{`{"text":"x"}`, `{"text":"x","attachment_ids":[]}`, `{"text":"","attachment_ids":["id"]}`} {
|
||||
var r CreateMessageRequest
|
||||
if err := json.Unmarshal([]byte(body), &r); err != nil {
|
||||
t.Fatalf("rejected %s: %v", body, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,10 @@ type ListMessagesResponse struct {
|
||||
}
|
||||
|
||||
type ListMessagesItem struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt Timestamp `json:"created_at"`
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
CreatedAt Timestamp `json:"created_at"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user