AlexStocks commented on code in PR #1054:
URL: https://github.com/apache/dubbo-go-pixiu/pull/1054#discussion_r4505245834
##########
pkg/filter/http/remote/dubbo_handler.go:
##########
@@ -446,3 +493,35 @@ func (h *DubboHandler) coerceDeclaredArguments(state
*outboundBuildState) error
state.arguments = values
return nil
}
+
+// finalizeJSONTextArguments renders the mapped value as the single JSON text
+// argument that the gson and protobuf-json modes send, and keeps the declared
+// parameter types describing one message. The provider derives the argument
+// type from the method signature, so the declared type is informational here.
+func (h *DubboHandler) finalizeJSONTextArguments(state *outboundBuildState, ir
config.IntegrationRequest) error {
+ if len(state.arguments) != 1 {
+ return errors.Errorf("generic mode %s requires exactly one
argument, got %d", state.generic, len(state.arguments))
+ }
+
+ payload, err := clientdubbo.EncodeJSONTextArgument(state.arguments[0])
+ if err != nil {
+ return err
+ }
+ state.arguments = []any{payload}
+
+ switch {
+ case ir.ParameterTypes != nil:
+ state.paramTypes = append([]string(nil), ir.ParameterTypes...)
+ case state.optTypes != nil:
+ state.paramTypes = append([]string(nil), state.optTypes...)
+ case strings.TrimSpace(ir.URL) != "":
+ return errors.New("direct generic invoke requires
parameterTypes")
+ default:
+ state.paramTypes =
clientdubbo.InferJavaClassNames(state.arguments)
Review Comment:
🟡 **`default` 分支在 JSON 文本模式下必然推出 `java.lang.String`**
实测(registry 模式,不配 `url`):
```yaml
generic: gson
mappingParams:
- name: requestBody.values
mapTo: "0"
mapType: com.demo.HelloRequest
```
`BuildOutbound` 的结果是 `Arguments = ["{\"name\":\"test\"}"]`、**`ParamTypes =
["java.lang.String"]`**。原因两层:
1. `applyMapping`(:200)已经把 `mp.MapType` 写进 `state.paramTypes`,但这个 switch 只读
`ir.ParameterTypes` 和 `state.optTypes`,`state.paramTypes` 被完全忽略;
2. `state.arguments` 在 :510 已经替换成 JSON 文本,所以 `InferJavaClassNames` 拿到的永远是
string。
对照一下 `opt.values` + `mapType` 的路径**是生效的**(`applyOptMapping` 里
`state.optTypes == nil && mapType != ""` →
`normalizeOptTypes`),只有位置映射这条路把它丢了,两种映射方式行为不一致。
后果取决于 provider:Go provider 用自己方法签名的 `method.ArgsType()`(dubbo-go
`filter/generic/service_filter.go:89`),收到的 types 被忽略,所以无害;Java provider 用
`$invoke(method, parameterTypes, args)` 里的 types 去查方法签名,会以
NoSuchMethodException 失败 —— 而 Java provider 正是泛化调用的主要场景。另外 direct 模式下同一份配置会当场报
`direct generic invoke requires parameterTypes`(我实测),registry
模式却静默通过:同一个配置换个部署方式,一个立刻失败、一个在对端失败。
建议:
```go
switch {
case ir.ParameterTypes != nil:
state.paramTypes = append([]string(nil), ir.ParameterTypes...)
case state.optTypes != nil:
state.paramTypes = append([]string(nil), state.optTypes...)
case state.hasPositional && len(state.paramTypes) == 1 &&
strings.TrimSpace(state.paramTypes[0]) != "":
state.paramTypes = []string{strings.TrimSpace(state.paramTypes[0])}
case strings.TrimSpace(ir.URL) != "":
return errors.New("direct generic invoke requires parameterTypes")
default:
return errors.Errorf("generic mode %s requires parameterTypes: the
declared type names the request message", state.generic)
}
```
文本模式下不要再 infer:从 JSON 文本推类型,推出来的必然是 String,没有信息量,不如让它在 pixiu 侧带着明确信息失败。
补用例时注意——现在的 `positional mapping keeps the mapped value` 同时给了
`parameterTypes` 和 `mapType`,所以抓不到这个分支;需要一条只给 `mapType`、不给 `parameterTypes` 的用例。
##########
docs/sample/dubbo/dubbo-universality.md:
##########
@@ -126,6 +126,43 @@ Supported `mapTo` options:
- opt.values
```
+#### Generic mode
+
+`integrationRequest.generic` selects how the generic invocation carries its
payload:
+
+```yaml
+- "true" # default, map mode: values are sent as typed arguments and
the result is a map
+- map # alias of "true"; unquoted `generic: true` is accepted as well
+- gson # the request is sent as JSON text and the result is JSON text
+- protobuf-json # the request is sent as protobuf JSON text and the result is
protobuf JSON text
+- bean # Java bean compatible mode
+```
+
+The map mode keeps the `opt.values` and `opt.types` contract above: `values`
is the argument list and `types` holds the Java type name of each argument.
+
+The `gson` and `protobuf-json` modes send the whole request message as JSON
text, so `values` must contain exactly one element and `parameterTypes` must
declare exactly one type, which describes the request message itself:
+
+```yaml
+integrationRequest:
+ requestType: dubbo
+ interface: com.example.Greeter
+ method: SayHello
+ protocol: tri
+ serialization: hessian2
+ generic: protobuf-json
+ parameterTypes:
+ - com.example.HelloRequest
+ mappingParams:
+ - name: requestBody.values
+ mapTo: opt.values
+```
+
+A request body of `{"values":{"name":"test"}}` is sent as the single generic
argument `{"name":"test"}`, and the JSON text the provider returns is written
as the response body instead of a quoted string.
+
+In these two modes a mapped value of type string is taken as an already
serialized JSON document and must be valid JSON text; a plain string such as a
query parameter is rejected. Object values are serialized, and integers keep
every digit because the request body is decoded with `json.Number`. `mapType`
only names the request message in a positional mapping and takes no part in
scalar conversion here.
Review Comment:
🟡 **这句和实现对不上:位置映射的 `mapType` 目前什么也不声明**
`finalizeJSONTextArguments` 只认 `ir.ParameterTypes` 或 `state.optTypes`;位置映射写进
`state.paramTypes` 的 `mapType` 不会被读,registry 模式下还会被 `InferJavaClassNames` 覆盖成
`java.lang.String`(复现配置见 `dubbo_handler.go:520` 那条)。
要么把那条接上,要么把这句改成「text 模式下位置映射的 `mapType` 会被忽略,消息类型必须由 `parameterTypes`
声明」。中文版 :159 同样。
另外想确认一下措辞:object 值走的是 `json.Marshal` 重新编码,键序按字典序重排、空白被压缩(我实测
`{"name":"test","id":...}` → `{"id":...,"name":"test"}`)。对 protobuf-json
无所谓,但「a string value must already be valid JSON text」容易让人以为整个 body 是原样透传的 ——
实际只有字符串值透传,对象值是重建的。
##########
pkg/common/constant/key.go:
##########
@@ -115,3 +115,20 @@ const (
// GRPCRequest represents the grpc request
GRPCRequest = "grpc"
)
+
+// Generic invocation modes accepted by integrationRequest.generic.
+const (
+ // GenericModeMap is the default map based mode: values are sent as
typed
+ // arguments and the provider returns a map. dubbo-go spells the mode
"true".
+ GenericModeMap = "true"
+ // GenericModeMapAlias is the readable alias of GenericModeMap, so a
+ // configuration can write `generic: map` instead of `generic: "true"`.
+ GenericModeMapAlias = "map"
+ // GenericModeGson sends the request as JSON text and expects JSON text
back.
+ GenericModeGson = "gson"
+ // GenericModeProtobufJSON sends the request as protobuf JSON text and
+ // expects protobuf JSON text back.
+ GenericModeProtobufJSON = "protobuf-json"
+ // GenericModeBean is the Java bean compatible mode.
+ GenericModeBean = "bean"
Review Comment:
🟡 **`bean` 暴露了,但 pixiu 发不出 bean 模式需要的参数形状(待确认)**
`bean` 在 dubbo-go 里是合法值(`common/constant/key.go:425`),但 `BeanGeneralizer`
要的入参是 `JavaBeanDescriptor`:`generalizer/bean.go:175` 的 `fromDescriptor` 只对
`*JavaBeanDescriptor` 或带 `className/type/properties` 的 map 做转换,其余原样返回。
pixiu 侧没有构造该对象的代码(全仓 grep 无
`JavaBeanDescriptor`),`IsJSONTextGenericMode("bean")` 又返回 false,走的是 map 那条路径 ——
所以 `bean` 实际发出的参数和 map 模式逐字节相同。对 Go provider 就等于 map 模式;对 Java provider 的
`BeanGenericService`,bean 泛化要求参数是 descriptor,会在 realize 阶段类型不匹配。
建议二选一:
- 先从 `supportedGenericModes` 里去掉 `bean`(像 `protobuf`
那样显式拒绝,错误信息里也不再列它),等有实现和用例再放开;
- 或在文档的模式表里写清「bean 模式只对接受 map 形状入参的 provider 有效」,并补一条用例说明它和 map 模式的差别 —— 现在
`bean` 只有 `TestNormalizeGenericMode` 里一条「能通过校验」的断言,没有任何行为验证。
本机没有 Java provider,这条属于静态推演,所以标「待确认」而不是「必须改」。
--
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]