AlexStocks commented on code in PR #1054: URL: https://github.com/apache/dubbo-go-pixiu/pull/1054#discussion_r4505144735
########## pkg/client/dubbo/generic.go: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dubbo + +import ( + "encoding/json" + "strings" +) + +import ( + "github.com/pkg/errors" +) + +import ( + cst "github.com/apache/dubbo-go-pixiu/pkg/common/constant" +) + +// supportedGenericModes lists the generic invocation modes accepted in +// integrationRequest.generic. Values outside this set are rejected instead of +// falling back to the map mode, because a silent fallback would change the +// request contract without telling the caller. +var supportedGenericModes = []string{ + cst.GenericModeMap, + cst.GenericModeGson, + cst.GenericModeProtobufJSON, + cst.GenericModeBean, +} + +// NormalizeGenericMode canonicalizes the configured generic invocation mode. +// An empty value selects the map mode, which is what dubbo-go applies when a +// reference does not request a specific mode. +func NormalizeGenericMode(mode string) (string, error) { + normalized := strings.ToLower(strings.TrimSpace(mode)) + if normalized == "" { + return cst.GenericModeMap, nil + } + for _, supported := range supportedGenericModes { + if normalized == supported { + return normalized, nil + } + } + return "", errors.Errorf("generic mode %q is not supported, expected one of %s", + mode, strings.Join(supportedGenericModes, ", ")) +} + +// IsJSONTextGenericMode reports whether the mode carries the request as JSON +// text instead of a list of typed arguments. The provider realizes that text +// into its own request type, which is what lets a caller without generated +// code invoke a protobuf defined service. +func IsJSONTextGenericMode(mode string) bool { + switch strings.ToLower(strings.TrimSpace(mode)) { + case cst.GenericModeGson, cst.GenericModeProtobufJSON: + return true + default: + return false + } +} + +// EncodeJSONTextArgument renders one mapped value as the JSON text that a JSON +// text mode sends. Values that are already text are passed through so a caller +// can hand over the exact payload. +func EncodeJSONTextArgument(value any) (string, error) { + if text, ok := value.(string); ok { Review Comment: 🟡 **`string` 直接透传,不做 JSON 合法性校验 —— 这是入参侧最容易被踩的地方。** ```go if text, ok := value.(string); ok { return text, nil } ``` 后果分成两半: - 调用方传 `{"a":1}`(合法 JSON 文本)→ 正常,这是注释里说的「hand over the exact payload」; - 调用方传普通字符串(比如 `mappingParams` 从 query 里取到的 `hello`)→ 原样发出去,provider 端 gson/protobuf-json 解码直接失败,报的却是 provider 内部的解析错误,排查成本很高。 更隐蔽的是**同一份 HTTP body 的语义漂移**:body 是 JSON 字符串 `"hello"` 时,`value` 是 `hello`(已解码)→ 透传成 `hello`(非法 JSON);而 body 是对象时 → `json.Marshal` 后是合法 JSON。同一份契约两种结果。 建议二选一: ```go // A. 严格:字符串也必须能当 JSON 文本解析 if text, ok := value.(string); ok { if !json.Valid([]byte(text)) { return "", errors.Errorf("generic JSON argument is not valid JSON text: %q", text) } return text, nil } ``` ```go // B. 一律 marshal(字符串会被正确加引号),把"透传原始载荷"作为显式能力另开一个开关 encoded, err := json.Marshal(value) ``` 如果保留透传,请在文档里写清「text 模式下字符串值会被当成已序列化的 JSON 载荷」,否则这个坑一定会有人踩。 ########## pkg/client/dubbo/generic.go: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dubbo + +import ( + "encoding/json" + "strings" +) + +import ( + "github.com/pkg/errors" +) + +import ( + cst "github.com/apache/dubbo-go-pixiu/pkg/common/constant" +) + +// supportedGenericModes lists the generic invocation modes accepted in +// integrationRequest.generic. Values outside this set are rejected instead of +// falling back to the map mode, because a silent fallback would change the +// request contract without telling the caller. +var supportedGenericModes = []string{ + cst.GenericModeMap, + cst.GenericModeGson, + cst.GenericModeProtobufJSON, + cst.GenericModeBean, +} + +// NormalizeGenericMode canonicalizes the configured generic invocation mode. +// An empty value selects the map mode, which is what dubbo-go applies when a +// reference does not request a specific mode. +func NormalizeGenericMode(mode string) (string, error) { + normalized := strings.ToLower(strings.TrimSpace(mode)) + if normalized == "" { + return cst.GenericModeMap, nil + } + for _, supported := range supportedGenericModes { + if normalized == supported { + return normalized, nil + } + } + return "", errors.Errorf("generic mode %q is not supported, expected one of %s", + mode, strings.Join(supportedGenericModes, ", ")) +} + +// IsJSONTextGenericMode reports whether the mode carries the request as JSON +// text instead of a list of typed arguments. The provider realizes that text +// into its own request type, which is what lets a caller without generated +// code invoke a protobuf defined service. +func IsJSONTextGenericMode(mode string) bool { + switch strings.ToLower(strings.TrimSpace(mode)) { + case cst.GenericModeGson, cst.GenericModeProtobufJSON: + return true + default: + return false + } +} + +// EncodeJSONTextArgument renders one mapped value as the JSON text that a JSON +// text mode sends. Values that are already text are passed through so a caller +// can hand over the exact payload. +func EncodeJSONTextArgument(value any) (string, error) { + if text, ok := value.(string); ok { + return text, nil + } + encoded, err := json.Marshal(value) + if err != nil { + return "", errors.Wrap(err, "encode generic JSON argument") + } + return string(encoded), nil +} + +// UnwrapJSONTextResult turns the JSON text a JSON text mode returns into raw +// bytes, so the HTTP layer writes it as a JSON body instead of a quoted string. +// Values that are not valid JSON text stay untouched. +func UnwrapJSONTextResult(value any) any { + text, ok := value.(string) + if !ok || !json.Valid([]byte(text)) { Review Comment: 🟡 **`json.Valid` 会把「JSON 字符串结果」也当成可解包的文本,导致响应被破坏。** `json.Valid([]byte("hello"))` 是 false(OK),但 `json.Valid([]byte(`"hello"`))` 是 **true**。所以 provider 返回一个 JSON 字符串(Java 方法返回 `String` 时,gson 模式的响应文本就是 `"hello"` 带引号)时: 1. 这里判定 valid → 返回 `[]byte(`"hello"`)`; 2. `call.go` 侧把它当作 JSON body 直接写出(`pkg/filter/http/remote/call.go:184-190`); 3. HTTP 客户端收到的是裸 `hello` —— 引号没了,既不是合法 JSON,也和 provider 实际返回值不同。 同样的问题也适用于标量:`123`、`true`、`null` 都会从「JSON 文本」变成裸字面量(这三个恰好还能被解析,但语义上已经不是「JSON body」了)。 建议只在「解包后仍是一个 JSON 文档」时才换成裸字节,也就是限制为 object / array: ```go func UnwrapJSONTextResult(value any) any { text, ok := value.(string) if !ok { return value } trimmed := strings.TrimSpace(text) if len(trimmed) == 0 { return value } switch trimmed[0] { case '{', '[': if json.Valid([]byte(trimmed)) { return []byte(trimmed) } } return value } ``` 这样字符串/数字/布尔结果会走原来的 `NewDubboResponse` 序列化路径,被正确转义成 JSON 字符串。 ########## pkg/common/constant/key.go: ########## @@ -115,3 +115,17 @@ 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. + GenericModeMap = "true" Review Comment: 🟡 **`GenericModeMap` 的字面量是 `"true"`,名实不符;而且 YAML 里 `generic: true` 会直接解析失败。** 两个独立的问题: 1. 常量名叫 `GenericModeMap`,值是 `"true"`。看代码的人会自然地去写 `generic: map`(然后被 `NormalizeGenericMode` 拒绝,因为 `supportedGenericModes` 里没有 `"map"`)。建议要么加 `"map"` 作为别名,要么把常量改名成 `GenericModeDefault`,并在错误信息里列出可接受值。 2. 更实际的问题:`DubboBackendConfig.Generic` 是 `string`,而 YAML 1.1/1.2 里裸 `true` 是 **bool**。用户看到文档写 `"true"`(带引号)没问题,但 `true` 这个值在 Dubbo 生态里太标准了,几乎必然有人不加引号写: ```yaml integrationRequest: generic: true # -> yaml: unmarshal errors: cannot unmarshal !!bool `true` into string ``` 而报错信息是通用 unmarshal 错误,用户很难联想到「要加引号」。 两种收法: ```go // A. 自定义类型,吃掉 bool 标量 type GenericMode string func (m *GenericMode) UnmarshalYAML(unmarshal func(any) error) error { var raw any if err := unmarshal(&raw); err != nil { return err } switch v := raw.(type) { case bool: if v { *m = GenericModeTrue } else { return errors.New("generic: false is not supported") } case string: *m = GenericMode(v) default: return errors.Errorf("generic: unsupported value %v", raw) } return nil } ``` ```go // B. 最低成本:在文档和示例里显式给"不加引号会失败"的提示,并在 Apply/校验时报错信息里带上取值样例 ``` B 更省事,但 A 更符合「配置应该宽容」的直觉。不管选哪个,`"map"` 别名建议加上——它比 `"true"` 更像是这个模式的名字。 ########## pkg/filter/http/remote/dubbo_handler.go: ########## @@ -146,9 +153,17 @@ func (h *DubboHandler) applyMapping(state *outboundBuildState, req *http.Request return errors.Errorf("Parameter mapping %v incorrect", mp) } - converted, err := clientdubbo.MapTypes(mp.MapType, value) - if err != nil { - return err + // JSON text modes carry the request as text, so the declared type of a + // positional mapping describes the message instead of a scalar to convert. + var converted any + if clientdubbo.IsJSONTextGenericMode(state.generic) { Review Comment: 💭 text 模式下这里**完全跳过** `MapTypes` 转换,也就是 `mappingParams[].mapType` 在这两个模式里被静默忽略。 这可能是刻意的(文本模式的参数就是一个 JSON 文档,不该按标量转换),但有两个后果值得在文档/注释里说明: 1. 同一条 API 配置从 `generic: true` 切到 `generic: gson` 时,`mapType` 会从「生效」变成「无声失效」——用户会以为是 bug; 2. 如果 `mapType` 写错了(拼写错误),在 map 模式下会立刻报错,在 text 模式下则永远发现不了。 建议在这里补一句注释,并在 `docs/sample/dubbo/dubbo-universality.md` 的模式表里加一行「text 模式下 `mapType` 不参与标量转换,声明类型仅描述请求消息」。 -- 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]
