Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve block info ID type error compatibility

Danielbfc643a86b8c
diff --git a/kernel/api/contract_block_info_decode_test.go b/kernel/api/contract_block_info_decode_test.go
new file mode 100644
index 0000000000..1be7d97a06
--- /dev/null
+++ b/kernel/api/contract_block_info_decode_test.go
@@ -0,0 +1,33 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+)
+
+func TestAPIContractGetBlockInfoIDTypeMessage(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ engine := gin.New()
+ engine.POST("/api/block/getBlockInfo", getBlockInfo)
+ for _, value := range []string{`42`, `true`, `[]`, `{}`} {
+ recorder := httptest.NewRecorder()
+ request := httptest.NewRequest(http.MethodPost, "/api/block/getBlockInfo", strings.NewReader(`{"id":`+value+`}`))
+ request.Header.Set("Content-Type", "application/json")
+ engine.ServeHTTP(recorder, request)
+ var result struct {
+ Code int `json:"code"`
+ Msg string `json:"msg"`
+ }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil {
+ t.Fatal(err)
+ }
+ if recorder.Code != http.StatusOK || result.Code != -1 || result.Msg != "Field [id] should be of type [String]" {
+ t.Fatalf("id=%s: status=%d body=%s", value, recorder.Code, recorder.Body.String())
+ }
+ }
+}
diff --git a/kernel/apicontract/block_info_decode.go b/kernel/apicontract/block_info_decode.go
new file mode 100644
index 0000000000..477e0eeaf1
--- /dev/null
+++ b/kernel/apicontract/block_info_decode.go
@@ -0,0 +1,21 @@
+package apicontract
+
+import (
+ "encoding/json"
+ "errors"
+ "io"
+ "reflect"
+)
+
+func init() {
+ endpoint := GetBlockInfo
+ GetBlockInfo.decodeRequest = func(reader io.Reader) (BlockInfoRequest, error) {
+ request, err := endpoint.Decode(reader)
+ var typeError *json.UnmarshalTypeError
+ // 块信息入口保留 ID 类型错误的既有消息。
+ if errors.As(err, &typeError) && typeError.Type.Kind() == reflect.String {
+ return request, errors.New("Field [id] should be of type [String]")
+ }
+ return request, err
+ }
+}
diff --git a/kernel/apicontract/block_info_decode_test.go b/kernel/apicontract/block_info_decode_test.go
new file mode 100644
index 0000000000..dd7dabb4db
--- /dev/null
+++ b/kernel/apicontract/block_info_decode_test.go
@@ -0,0 +1,15 @@
+package apicontract
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestGetBlockInfoIDTypeMessage(t *testing.T) {
+ for _, value := range []string{`42`, `true`, `[]`, `{}`} {
+ _, err := GetBlockInfo.Decode(strings.NewReader(`{"id":` + value + `}`))
+ if err == nil || err.Error() != "Field [id] should be of type [String]" {
+ t.Errorf("id=%s: unexpected error %v", value, err)
+ }
+ }
+}