Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net/goai): change default value of RequestBody.Required from true to false, add required tag support for RequestBody #3796

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
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 TestIssue3664(t *testing.T) {
gqcn marked this conversation as resolved.
Show resolved Hide resolved
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
}
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
Loading