From 3a8ee2806819c182034a1cd906ec95ab3606f1d8 Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 23 Sep 2024 20:50:04 +0800 Subject: [PATCH 1/2] fix(net/goai): change default value of RequestBody.Required from true to false, add required tag support for RequestBody --- net/ghttp/ghttp_server_config_api.go | 1 + net/goai/goai_path.go | 9 ++-- net/goai/goai_z_unit_issue_test.go | 72 ++++++++++++++++++++++++++++ os/glog/glog_logger.go | 8 ++++ os/glog/glog_logger_api.go | 4 ++ util/gtag/gtag.go | 1 + 6 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 net/goai/goai_z_unit_issue_test.go diff --git a/net/ghttp/ghttp_server_config_api.go b/net/ghttp/ghttp_server_config_api.go index abbf79b01eb..d9a66d3cb09 100644 --- a/net/ghttp/ghttp_server_config_api.go +++ b/net/ghttp/ghttp_server_config_api.go @@ -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 } diff --git a/net/goai/goai_path.go b/net/goai/goai_path.go index d3667638cab..fe1013ebd1e 100644 --- a/net/goai/goai_path.go +++ b/net/goai/goai_path.go @@ -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, ",") } diff --git a/net/goai/goai_z_unit_issue_test.go b/net/goai/goai_z_unit_issue_test.go new file mode 100644 index 00000000000..9346bf5ed42 --- /dev/null +++ b/net/goai/goai_z_unit_issue_test.go @@ -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 TestIssue3664(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") + }) +} diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 9377fb8c5d7..cf3d03e2984 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -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() diff --git a/os/glog/glog_logger_api.go b/os/glog/glog_logger_api.go index d485d6fc2e1..748fae74c7a 100644 --- a/os/glog/glog_logger_api.go +++ b/os/glog/glog_logger_api.go @@ -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 } diff --git a/util/gtag/gtag.go b/util/gtag/gtag.go index edcd96247e2..2dc46455277 100644 --- a/util/gtag/gtag.go +++ b/util/gtag/gtag.go @@ -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. From 2c9eacff6757581c63a7b087541ddda0c385a32a Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 24 Sep 2024 10:05:54 +0800 Subject: [PATCH 2/2] up --- container/glist/glist_z_unit_test.go | 4 ++-- internal/deepcopy/deepcopy_test.go | 2 +- net/ghttp/ghttp_z_unit_issue_test.go | 2 +- net/goai/goai_z_unit_issue_test.go | 2 +- util/gconv/gconv_z_unit_issue_test.go | 24 ++++++++++++------------ 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/container/glist/glist_z_unit_test.go b/container/glist/glist_z_unit_test.go index 36984790f2a..d530f097341 100644 --- a/container/glist/glist_z_unit_test.go +++ b/container/glist/glist_z_unit_test.go @@ -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) @@ -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) diff --git a/internal/deepcopy/deepcopy_test.go b/internal/deepcopy/deepcopy_test.go index 060001ed70b..fa3e4a1871d 100644 --- a/internal/deepcopy/deepcopy_test.go +++ b/internal/deepcopy/deepcopy_test.go @@ -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{ diff --git a/net/ghttp/ghttp_z_unit_issue_test.go b/net/ghttp/ghttp_z_unit_issue_test.go index 35bf20ccd8b..d0d17b8471c 100644 --- a/net/ghttp/ghttp_z_unit_issue_test.go +++ b/net/ghttp/ghttp_z_unit_issue_test.go @@ -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) diff --git a/net/goai/goai_z_unit_issue_test.go b/net/goai/goai_z_unit_issue_test.go index 9346bf5ed42..44624c2de3a 100644 --- a/net/goai/goai_z_unit_issue_test.go +++ b/net/goai/goai_z_unit_issue_test.go @@ -45,7 +45,7 @@ func (Issue3664) RequiredTag(ctx context.Context, req *Issue3664RequiredTagReq) } // https://github.com/gogf/gf/issues/3664 -func TestIssue3664(t *testing.T) { +func Test_Issue3664(t *testing.T) { gtest.C(t, func(t *gtest.T) { s := g.Server() s.Use(ghttp.MiddlewareHandlerResponse) diff --git a/util/gconv/gconv_z_unit_issue_test.go b/util/gconv/gconv_z_unit_issue_test.go index 0aade8596df..3719ede4d1a 100644 --- a/util/gconv/gconv_z_unit_issue_test.go +++ b/util/gconv/gconv_z_unit_issue_test.go @@ -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"` @@ -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 @@ -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 @@ -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"` @@ -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 @@ -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 @@ -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 { @@ -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 } @@ -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"` @@ -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"` } @@ -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"` @@ -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"`