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

判断是否传递某参数 #68

Open
bootun opened this issue Oct 9, 2024 · 2 comments
Open

判断是否传递某参数 #68

bootun opened this issue Oct 9, 2024 · 2 comments

Comments

@bootun
Copy link

bootun commented Oct 9, 2024

在写proto方式的项目时,proto里定义的bool, int无法判断前端是传了默认值还是没传。
使用google.protobuf.BoolValue这种包装类型时,生成的pb文件又无法正常解析HTTP请求参数。

@zhufuyi
Copy link
Owner

zhufuyi commented Oct 10, 2024

可以通过validate检查前端是否传参数,如果不传会返回参数错误,示例如下

import "google/protobuf/wrappers.proto";
import "validate/validate.proto";

message CheckRequest{
  google.protobuf.BoolValue isLocal = 1 [(validate.rules).message.required = true];
}

请求参数

curl -X 'PUT' \
  'http://localhost:8080/api/v1/check' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{"isLocal": {"value":false}}'

如果想要在swagger上请求,需要人工修改apis.swagger.json文件,改为对应go结构体

        "isLocal": {
	    "type": "object",
            "properties": {
                "value": {
                    "type": "boolean"
                }
            }
        }

@bootun
Copy link
Author

bootun commented Oct 10, 2024

所以这样写的话,更新必须是全量更新对吧,没办法只单独修改某个字段

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