CAICAIIs commented on code in PR #1054:
URL: https://github.com/apache/dubbo-go-pixiu/pull/1054#discussion_r4505499142


##########
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:
   位置映射的 `mapType` 现在会作为声明类型使用,文本模式不再推断。`finalizeJSONTextArguments` 取类型的顺序是 
`parameterTypes` → `opt.types` → 位置映射写进 `state.paramTypes` 的 
`mapType`;三者都没有时直接报 `generic mode gson requires parameterTypes to name the 
request message`,直连模式仍然是 `direct generic invoke requires 
parameterTypes`。registry 模式不再被 `InferJavaClassNames` 覆盖成 
`java.lang.String`,同一份配置在两种部署方式下行为一致。
   
   用例按你的提醒补了只给 `mapType`、不给 `parameterTypes` 
的一条:`TestBuildOutboundJSONTextGenericMode/positional mapping declares the 
request message` 断言 `Arguments` 是 `{"name":"test"}`、`ParamTypes` 是 
`com.demo.HelloRequest`;另有 `missing declared type is rejected` 覆盖三者都没有的情况。提交 
0d7954a,`go test ./pkg/filter/http/remote/... ./pkg/client/dubbo/... -count=1` 
通过。
   



##########
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` 现在是声明类型之一,所以文档那句改成「消息类型可由 
`parameterTypes`、`opt.types` 或位置映射的 `mapType` 声明,三者都没有时请求被拒绝,因为 JSON 文本本身无法告诉 
Java provider 查找哪个方法签名」,中英文都改了。
   
   措辞你也说对了:只有字符串值是逐字节透传,其它类型的值走 `json.Marshal` 
重新编码,所以对象键按字典序排列、空白被压缩。文档现在分开写这两种情况,并说明 `mapType` 在这两个模式下不参与标量转换。
   



##########
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` 从支持集合里移除,`NormalizeGenericMode("bean")` 现在和其它未支持的值一样报 
`generic mode "bean" is not supported, expected one of true, map, gson, 
protobuf-json`。常量 `GenericModeBean` 一并删除,两份 universality 文档的取值表也去掉了这一行。
   
   理由就是你写的:`BeanGeneralizer` 需要 `JavaBeanDescriptor`,pixiu 侧没有构造它的代码,接受这个值只会把 
map 模式的参数换个标签发出去,对 Java 的 `BeanGenericService` 会在 realize 阶段类型不匹配。等有 descriptor 
的构造和用例再放开。
   



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

Reply via email to