修复了json schema 格式中 array下如果只有一个数据并且数据格式为对象时 解析失败bug

This commit is contained in:
zhangzeyi
2023-03-02 17:26:06 +08:00
parent e10b07ff4f
commit 9cb08b3ebc
2 changed files with 192 additions and 3 deletions

View File

@@ -173,8 +173,23 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
randomNum = int(RandInt64(int64(minVal), int64(maxVal))) randomNum = int(RandInt64(int64(minVal), int64(maxVal)))
} }
if randomNum == 1 { //随机选取一个 if randomNum == 1 {
resultMap[name] = templateList[rand.Intn(len(templateList))] if len(templateList) > 1 {
resultMap[name] = templateList[rand.Intn(len(templateList))]
continue
}
switch templateVal := templateList[0].(type) {
case map[string]interface{}:
tempMap := make(map[string]interface{})
for key, val := range templateVal {
split := strings.Split(key, "|")
tempMap[split[0]] = mockConstant(val)
}
resultMap[name] = tempMap
default:
resultMap[name] = templateVal
}
continue continue
} }

View File

@@ -1299,13 +1299,187 @@ var mock5 = `{
] ]
}` }`
var mock6 = `{
"template": {
"users|1": [
{
"email": "@email",
"name": "@name",
"ip": "@ip"
}
]
},
"type": "object",
"rule": {},
"path": [
"ROOT"
],
"properties": [
{
"name": "users",
"template": [
{
"email": "@email",
"name": "@name",
"ip": "@ip"
}
],
"type": "array",
"rule": {
"parameters": [
"users|1",
"users",
null,
"1",
null
],
"range": [
"1",
"1",
null
],
"min": 1,
"count": 1
},
"path": [
"ROOT",
"users"
],
"items": [
{
"name": 0,
"template": {
"email": "@email",
"name": "@name",
"ip": "@ip"
},
"type": "object",
"rule": {},
"path": [
"ROOT",
"users",
0
],
"properties": [
{
"name": "email",
"template": "@email",
"type": "string",
"rule": {},
"path": [
"ROOT",
"users",
0,
"email"
]
},
{
"name": "name",
"template": "@name",
"type": "string",
"rule": {},
"path": [
"ROOT",
"users",
0,
"name"
]
},
{
"name": "ip",
"template": "@ip",
"type": "string",
"rule": {},
"path": [
"ROOT",
"users",
0,
"ip"
]
}
]
}
]
}
]
}`
var mock7 = `{
"template": {
"users|1-10": [
10,
20
]
},
"type": "object",
"rule": {},
"path": [
"ROOT"
],
"properties": [
{
"name": "users",
"template": [
10,
20
],
"type": "array",
"rule": {
"parameters": [
"users|1-10",
"users",
null,
"1-10",
null
],
"range": [
"1-10",
"1",
"10"
],
"min": 1,
"max": 10,
"count": 2
},
"path": [
"ROOT",
"users"
],
"items": [
{
"name": 0,
"template": 10,
"type": "number",
"rule": {},
"path": [
"ROOT",
"users",
0
]
},
{
"name": 1,
"template": 20,
"type": "number",
"rule": {},
"path": [
"ROOT",
"users",
1
]
}
]
}
]
}`
func TestJsonSchemaMockJsUnmarshal(t *testing.T) { func TestJsonSchemaMockJsUnmarshal(t *testing.T) {
type args struct { type args struct {
valueMap interface{} valueMap interface{}
} }
valueMap := make(map[string]interface{}) valueMap := make(map[string]interface{})
json.Unmarshal([]byte(mock5), &valueMap) json.Unmarshal([]byte(mock7), &valueMap)
tests := []struct { tests := []struct {
name string name string
args args args args