Skip to content

Commit

Permalink
fix(net/goai): change default value of RequestBody.Required from true…
Browse files Browse the repository at this point in the history
… to false, add required tag support for RequestBody (#3796)
  • Loading branch information
gqcn authored Sep 24, 2024
1 parent e15b543 commit 9af8393
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 20 deletions.
4 changes: 2 additions & 2 deletions container/glist/glist_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func TestRemove(t *testing.T) {
})
}

func TestIssue4103(t *testing.T) {
func Test_Issue4103(t *testing.T) {
l1 := New()
l1.PushBack(1)
l1.PushBack(2)
Expand All @@ -312,7 +312,7 @@ func TestIssue4103(t *testing.T) {
}
}

func TestIssue6349(t *testing.T) {
func Test_Issue6349(t *testing.T) {
l := New()
l.PushBack(1)
l.PushBack(2)
Expand Down
2 changes: 1 addition & 1 deletion internal/deepcopy/deepcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ func TestPointerToStruct(t *testing.T) {
}
}

func TestIssue9(t *testing.T) {
func Test_Issue9(t *testing.T) {
// simple pointer copy
x := 42
testA := map[string]*int{
Expand Down
1 change: 1 addition & 0 deletions net/ghttp/ghttp_server_config_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (s *Server) SetSwaggerUITemplate(swaggerUITemplate string) {
}

// SetOpenApiPath sets the OpenApiPath for server.
// For example: SetOpenApiPath("/api.json")
func (s *Server) SetOpenApiPath(path string) {
s.config.OpenApiPath = path
}
2 changes: 1 addition & 1 deletion net/ghttp/ghttp_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func (Issue3789) Say(ctx context.Context, req *Issue3789Req) (res *Issue3789Res,
}

// https://github.com/gogf/gf/issues/3789
func TestIssue3789(t *testing.T) {
func Test_Issue3789(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server()
s.Use(ghttp.MiddlewareHandlerResponse)
Expand Down
9 changes: 5 additions & 4 deletions net/goai/goai_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,16 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
if operation.RequestBody.Value == nil {
var (
requestBody = RequestBody{
Required: true,
Content: map[string]MediaType{},
Content: map[string]MediaType{},
}
)
// Supported mime types of request.
var (
contentTypes = oai.Config.ReadContentTypes
tagMimeValue = gmeta.Get(inputObject.Interface(), gtag.Mime).String()
contentTypes = oai.Config.ReadContentTypes
tagMimeValue = gmeta.Get(inputObject.Interface(), gtag.Mime).String()
tagRequiredValue = gmeta.Get(inputObject.Interface(), gtag.Required).Bool()
)
requestBody.Required = tagRequiredValue
if tagMimeValue != "" {
contentTypes = gstr.SplitAndTrim(tagMimeValue, ",")
}
Expand Down
72 changes: 72 additions & 0 deletions net/goai/goai_z_unit_issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package goai_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
)

var ctx = context.Background()

type Issue3664DefaultReq struct {
g.Meta `path:"/default" method:"post"`
Name string
}
type Issue3664DefaultRes struct{}

type Issue3664RequiredTagReq struct {
g.Meta `path:"/required-tag" required:"true" method:"post"`
Name string
}
type Issue3664RequiredTagRes struct{}

type Issue3664 struct{}

func (Issue3664) Default(ctx context.Context, req *Issue3664DefaultReq) (res *Issue3664DefaultRes, err error) {
res = &Issue3664DefaultRes{}
return
}

func (Issue3664) RequiredTag(ctx context.Context, req *Issue3664RequiredTagReq) (res *Issue3664RequiredTagRes, err error) {
res = &Issue3664RequiredTagRes{}
return
}

// https://github.com/gogf/gf/issues/3664
func Test_Issue3664(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server()
s.Use(ghttp.MiddlewareHandlerResponse)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(
new(Issue3664),
)
})
s.SetLogger(nil)
s.SetOpenApiPath("/api.json")
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
apiContent := c.GetContent(ctx, "/api.json")
j, err := gjson.LoadJson(apiContent)
t.AssertNil(err)
t.Assert(j.Get(`paths./default.post.requestBody.required`).String(), "")
t.Assert(j.Get(`paths./required-tag.post.requestBody.required`).String(), "true")
})
}
8 changes: 8 additions & 0 deletions os/glog/glog_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,19 @@ func (l *Logger) getFpFromPool(ctx context.Context, path string) *gfpool.File {

// printStd prints content `s` without stack.
func (l *Logger) printStd(ctx context.Context, level int, values ...interface{}) {
// nil logger, print nothing
if l == nil {
return
}
l.print(ctx, level, "", values...)
}

// printErr prints content `s` with stack check.
func (l *Logger) printErr(ctx context.Context, level int, values ...interface{}) {
// nil logger, print nothing
if l == nil {
return
}
var stack string
if l.config.StStatus == 1 {
stack = l.GetStack()
Expand Down
4 changes: 4 additions & 0 deletions os/glog/glog_logger_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,9 @@ func (l *Logger) Criticalf(ctx context.Context, format string, v ...interface{})

// checkLevel checks whether the given `level` could be output.
func (l *Logger) checkLevel(level int) bool {
// nil logger, print nothing
if l == nil {
return false
}
return l.config.Level&level > 0
}
24 changes: 12 additions & 12 deletions util/gconv/gconv_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// https://github.com/gogf/gf/issues/1227
func TestIssue1227(t *testing.T) {
func Test_Issue1227(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type StructFromIssue1227 struct {
Name string `json:"n1"`
Expand Down Expand Up @@ -130,7 +130,7 @@ func (f *issue1607Float64) UnmarshalValue(value interface{}) error {
return nil
}

func TestIssue1607(t *testing.T) {
func Test_Issue1607(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Demo struct {
B issue1607Float64
Expand All @@ -148,7 +148,7 @@ func TestIssue1607(t *testing.T) {
}

// https://github.com/gogf/gf/issues/1946
func TestIssue1946(t *testing.T) {
func Test_Issue1946(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type B struct {
init *gtype.Bool
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestIssue1946(t *testing.T) {
}

// https://github.com/gogf/gf/issues/2381
func TestIssue2381(t *testing.T) {
func Test_Issue2381(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Inherit struct {
Id int64 `json:"id" description:"Id"`
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestIssue2381(t *testing.T) {
}

// https://github.com/gogf/gf/issues/2391
func TestIssue2391(t *testing.T) {
func Test_Issue2391(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Inherit struct {
Ids []int
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestIssue2391(t *testing.T) {
}

// https://github.com/gogf/gf/issues/2395
func TestIssue2395(t *testing.T) {
func Test_Issue2395(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Test struct {
Num int
Expand All @@ -311,7 +311,7 @@ func TestIssue2395(t *testing.T) {
}

// https://github.com/gogf/gf/issues/2371
func TestIssue2371(t *testing.T) {
func Test_Issue2371(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
s = struct {
Expand All @@ -327,7 +327,7 @@ func TestIssue2371(t *testing.T) {
}

// https://github.com/gogf/gf/issues/2901
func TestIssue2901(t *testing.T) {
func Test_Issue2901(t *testing.T) {
type GameApp2 struct {
ForceUpdateTime *time.Time
}
Expand All @@ -342,7 +342,7 @@ func TestIssue2901(t *testing.T) {
}

// https://github.com/gogf/gf/issues/3006
func TestIssue3006(t *testing.T) {
func Test_Issue3006(t *testing.T) {
type tFF struct {
Val1 json.RawMessage `json:"val1"`
Val2 []json.RawMessage `json:"val2"`
Expand All @@ -369,7 +369,7 @@ func TestIssue3006(t *testing.T) {
}

// https://github.com/gogf/gf/issues/3731
func TestIssue3731(t *testing.T) {
func Test_Issue3731(t *testing.T) {
type Data struct {
Doc map[string]interface{} `json:"doc"`
}
Expand All @@ -389,7 +389,7 @@ func TestIssue3731(t *testing.T) {
}

// https://github.com/gogf/gf/issues/3764
func TestIssue3764(t *testing.T) {
func Test_Issue3764(t *testing.T) {
type T struct {
True bool `json:"true"`
False bool `json:"false"`
Expand All @@ -416,7 +416,7 @@ func TestIssue3764(t *testing.T) {
}

// https://github.com/gogf/gf/issues/3789
func TestIssue3789(t *testing.T) {
func Test_Issue3789(t *testing.T) {
type ItemSecondThird struct {
SecondID uint64 `json:"secondId,string"`
ThirdID uint64 `json:"thirdId,string"`
Expand Down
1 change: 1 addition & 0 deletions util/gtag/gtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
Json = "json" // Json tag is supported by stdlib.
Security = "security" // Security defines scheme for authentication. Detail to see https://swagger.io/docs/specification/authentication/
In = "in" // Swagger distinguishes between the following parameter types based on the parameter location. Detail to see https://swagger.io/docs/specification/describing-parameters/
Required = "required" // OpenAPIv3 required attribute name for request body.
)

// StructTagPriority defines the default priority tags for Map*/Struct* functions.
Expand Down

0 comments on commit 9af8393

Please sign in to comment.