Skip to content
Myr

fix: check obj type in protobufBinding (#2851)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: check obj type in protobufBinding * fix: UnitTest for invalid proto obj

authored by Tevic · committed by GitHub · parent deb83b6 · Browse files · 2 files+13 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−1 removed

  • other:+13 added−1 removed

2 files+13 added−1 removed

modified:binding/binding_test.go+7 added
1337 unmodified lines
1338 req.Header.Add("Content-Type", MIMEPROTOBUF)1339 err := b.Bind(req, &obj)1340 assert.Error(t, err)13411342 invalid_obj := FooStruct{}1343 req.Body = ioutil.NopCloser(strings.NewReader(`{"msg":"hello"}`))1344 req.Header.Add("Content-Type", MIMEPROTOBUF)1345 err = b.Bind(req, &invalid_obj)1346 assert.Error(t, err)1347 assert.Equal(t, err.Error(), "obj is not ProtoMessage")13481349 obj = protoexample.Test{}1350 req = requestWithBody("POST", badPath, badBody)1351 req.Header.Add("Content-Type", MIMEPROTOBUF)1352 err = ProtoBuf.Bind(req, &obj)
7 unmodified lines
modified:binding/protobuf.go+6 added-1 removed
3 unmodified lines
45package binding67import (8 "errors"9 "io/ioutil"10 "net/http"1112 "google.golang.org/protobuf/proto"
13 unmodified lines
26 return b.BindBody(buf, obj)27}2829func (protobufBinding) BindBody(body []byte, obj interface{}) error {30 msg, ok := obj.(proto.Message)31 if !ok {32 return errors.New("obj is not ProtoMessage")33 }29 if err := proto.Unmarshal(body, obj.(proto.Message)); err != nil {34 if err := proto.Unmarshal(body, msg); err != nil {35 return err36 }37 // Here it's same to return validate(obj), but util now we can't add38 // `binding:""` to the struct which automatically generate by gen-proto
3 unmodified lines