Skip to content

Commit 995fa8e

Browse files
JINNOUCHI Yasushiappleboy
JINNOUCHI Yasushi
authored andcommitted
Fix #216: Enable to call binding multiple times in some formats (#1341)
* Add interface to read body bytes in binding * Add BindingBody implementation for some binding * Fix to use `BindBodyBytesKey` for key * Revert "Fix to use `BindBodyBytesKey` for key" This reverts commit 2c82901. * Use private-like key for body bytes * Add tests for BindingBody & ShouldBindBodyWith * Add note for README * Remove redundant space between sentences
1 parent 6e09ef0 commit 995fa8e

9 files changed

+279
-10
lines changed

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
5252
- [Graceful restart or stop](#graceful-restart-or-stop)
5353
- [Build a single binary with templates](#build-a-single-binary-with-templates)
5454
- [Bind form-data request with custom struct](#bind-form-data-request-with-custom-struct)
55+
- [Try to bind body into different structs](#try-to-bind-body-into-different-structs)
5556
- [Testing](#testing)
5657
- [Users](#users--)
5758

@@ -1554,6 +1555,64 @@ type StructZ struct {
15541555

15551556
In a word, only support nested custom struct which have no `form` now.
15561557

1558+
### Try to bind body into different structs
1559+
1560+
The normal methods for binding request body consumes `c.Request.Body` and they
1561+
cannot be called multiple times.
1562+
1563+
```go
1564+
type formA struct {
1565+
Foo string `json:"foo" xml:"foo" binding:"required"`
1566+
}
1567+
1568+
type formB struct {
1569+
Bar string `json:"bar" xml:"bar" binding:"required"`
1570+
}
1571+
1572+
func SomeHandler(c *gin.Context) {
1573+
objA := formA{}
1574+
objB := formB{}
1575+
// This c.ShouldBind consumes c.Request.Body and it cannot be reused.
1576+
if errA := c.ShouldBind(&objA); errA == nil {
1577+
c.String(http.StatusOK, `the body should be formA`)
1578+
// Always an error is occurred by this because c.Request.Body is EOF now.
1579+
} else if errB := c.ShouldBind(&objB); errB == nil {
1580+
c.String(http.StatusOK, `the body should be formB`)
1581+
} else {
1582+
...
1583+
}
1584+
}
1585+
```
1586+
1587+
For this, you can use `c.ShouldBindBodyWith`.
1588+
1589+
```go
1590+
func SomeHandler(c *gin.Context) {
1591+
objA := formA{}
1592+
objB := formB{}
1593+
// This reads c.Request.Body and stores the result into the context.
1594+
if errA := c.ShouldBindBodyWith(&objA, binding.JSON); errA == nil {
1595+
c.String(http.StatusOK, `the body should be formA`)
1596+
// At this time, it reuses body stored in the context.
1597+
} else if errB := c.ShouldBindBodyWith(&objB, binding.JSON); errB == nil {
1598+
c.String(http.StatusOK, `the body should be formB JSON`)
1599+
// And it can accepts other formats
1600+
} else if errB2 := c.ShouldBindBodyWith(&objB, binding.XML); errB2 == nil {
1601+
c.String(http.StatusOK, `the body should be formB XML`)
1602+
} else {
1603+
...
1604+
}
1605+
}
1606+
```
1607+
1608+
* `c.ShouldBindBodyWith` stores body into the context before binding. This has
1609+
a slight impact to performance, so you should not use this method if you are
1610+
enough to call binding at once.
1611+
* This feature is only needed for some formats -- `JSON`, `XML`, `MsgPack`,
1612+
`ProtoBuf`. For other formats, `Query`, `Form`, `FormPost`, `FormMultipart`,
1613+
can be called by `c.ShouldBind()` multiple times without any damage to
1614+
performance (See [#1341](https://github.com/gin-gonic/gin/pull/1341)).
1615+
15571616
## Testing
15581617

15591618
The `net/http/httptest` package is preferable way for HTTP testing.

binding/binding.go

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ type Binding interface {
2929
Bind(*http.Request, interface{}) error
3030
}
3131

32+
// BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
33+
// but it reads the body from supplied bytes instead of req.Body.
34+
type BindingBody interface {
35+
Binding
36+
BindBody([]byte, interface{}) error
37+
}
38+
3239
// StructValidator is the minimal interface which needs to be implemented in
3340
// order for it to be used as the validator engine for ensuring the correctness
3441
// of the reqest. Gin provides a default implementation for this using

binding/binding_body_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package binding
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"testing"
7+
8+
"github.com/gin-gonic/gin/binding/example"
9+
"github.com/golang/protobuf/proto"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/ugorji/go/codec"
12+
)
13+
14+
func TestBindingBody(t *testing.T) {
15+
for _, tt := range []struct {
16+
name string
17+
binding BindingBody
18+
body string
19+
want string
20+
}{
21+
{
22+
name: "JSON bidning",
23+
binding: JSON,
24+
body: `{"foo":"FOO"}`,
25+
},
26+
{
27+
name: "XML bidning",
28+
binding: XML,
29+
body: `<?xml version="1.0" encoding="UTF-8"?>
30+
<root>
31+
<foo>FOO</foo>
32+
</root>`,
33+
},
34+
{
35+
name: "MsgPack binding",
36+
binding: MsgPack,
37+
body: msgPackBody(t),
38+
},
39+
} {
40+
t.Logf("testing: %s", tt.name)
41+
req := requestWithBody("POST", "/", tt.body)
42+
form := FooStruct{}
43+
body, _ := ioutil.ReadAll(req.Body)
44+
assert.NoError(t, tt.binding.BindBody(body, &form))
45+
assert.Equal(t, FooStruct{"FOO"}, form)
46+
}
47+
}
48+
49+
func msgPackBody(t *testing.T) string {
50+
test := FooStruct{"FOO"}
51+
h := new(codec.MsgpackHandle)
52+
buf := bytes.NewBuffer(nil)
53+
assert.NoError(t, codec.NewEncoder(buf, h).Encode(test))
54+
return buf.String()
55+
}
56+
57+
func TestBindingBodyProto(t *testing.T) {
58+
test := example.Test{
59+
Label: proto.String("FOO"),
60+
}
61+
data, _ := proto.Marshal(&test)
62+
req := requestWithBody("POST", "/", string(data))
63+
form := example.Test{}
64+
body, _ := ioutil.ReadAll(req.Body)
65+
assert.NoError(t, ProtoBuf.BindBody(body, &form))
66+
assert.Equal(t, test, form)
67+
}

binding/json.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package binding
66

77
import (
8+
"bytes"
9+
"io"
810
"net/http"
911

1012
"github.com/gin-gonic/gin/json"
@@ -22,7 +24,15 @@ func (jsonBinding) Name() string {
2224
}
2325

2426
func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
25-
decoder := json.NewDecoder(req.Body)
27+
return decodeJSON(req.Body, obj)
28+
}
29+
30+
func (jsonBinding) BindBody(body []byte, obj interface{}) error {
31+
return decodeJSON(bytes.NewReader(body), obj)
32+
}
33+
34+
func decodeJSON(r io.Reader, obj interface{}) error {
35+
decoder := json.NewDecoder(r)
2636
if EnableDecoderUseNumber {
2737
decoder.UseNumber()
2838
}

binding/msgpack.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package binding
66

77
import (
8+
"bytes"
9+
"io"
810
"net/http"
911

1012
"github.com/ugorji/go/codec"
@@ -17,7 +19,16 @@ func (msgpackBinding) Name() string {
1719
}
1820

1921
func (msgpackBinding) Bind(req *http.Request, obj interface{}) error {
20-
if err := codec.NewDecoder(req.Body, new(codec.MsgpackHandle)).Decode(&obj); err != nil {
22+
return decodeMsgPack(req.Body, obj)
23+
}
24+
25+
func (msgpackBinding) BindBody(body []byte, obj interface{}) error {
26+
return decodeMsgPack(bytes.NewReader(body), obj)
27+
}
28+
29+
func decodeMsgPack(r io.Reader, obj interface{}) error {
30+
cdc := new(codec.MsgpackHandle)
31+
if err := codec.NewDecoder(r, cdc).Decode(&obj); err != nil {
2132
return err
2233
}
2334
return validate(obj)

binding/protobuf.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@ func (protobufBinding) Name() string {
1717
return "protobuf"
1818
}
1919

20-
func (protobufBinding) Bind(req *http.Request, obj interface{}) error {
21-
20+
func (b protobufBinding) Bind(req *http.Request, obj interface{}) error {
2221
buf, err := ioutil.ReadAll(req.Body)
2322
if err != nil {
2423
return err
2524
}
25+
return b.BindBody(buf, obj)
26+
}
2627

27-
if err = proto.Unmarshal(buf, obj.(proto.Message)); err != nil {
28+
func (protobufBinding) BindBody(body []byte, obj interface{}) error {
29+
if err := proto.Unmarshal(body, obj.(proto.Message)); err != nil {
2830
return err
2931
}
30-
31-
//Here it's same to return validate(obj), but util now we cann't add `binding:""` to the struct
32-
//which automatically generate by gen-proto
32+
// Here it's same to return validate(obj), but util now we cann't add
33+
// `binding:""` to the struct which automatically generate by gen-proto
3334
return nil
34-
//return validate(obj)
35+
// return validate(obj)
3536
}

binding/xml.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package binding
66

77
import (
8+
"bytes"
89
"encoding/xml"
10+
"io"
911
"net/http"
1012
)
1113

@@ -16,7 +18,14 @@ func (xmlBinding) Name() string {
1618
}
1719

1820
func (xmlBinding) Bind(req *http.Request, obj interface{}) error {
19-
decoder := xml.NewDecoder(req.Body)
21+
return decodeXML(req.Body, obj)
22+
}
23+
24+
func (xmlBinding) BindBody(body []byte, obj interface{}) error {
25+
return decodeXML(bytes.NewReader(body), obj)
26+
}
27+
func decodeXML(r io.Reader, obj interface{}) error {
28+
decoder := xml.NewDecoder(r)
2029
if err := decoder.Decode(obj); err != nil {
2130
return err
2231
}

context.go

+25
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131
MIMEPlain = binding.MIMEPlain
3232
MIMEPOSTForm = binding.MIMEPOSTForm
3333
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm
34+
BodyBytesKey = "_gin-gonic/gin/bodybyteskey"
3435
)
3536

3637
const abortIndex int8 = math.MaxInt8 / 2
@@ -508,6 +509,30 @@ func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error {
508509
return b.Bind(c.Request, obj)
509510
}
510511

512+
// ShouldBindBodyWith is similar with ShouldBindWith, but it stores the request
513+
// body into the context, and reuse when it is called again.
514+
//
515+
// NOTE: This method reads the body before binding. So you should use
516+
// ShouldBindWith for better performance if you need to call only once.
517+
func (c *Context) ShouldBindBodyWith(
518+
obj interface{}, bb binding.BindingBody,
519+
) (err error) {
520+
var body []byte
521+
if cb, ok := c.Get(BodyBytesKey); ok {
522+
if cbb, ok := cb.([]byte); ok {
523+
body = cbb
524+
}
525+
}
526+
if body == nil {
527+
body, err = ioutil.ReadAll(c.Request.Body)
528+
if err != nil {
529+
return err
530+
}
531+
c.Set(BodyBytesKey, body)
532+
}
533+
return bb.BindBody(body, obj)
534+
}
535+
511536
// ClientIP implements a best effort algorithm to return the real client IP, it parses
512537
// X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
513538
// Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.

context_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/gin-contrib/sse"
21+
"github.com/gin-gonic/gin/binding"
2122
"github.com/stretchr/testify/assert"
2223
"golang.org/x/net/context"
2324
)
@@ -1334,6 +1335,85 @@ func TestContextBadAutoShouldBind(t *testing.T) {
13341335
assert.False(t, c.IsAborted())
13351336
}
13361337

1338+
func TestContextShouldBindBodyWith(t *testing.T) {
1339+
type typeA struct {
1340+
Foo string `json:"foo" xml:"foo" binding:"required"`
1341+
}
1342+
type typeB struct {
1343+
Bar string `json:"bar" xml:"bar" binding:"required"`
1344+
}
1345+
for _, tt := range []struct {
1346+
name string
1347+
bindingA, bindingB binding.BindingBody
1348+
bodyA, bodyB string
1349+
}{
1350+
{
1351+
name: "JSON & JSON",
1352+
bindingA: binding.JSON,
1353+
bindingB: binding.JSON,
1354+
bodyA: `{"foo":"FOO"}`,
1355+
bodyB: `{"bar":"BAR"}`,
1356+
},
1357+
{
1358+
name: "JSON & XML",
1359+
bindingA: binding.JSON,
1360+
bindingB: binding.XML,
1361+
bodyA: `{"foo":"FOO"}`,
1362+
bodyB: `<?xml version="1.0" encoding="UTF-8"?>
1363+
<root>
1364+
<bar>BAR</bar>
1365+
</root>`,
1366+
},
1367+
{
1368+
name: "XML & XML",
1369+
bindingA: binding.XML,
1370+
bindingB: binding.XML,
1371+
bodyA: `<?xml version="1.0" encoding="UTF-8"?>
1372+
<root>
1373+
<foo>FOO</foo>
1374+
</root>`,
1375+
bodyB: `<?xml version="1.0" encoding="UTF-8"?>
1376+
<root>
1377+
<bar>BAR</bar>
1378+
</root>`,
1379+
},
1380+
} {
1381+
t.Logf("testing: %s", tt.name)
1382+
// bodyA to typeA and typeB
1383+
{
1384+
w := httptest.NewRecorder()
1385+
c, _ := CreateTestContext(w)
1386+
c.Request, _ = http.NewRequest(
1387+
"POST", "http://example.com", bytes.NewBufferString(tt.bodyA),
1388+
)
1389+
// When it binds to typeA and typeB, it finds the body is
1390+
// not typeB but typeA.
1391+
objA := typeA{}
1392+
assert.NoError(t, c.ShouldBindBodyWith(&objA, tt.bindingA))
1393+
assert.Equal(t, typeA{"FOO"}, objA)
1394+
objB := typeB{}
1395+
assert.Error(t, c.ShouldBindBodyWith(&objB, tt.bindingB))
1396+
assert.NotEqual(t, typeB{"BAR"}, objB)
1397+
}
1398+
// bodyB to typeA and typeB
1399+
{
1400+
// When it binds to typeA and typeB, it finds the body is
1401+
// not typeA but typeB.
1402+
w := httptest.NewRecorder()
1403+
c, _ := CreateTestContext(w)
1404+
c.Request, _ = http.NewRequest(
1405+
"POST", "http://example.com", bytes.NewBufferString(tt.bodyB),
1406+
)
1407+
objA := typeA{}
1408+
assert.Error(t, c.ShouldBindBodyWith(&objA, tt.bindingA))
1409+
assert.NotEqual(t, typeA{"FOO"}, objA)
1410+
objB := typeB{}
1411+
assert.NoError(t, c.ShouldBindBodyWith(&objB, tt.bindingB))
1412+
assert.Equal(t, typeB{"BAR"}, objB)
1413+
}
1414+
}
1415+
}
1416+
13371417
func TestContextGolangContext(t *testing.T) {
13381418
c, _ := CreateTestContext(httptest.NewRecorder())
13391419
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))

0 commit comments

Comments
 (0)