AlexStocks commented on code in PR #3709:
URL: https://github.com/apache/dubbo-go/pull/3709#discussion_r3878263529
##########
filter/generic/generalizer/map.go:
##########
@@ -183,40 +187,61 @@ func objToMap(obj any) any {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
value := v.Field(i)
+ tag := parseMTag(field)
+ if tag.ignore || tag.omitEmpty && isEmptyValue(value) {
+ continue
+ }
kind := value.Kind()
if !value.CanInterface() {
logger.Debugf("[Filter][Generic] objToMap is
skipped because it couldn't be converted to interface, field=%v", field)
continue
}
valueIface := value.Interface()
+ var generalizedValue any
+ var err error
switch kind {
case reflect.Pointer:
if value.IsNil() {
- setInMap(result, field, nil)
- continue
+ generalizedValue = nil
+ break
}
- setInMap(result, field, objToMap(valueIface))
+ generalizedValue, err = objToMap(valueIface)
case reflect.Struct, reflect.Slice, reflect.Map:
if isPrimitive(valueIface) {
logger.Warnf("[Filter][Generic] %q is
primitive. Cross-language transfer (e.g., dubbo-go <-> dubbo-java) may crash.
Use basic types like string.", value.Type())
- setInMap(result, field, valueIface)
- continue
+ generalizedValue = valueIface
+ break
}
- setInMap(result, field, objToMap(valueIface))
+ generalizedValue, err = objToMap(valueIface)
default:
- setInMap(result, field, valueIface)
+ generalizedValue = valueIface
+ }
+ if err != nil {
+ return nil, err
}
+ if tag.squash {
+ squashed, ok :=
generalizedValue.(map[string]any)
+ if !ok {
+ return nil, perrors.Errorf("cannot
squash non-struct type '%s'", value.Type())
+ }
+ maps.Copy(result, squashed)
Review Comment:
[P1] `squash` 会把外层 POJO 的 `class` 覆盖成内嵌类型
`objToMap` 处理外层 POJO 时先写入 `result["class"] = pojo.JavaClassName()`;如果带
`m:",squash"` 的字段也实现了 `hessian.POJO`,递归得到的子 Map 同样包含 `class`,这里的 `maps.Copy`
会用子类值覆盖外层类型。`generic.include.class` 默认是 true,因此父、子 JavaClassName
不同时,跨语言消费端会按错误的内层 Java 类型解释整个参数。请在展开前移除子 Map 的 `class`,或复制后恢复外层 class,并补一个父子
POJO 类型名明显不同、include-class=true 的公开 `GetMapGeneralizer().Generalize` 回归测试。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]