cloudwego / hertz

Go HTTP framework with high-performance and strong-extensibility for building micro-services.
https://www.cloudwego.io
Apache License 2.0
5.12k stars 496 forks source link

绑定与校验设置默认值什么时候生效, 我在json body设置有值, 但是会被默认值覆盖 #1088

Closed crosstime1986 closed 5 months ago

crosstime1986 commented 5 months ago

Describe the bug 我在方法中使用绑定和验证的方法, 在req struct 中某个字段设置默认值, 实际上我请求json body 后, 我请求的值被默认值覆盖了。想请教一下有什么解决方法?

To Reproduce

// CreateMenu .
// @router /v1/member/menu_create [POST]
func CreateMenu(ctx context.Context, c *app.RequestContext) {
    var err error
    var req member.CreateMenuRequest
    err = c.BindAndValidate(&req)
    if err != nil {
        c.String(consts.StatusBadRequest, err.Error())
        return
    }

    resp := new(member.CreateMenuResponse)
    resp.StatusCode = 0
    resp.StatusMsg = "success"
    defer func() {
        c.JSON(consts.StatusOK, resp)
    }()

    if req.Data == nil {
        resp.StatusCode = -10003
        resp.StatusMsg = "data 为空"
        return
    }

    hlog.CtxDebugf(ctx, "%+v", req)

    menu1 := &orm_gen.Menu{
        Name:        req.Data.Name,
        Orders:      req.Data.Order,
        Visible:     req.Data.Visible,
        ParentID:    req.Data.ParentId,
        URL:         req.Data.Url,
        Description: req.Data.Description,
        Left:        cast.ToInt32(req.Data.Left),
        Right:       cast.ToInt32(req.Data.Right),
    }

    err = query.Menu.Create(menu1)

    if err != nil {
        // 创建用户失败,返回错误信息
        resp.StatusCode = -10002
        resp.StatusMsg = err.Error()
        return
    }

    resp.Data = req.Data
}

menu.pb.go


type Menu struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    Id          int64  `protobuf:"varint,1,opt,name=id,proto3" json:"id" path:"id" vd:"$>=0"`
    Name        string `protobuf:"bytes,2,opt,name=name,proto3" json:"name" form:"name" query:"name"`
    Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description" form:"description" query:"description"`
    Url         string `protobuf:"bytes,4,opt,name=url,proto3" json:"url" default:"http://www.163.com" form:"url" query:"url"`
    Order       int32  `protobuf:"varint,5,opt,name=order,proto3" json:"order" form:"order" query:"order"`
    Visible     bool   `protobuf:"varint,6,opt,name=visible,proto3" json:"visible" form:"visible" query:"visible"`
    ParentId    int32  `protobuf:"varint,7,opt,name=parent_id,json=parentId,proto3" json:"parent_id" form:"parent_id" query:"parent_id"`
    Left        *int32 `protobuf:"varint,8,opt,name=left,proto3,oneof" json:"left" form:"left" query:"left"`
    Right       *int32 `protobuf:"varint,9,opt,name=right,proto3,oneof" json:"right" form:"right" query:"right"`
}

image

Hertz version:

v0.8.1

Environment:

GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/liangzy/Library/Caches/go-build"
GOENV="/Users/liangzy/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/liangzy/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/liangzy/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/liangzy/go/src/hertz_demo/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/n7/t4xlgdq574v8xtc1dfyztg0h0000gn/T/go-build614023532=/tmp/go-build -gno-record-gcc-switches -fno-common"
li-jin-gou commented 5 months ago

cc @FGYFFFF ,帮看下这个 case ,感觉不应该。

FGYFFFF commented 5 months ago

代码中错误分配了优先级;修复正在合码中 https://github.com/cloudwego/hertz/pull/1056; 抱拳了

FGYFFFF commented 5 months ago

@crosstime1986 已合入,请 go get develop 分支测试一下

crosstime1986 commented 5 months ago

@crosstime1986 已合入,请 go get develop 分支测试一下

好的