Refine error message for json length exceed max length (#26676)

Signed-off-by: cai.zhang <cai.zhang@zilliz.com>
This commit is contained in:
cai.zhang 2023-09-04 17:47:48 +08:00 committed by GitHub
parent b2e7cbdf4b
commit 622077f9ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 15 deletions

View File

@ -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

View File

@ -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)
})
}