Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何使用 “自定义字段 tag” #45

Open
achilsh opened this issue Sep 9, 2024 · 1 comment
Open

如何使用 “自定义字段 tag” #45

achilsh opened this issue Sep 9, 2024 · 1 comment

Comments

@achilsh
Copy link

achilsh commented Sep 9, 2024

Is your feature request related to a problem?

  1. 我设置了 proto 如下:

syntax = "proto3";
package helloworld;

import "trpc/proto/trpc_options.proto";
import "trpc/swagger/swagger.proto";
import "validate/validate.proto";
option go_package = "trpc-http-demo/helloworld";

// HelloRequest is hello request.
message HelloRequest {
string msg = 1 [ (trpc.go_tag)='json:"xxx"', (validate.rules).string.email=true];
}

// HelloResponse is hello response.
message HelloResponse {
string msg = 1;
}

// HelloWorldService handles hello request and echo message.
service HelloWorldService {
// Hello says hello.
//@alias="/api/helloworld"
rpc Hello(HelloRequest) returns(HelloResponse);
}

  1. 运行命令行阐述 项目:
    trpc create --protocol http -p ./hello-world.proto -f -o . --alias --gotag --validate

  2. 修改配置文件,http端口号为9100

  3. 使用http client 调用:
    5
    image

  4. 服务端错误:
    2024-09-10 00:09:06.615 DEBUG server/service.go:235 service: helloworld.HelloWorldService handle err (if caused by health checking, this error can be ignored): type:framework, code:1, msg:service codec Unmarshal: proto: (line 2:5): unknown field "xxx"

What is the solution you'd like?

What alternatives have you considered?

Could you provide additional context?

@WineChord
Copy link
Contributor

The default serialization type for application/json is codec.SerializationTypeJSON. Its implementation is jsonpb. When the request struct to be unmarshaled on comes from protobuf pb stub code, it will use jsonpb serializer, which ignores the json:"xxx" tag.

Possible fix: re-register codec.SerializationTypeJSON with std/json or jsoniter (used by the framework), such as:

func main() {
    codec.RegisterSerializer(SerializationTypeJSON, &codec.JSONSerialization{})
    // trpc.NewServer() ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants