mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-19 15:57:48 +08:00
feat: use ShouldBindWith after ShouldBindBodyWith
This commit is contained in:
parent
db9174ae0c
commit
c49dbd7305
22
context.go
22
context.go
@ -5,6 +5,7 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -33,7 +34,6 @@ const (
|
|||||||
MIMEPOSTForm = binding.MIMEPOSTForm
|
MIMEPOSTForm = binding.MIMEPOSTForm
|
||||||
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm
|
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm
|
||||||
MIMEYAML = binding.MIMEYAML
|
MIMEYAML = binding.MIMEYAML
|
||||||
BodyBytesKey = "_gin-gonic/gin/bodybyteskey"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const abortIndex int8 = math.MaxInt8 / 2
|
const abortIndex int8 = math.MaxInt8 / 2
|
||||||
@ -666,19 +666,15 @@ func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error {
|
|||||||
// NOTE: This method reads the body before binding. So you should use
|
// NOTE: This method reads the body before binding. So you should use
|
||||||
// ShouldBindWith for better performance if you need to call only once.
|
// ShouldBindWith for better performance if you need to call only once.
|
||||||
func (c *Context) ShouldBindBodyWith(obj interface{}, bb binding.BindingBody) (err error) {
|
func (c *Context) ShouldBindBodyWith(obj interface{}, bb binding.BindingBody) (err error) {
|
||||||
var body []byte
|
var buf bytes.Buffer
|
||||||
if cb, ok := c.Get(BodyBytesKey); ok {
|
tee := io.TeeReader(c.Request.Body, &buf)
|
||||||
if cbb, ok := cb.([]byte); ok {
|
body, err := ioutil.ReadAll(tee)
|
||||||
body = cbb
|
if err != nil {
|
||||||
}
|
return err
|
||||||
}
|
|
||||||
if body == nil {
|
|
||||||
body, err = ioutil.ReadAll(c.Request.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c.Set(BodyBytesKey, body)
|
|
||||||
}
|
}
|
||||||
|
defer c.Request.Body.Close()
|
||||||
|
|
||||||
|
c.Request.Body = ioutil.NopCloser(&buf)
|
||||||
return bb.BindBody(body, obj)
|
return bb.BindBody(body, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1651,31 +1651,38 @@ func TestContextShouldBindBodyWith(t *testing.T) {
|
|||||||
Bar string `json:"bar" xml:"bar" binding:"required"`
|
Bar string `json:"bar" xml:"bar" binding:"required"`
|
||||||
}
|
}
|
||||||
for _, tt := range []struct {
|
for _, tt := range []struct {
|
||||||
name string
|
name string
|
||||||
bindingA, bindingB binding.BindingBody
|
bindingA, bindingB binding.BindingBody
|
||||||
bodyA, bodyB string
|
bodyA, bodyB string
|
||||||
|
contentTypeA, contentTypeB string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "JSON & JSON",
|
name: "JSON & JSON",
|
||||||
bindingA: binding.JSON,
|
bindingA: binding.JSON,
|
||||||
bindingB: binding.JSON,
|
bindingB: binding.JSON,
|
||||||
bodyA: `{"foo":"FOO"}`,
|
bodyA: `{"foo":"FOO"}`,
|
||||||
bodyB: `{"bar":"BAR"}`,
|
bodyB: `{"bar":"BAR"}`,
|
||||||
|
contentTypeA: binding.MIMEJSON,
|
||||||
|
contentTypeB: binding.MIMEJSON,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "JSON & XML",
|
name: "JSON & XML",
|
||||||
bindingA: binding.JSON,
|
bindingA: binding.JSON,
|
||||||
bindingB: binding.XML,
|
bindingB: binding.XML,
|
||||||
bodyA: `{"foo":"FOO"}`,
|
contentTypeA: binding.MIMEJSON,
|
||||||
|
contentTypeB: binding.MIMEXML,
|
||||||
|
bodyA: `{"foo":"FOO"}`,
|
||||||
bodyB: `<?xml version="1.0" encoding="UTF-8"?>
|
bodyB: `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<root>
|
<root>
|
||||||
<bar>BAR</bar>
|
<bar>BAR</bar>
|
||||||
</root>`,
|
</root>`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "XML & XML",
|
name: "XML & XML",
|
||||||
bindingA: binding.XML,
|
bindingA: binding.XML,
|
||||||
bindingB: binding.XML,
|
bindingB: binding.XML,
|
||||||
|
contentTypeA: binding.MIMEXML,
|
||||||
|
contentTypeB: binding.MIMEXML,
|
||||||
bodyA: `<?xml version="1.0" encoding="UTF-8"?>
|
bodyA: `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<root>
|
<root>
|
||||||
<foo>FOO</foo>
|
<foo>FOO</foo>
|
||||||
@ -1694,6 +1701,7 @@ func TestContextShouldBindBodyWith(t *testing.T) {
|
|||||||
c.Request, _ = http.NewRequest(
|
c.Request, _ = http.NewRequest(
|
||||||
"POST", "http://example.com", bytes.NewBufferString(tt.bodyA),
|
"POST", "http://example.com", bytes.NewBufferString(tt.bodyA),
|
||||||
)
|
)
|
||||||
|
c.Request.Header.Add("Content-Type", tt.contentTypeA)
|
||||||
// When it binds to typeA and typeB, it finds the body is
|
// When it binds to typeA and typeB, it finds the body is
|
||||||
// not typeB but typeA.
|
// not typeB but typeA.
|
||||||
objA := typeA{}
|
objA := typeA{}
|
||||||
@ -1702,6 +1710,9 @@ func TestContextShouldBindBodyWith(t *testing.T) {
|
|||||||
objB := typeB{}
|
objB := typeB{}
|
||||||
assert.Error(t, c.ShouldBindBodyWith(&objB, tt.bindingB))
|
assert.Error(t, c.ShouldBindBodyWith(&objB, tt.bindingB))
|
||||||
assert.NotEqual(t, typeB{"BAR"}, objB)
|
assert.NotEqual(t, typeB{"BAR"}, objB)
|
||||||
|
objB2 := typeB{}
|
||||||
|
assert.Error(t, c.ShouldBind(&objB2))
|
||||||
|
assert.NotEqual(t, typeB{"BAR"}, objB2)
|
||||||
}
|
}
|
||||||
// bodyB to typeA and typeB
|
// bodyB to typeA and typeB
|
||||||
{
|
{
|
||||||
@ -1712,12 +1723,16 @@ func TestContextShouldBindBodyWith(t *testing.T) {
|
|||||||
c.Request, _ = http.NewRequest(
|
c.Request, _ = http.NewRequest(
|
||||||
"POST", "http://example.com", bytes.NewBufferString(tt.bodyB),
|
"POST", "http://example.com", bytes.NewBufferString(tt.bodyB),
|
||||||
)
|
)
|
||||||
|
c.Request.Header.Add("Content-Type", tt.contentTypeB)
|
||||||
objA := typeA{}
|
objA := typeA{}
|
||||||
assert.Error(t, c.ShouldBindBodyWith(&objA, tt.bindingA))
|
assert.Error(t, c.ShouldBindBodyWith(&objA, tt.bindingA))
|
||||||
assert.NotEqual(t, typeA{"FOO"}, objA)
|
assert.NotEqual(t, typeA{"FOO"}, objA)
|
||||||
objB := typeB{}
|
objB := typeB{}
|
||||||
assert.NoError(t, c.ShouldBindBodyWith(&objB, tt.bindingB))
|
assert.NoError(t, c.ShouldBindBodyWith(&objB, tt.bindingB))
|
||||||
assert.Equal(t, typeB{"BAR"}, objB)
|
assert.Equal(t, typeB{"BAR"}, objB)
|
||||||
|
objB2 := typeB{}
|
||||||
|
assert.NoError(t, c.ShouldBind(&objB2))
|
||||||
|
assert.Equal(t, typeB{"BAR"}, objB2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user