apiextensions: fix object keys in fuzzer to exclude escape characters

Jsoniter requires this.
This commit is contained in:
Dr. Stefan Schimanski 2017-11-20 14:35:58 +01:00
parent 54a4de04c0
commit 9603696116

View File

@ -58,8 +58,24 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
case reflect.Interface, reflect.Map, reflect.Slice, reflect.Ptr:
isValue = false
}
if isValue || c.Intn(5) == 0 {
if isValue || c.Intn(10) == 0 {
c.Fuzz(vobj.Field(i).Addr().Interface())
// JSON keys must not contain escape char with our JSON codec (jsoniter)
// TODO: remove this when/if we moved from jsoniter.ConfigFastest to ConfigCompatibleWithStandardLibrary
if field.Type.Kind() == reflect.Map {
keys := append([]reflect.Value(nil), vobj.Field(i).MapKeys()...)
for _, k := range keys {
stripped := toJSONString(k.String())
if stripped == k.String() {
continue
}
// set new key
vobj.Field(i).SetMapIndex(reflect.ValueOf(stripped), vobj.Field(i).MapIndex(k))
// remove old
vobj.Field(i).SetMapIndex(k, reflect.Value{})
}
}
}
}
}
@ -109,3 +125,13 @@ func Funcs(codecs runtimeserializer.CodecFactory) []interface{} {
},
}
}
func toJSONString(s string) string {
return strings.Map(func(r rune) rune {
// replace chars which are not supported in keys by jsoniter.ConfigFastest
if r == '\\' || r == '"' {
return 'x'
}
return r
}, s)
}