mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-11-30 02:48:45 +08:00
Refine error message for json length exceed max length (#26676)
Signed-off-by: cai.zhang <cai.zhang@zilliz.com>
This commit is contained in:
parent
b2e7cbdf4b
commit
622077f9ad
@ -284,7 +284,18 @@ func (v *validateUtil) checkJSONFieldData(field *schemapb.FieldData, fieldSchema
|
||||
}
|
||||
|
||||
if v.checkMaxLen {
|
||||
return verifyLengthPerRow(jsonArray, paramtable.Get().CommonCfg.JSONMaxLength.GetAsInt64())
|
||||
for _, s := range jsonArray {
|
||||
if int64(len(s)) > paramtable.Get().CommonCfg.JSONMaxLength.GetAsInt64() {
|
||||
if field.GetIsDynamic() {
|
||||
msg := fmt.Sprintf("the length (%d) of dynamic field exceeds max length (%d)", len(s),
|
||||
paramtable.Get().CommonCfg.JSONMaxLength.GetAsInt64())
|
||||
return merr.WrapErrParameterInvalid("valid length dynamic field", "length exceeds max length", msg)
|
||||
}
|
||||
msg := fmt.Sprintf("the length (%d) of json field (%s) exceeds max length (%d)", len(s),
|
||||
field.GetFieldName(), paramtable.Get().CommonCfg.JSONMaxLength.GetAsInt64())
|
||||
return merr.WrapErrParameterInvalid("valid length json string", "length exceeds max length", msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -1,6 +1,7 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
@ -2111,6 +2112,7 @@ func Test_validateUtil_checkIntegerFieldData(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_validateUtil_checkJSONData(t *testing.T) {
|
||||
t.Run("no json data", func(t *testing.T) {
|
||||
v := newValidateUtil(withOverflowCheck())
|
||||
|
||||
f := &schemapb.FieldSchema{
|
||||
@ -2130,4 +2132,60 @@ func Test_validateUtil_checkJSONData(t *testing.T) {
|
||||
|
||||
err := v.checkJSONFieldData(data, f)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("json string exceed max length", func(t *testing.T) {
|
||||
v := newValidateUtil(withOverflowCheck(), withMaxLenCheck())
|
||||
jsonString := ""
|
||||
for i := 0; i < Params.CommonCfg.JSONMaxLength.GetAsInt(); i++ {
|
||||
jsonString += fmt.Sprintf("key: %d, value: %d", i, i)
|
||||
}
|
||||
jsonString = "{" + jsonString + "}"
|
||||
f := &schemapb.FieldSchema{
|
||||
DataType: schemapb.DataType_JSON,
|
||||
}
|
||||
data := &schemapb.FieldData{
|
||||
FieldName: "json",
|
||||
Field: &schemapb.FieldData_Scalars{
|
||||
Scalars: &schemapb.ScalarField{
|
||||
Data: &schemapb.ScalarField_JsonData{
|
||||
JsonData: &schemapb.JSONArray{
|
||||
Data: [][]byte{[]byte(jsonString)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := v.checkJSONFieldData(data, f)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("dynamic field exceed max length", func(t *testing.T) {
|
||||
v := newValidateUtil(withOverflowCheck(), withMaxLenCheck())
|
||||
jsonString := ""
|
||||
for i := 0; i < Params.CommonCfg.JSONMaxLength.GetAsInt(); i++ {
|
||||
jsonString += fmt.Sprintf("key: %d, value: %d", i, i)
|
||||
}
|
||||
jsonString = "{" + jsonString + "}"
|
||||
f := &schemapb.FieldSchema{
|
||||
DataType: schemapb.DataType_JSON,
|
||||
}
|
||||
data := &schemapb.FieldData{
|
||||
FieldName: "$meta",
|
||||
Field: &schemapb.FieldData_Scalars{
|
||||
Scalars: &schemapb.ScalarField{
|
||||
Data: &schemapb.ScalarField_JsonData{
|
||||
JsonData: &schemapb.JSONArray{
|
||||
Data: [][]byte{[]byte(jsonString)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
IsDynamic: true,
|
||||
}
|
||||
|
||||
err := v.checkJSONFieldData(data, f)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user