mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-12 16:48:19 +08:00
Compare commits
7 Commits
d798baf148
...
ef68d3b2e3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef68d3b2e3 | ||
|
|
db309081bc | ||
|
|
ba093d1947 | ||
|
|
1b414bd54e | ||
|
|
cafe8d9acf | ||
|
|
47a2602fa0 | ||
|
|
7b7f800b2c |
2
.github/workflows/goreleaser.yml
vendored
2
.github/workflows/goreleaser.yml
vendored
@ -21,7 +21,7 @@ jobs:
|
||||
with:
|
||||
go-version: "^1"
|
||||
- name: Run GoReleaser
|
||||
uses: goreleaser/goreleaser-action@v6
|
||||
uses: goreleaser/goreleaser-action@v7
|
||||
with:
|
||||
# either 'goreleaser' (default) or 'goreleaser-pro'
|
||||
distribution: goreleaser
|
||||
|
||||
@ -21,7 +21,7 @@ import (
|
||||
"github.com/gin-gonic/gin/testdata/protoexample"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
type bsonBinding struct{}
|
||||
|
||||
@ -250,19 +250,24 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
|
||||
switch value.Kind() {
|
||||
case reflect.Slice:
|
||||
if len(vs) == 0 {
|
||||
if vs == nil {
|
||||
if !opt.isDefaultExists {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
vs = []string{opt.defaultValue}
|
||||
// pre-process the default value for multi if present
|
||||
cfTag := field.Tag.Get("collection_format")
|
||||
if cfTag == "" || cfTag == "multi" {
|
||||
vs = strings.Split(opt.defaultValue, ",")
|
||||
} else {
|
||||
vs = []string{opt.defaultValue}
|
||||
}
|
||||
}
|
||||
|
||||
if len(vs) == 0 {
|
||||
return true, setSlice(vs, value, field, opt)
|
||||
}
|
||||
|
||||
if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok {
|
||||
return ok, err
|
||||
} else if ok, err = trySetCustom(vs[0], value); ok {
|
||||
@ -280,11 +285,12 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
return false, nil
|
||||
}
|
||||
|
||||
vs = []string{opt.defaultValue}
|
||||
// pre-process the default value for multi if present
|
||||
cfTag := field.Tag.Get("collection_format")
|
||||
if cfTag == "" || cfTag == "multi" {
|
||||
vs = strings.Split(opt.defaultValue, ",")
|
||||
} else {
|
||||
vs = []string{opt.defaultValue}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -915,7 +915,7 @@ func TestMappingCustomSliceOfSliceUnmarshalTextDefault(t *testing.T) {
|
||||
var s struct {
|
||||
FileData []customPathUnmarshalText `form:"path,default=bar/foo;bar/foo/spam,parser=encoding.TextUnmarshaler" collection_format:"csv"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"path": {}}, "form")
|
||||
err := mappingByPtr(&s, formSource{"path": nil}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []customPathUnmarshalText{{"bar", "foo"}, {"bar", "foo", "spam"}}, s.FileData)
|
||||
}
|
||||
@ -994,7 +994,7 @@ func TestMappingCustomArrayOfArrayUnmarshalTextDefault(t *testing.T) {
|
||||
var s struct {
|
||||
FileData []objectIDUnmarshalText `form:"ids,default=664a062ac74a8ad104e0e80e;664a062ac74a8ad104e0e80f,parser=encoding.TextUnmarshaler" collection_format:"csv"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"ids": {}}, "form")
|
||||
err := mappingByPtr(&s, formSource{"ids": nil}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []objectIDUnmarshalText{id1, id2}, s.FileData)
|
||||
}
|
||||
@ -1079,7 +1079,7 @@ func TestMappingEmptyValues(t *testing.T) {
|
||||
// field present but empty
|
||||
err = mappingByPtr(&s, formSource{"slice": {}}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []int{5}, s.Slice)
|
||||
assert.Equal(t, []int{}, s.Slice)
|
||||
|
||||
// field present with values
|
||||
err = mappingByPtr(&s, formSource{"slice": {"1", "2", "3"}}, "form")
|
||||
@ -1108,10 +1108,15 @@ func TestMappingEmptyValues(t *testing.T) {
|
||||
Slice []int `form:"slice"`
|
||||
}
|
||||
|
||||
// field present but empty
|
||||
err := mappingByPtr(&s, formSource{"slice": {}}, "form")
|
||||
// field not present
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []int(nil), s.Slice)
|
||||
|
||||
// field present but empty
|
||||
err = mappingByPtr(&s, formSource{"slice": {}}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []int{}, s.Slice)
|
||||
})
|
||||
|
||||
t.Run("array without default", func(t *testing.T) {
|
||||
@ -1140,7 +1145,7 @@ func TestMappingEmptyValues(t *testing.T) {
|
||||
// field present but empty
|
||||
err = mappingByPtr(&s, formSource{"slice_multi": {}, "slice_csv": {}}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []int{1, 2, 3}, s.SliceMulti)
|
||||
assert.Equal(t, []int{1, 2, 3}, s.SliceCsv)
|
||||
assert.Equal(t, []int{}, s.SliceMulti)
|
||||
assert.Equal(t, []int{}, s.SliceCsv)
|
||||
})
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ import (
|
||||
testdata "github.com/gin-gonic/gin/testdata/protoexample"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
|
||||
15
docs/doc.md
15
docs/doc.md
@ -22,6 +22,7 @@
|
||||
- [How to write log file](#how-to-write-log-file)
|
||||
- [Custom Log Format](#custom-log-format)
|
||||
- [Controlling Log output coloring](#controlling-log-output-coloring)
|
||||
- [Avoid logging query strings](#avoid-loging-query-strings)
|
||||
- [Model binding and validation](#model-binding-and-validation)
|
||||
- [Custom Validators](#custom-validators)
|
||||
- [Only Bind Query String](#only-bind-query-string)
|
||||
@ -592,6 +593,20 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### Avoid logging query strings
|
||||
|
||||
```go
|
||||
func main() {
|
||||
router := gin.New()
|
||||
|
||||
// SkipQueryString indicates that the logger should not log the query string.
|
||||
// For example, /path?q=1 will be logged as /path
|
||||
loggerConfig := gin.LoggerConfig{SkipQueryString: true}
|
||||
|
||||
router.Use(gin.LoggerWithConfig(loggerConfig))
|
||||
}
|
||||
```
|
||||
|
||||
### Model binding and validation
|
||||
|
||||
To bind a request body into a type, use model binding. We currently support binding of JSON, XML, YAML, TOML and standard form values (foo=bar&boo=baz).
|
||||
|
||||
2
go.mod
2
go.mod
@ -17,7 +17,7 @@ require (
|
||||
github.com/quic-go/quic-go v0.59.0
|
||||
github.com/stretchr/testify v1.11.1
|
||||
github.com/ugorji/go/codec v1.3.1
|
||||
go.mongodb.org/mongo-driver v1.17.9
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0
|
||||
golang.org/x/net v0.50.0
|
||||
google.golang.org/protobuf v1.36.10
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@ -71,8 +71,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY=
|
||||
github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
go.mongodb.org/mongo-driver v1.17.9 h1:IexDdCuuNJ3BHrELgBlyaH9p60JXAvdzWR128q+U5tU=
|
||||
go.mongodb.org/mongo-driver v1.17.9/go.mod h1:LlOhpH5NUEfhxcAwG0UEkMqwYcc4JU18gtCdGudk/tQ=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
|
||||
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
|
||||
|
||||
@ -48,6 +48,11 @@ type LoggerConfig struct {
|
||||
// Optional.
|
||||
SkipPaths []string
|
||||
|
||||
// SkipQueryString indicates that query strings should not be written
|
||||
// for cases such as when API keys are passed via query strings.
|
||||
// Optional. Default value is false.
|
||||
SkipQueryString bool
|
||||
|
||||
// Skip is a Skipper that indicates which logs should not be written.
|
||||
// Optional.
|
||||
Skip Skipper
|
||||
@ -298,7 +303,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
||||
|
||||
param.BodySize = c.Writer.Size()
|
||||
|
||||
if raw != "" {
|
||||
if raw != "" && !conf.SkipQueryString {
|
||||
path = path + "?" + raw
|
||||
}
|
||||
|
||||
|
||||
@ -471,3 +471,17 @@ func TestForceConsoleColor(t *testing.T) {
|
||||
// reset console color mode.
|
||||
consoleColorMode = autoColor
|
||||
}
|
||||
|
||||
func TestLoggerWithConfigSkipQueryString(t *testing.T) {
|
||||
buffer := new(strings.Builder)
|
||||
router := New()
|
||||
router.Use(LoggerWithConfig(LoggerConfig{
|
||||
Output: buffer,
|
||||
SkipQueryString: true,
|
||||
}))
|
||||
router.GET("/logged", func(c *Context) { c.Status(http.StatusOK) })
|
||||
|
||||
PerformRequest(router, "GET", "/logged?a=21")
|
||||
assert.Contains(t, buffer.String(), "200")
|
||||
assert.NotContains(t, buffer.String(), "a=21")
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ package render
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
// BSON contains the given interface object.
|
||||
|
||||
@ -19,7 +19,7 @@ import (
|
||||
testdata "github.com/gin-gonic/gin/testdata/protoexample"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user