From e582d851a4d271779dcdcbff6fb2e70c173d686d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Thu, 23 Jul 2026 18:55:03 +0800 Subject: [PATCH] fix: wrap Notable record fields payload --- internal/questiontable/client.go | 8 ++++++-- internal/questiontable/client_test.go | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/internal/questiontable/client.go b/internal/questiontable/client.go index ccd971d..431cca5 100644 --- a/internal/questiontable/client.go +++ b/internal/questiontable/client.go @@ -132,7 +132,11 @@ func (c *Client) BaseURL() string { return c.baseURL } // insertRecordReq is the JSON body for the Notable v1 insertRecord endpoint. type insertRecordReq struct { - Records []map[string]interface{} `json:"records"` + Records []insertRecord `json:"records"` +} + +type insertRecord struct { + Fields map[string]interface{} `json:"fields"` } // insertRecordResp is the success response body. @@ -161,7 +165,7 @@ func (c *Client) InsertRecord(ctx context.Context, row Row) (string, error) { reqURL := c.baseURL + path + "?operatorId=" + url.QueryEscape(c.operatorID) fields := row.ToFields() - body := insertRecordReq{Records: []map[string]interface{}{fields}} + body := insertRecordReq{Records: []insertRecord{{Fields: fields}}} jsonBody, err := json.Marshal(body) if err != nil { return "", fmt.Errorf("questiontable marshal: %w", err) diff --git a/internal/questiontable/client_test.go b/internal/questiontable/client_test.go index e462a95..b1a9364 100644 --- a/internal/questiontable/client_test.go +++ b/internal/questiontable/client_test.go @@ -55,10 +55,28 @@ func TestInsertRecordMethodPathHeaderBody(t *testing.T) { gotQuery = r.URL.RawQuery gotToken = r.Header.Get("x-acs-dingtalk-access-token") - var body insertRecordReq + var body map[string]interface{} if err := json.NewDecoder(r.Body).Decode(&body); err != nil { t.Errorf("decode body: %v", err) } + records, ok := body["records"].([]interface{}) + if !ok || len(records) != 1 { + t.Errorf("records must contain exactly one record: %#v", body["records"]) + } else { + record, ok := records[0].(map[string]interface{}) + if !ok { + t.Errorf("record must be an object: %#v", records[0]) + } else { + fields, ok := record["fields"].(map[string]interface{}) + if !ok { + t.Errorf("record must wrap values in a fields object: %#v", record) + } else if len(record) != 1 { + t.Errorf("record must contain only fields, got keys: %#v", record) + } else if _, ok := fields[FieldOriginalQuestion]; !ok { + t.Errorf("nested fields missing original question field") + } + } + } raw, _ := json.Marshal(body) gotBody = string(raw)