mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
Merge pull request #1789 from mingzaily/master
fix: swagger ignore "-" param.
This commit is contained in:
@ -240,3 +240,10 @@ func (oai *OpenApiV3) fileMapWithShortTags(m map[string]string) map[string]strin
|
||||
func formatRefToBytes(ref string) []byte {
|
||||
return []byte(fmt.Sprintf(`{"$ref":"#/components/schemas/%s"}`, ref))
|
||||
}
|
||||
|
||||
func isValidTag(key string) bool {
|
||||
if key == "-" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@ -74,6 +74,11 @@ func (oai *OpenApiV3) newParameterRefWithStructMethod(field gstructs.Field, path
|
||||
}
|
||||
parameter.Schema = schemaRef
|
||||
|
||||
// Ignore parameter.
|
||||
if !isValidTag(parameter.Name) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Required check.
|
||||
if parameter.Schema.Value != nil && parameter.Schema.Value.Pattern != "" {
|
||||
if gset.NewStrSetFrom(gstr.Split(parameter.Schema.Value.Pattern, "|")).Contains(patternKeyForRequired) {
|
||||
|
||||
@ -138,6 +138,7 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) {
|
||||
Properties: createSchemas(),
|
||||
XExtensions: make(XExtensions),
|
||||
}
|
||||
ignoreProperties []interface{}
|
||||
)
|
||||
if len(tagMap) > 0 {
|
||||
if err := oai.tagMapToSchema(tagMap, schema); err != nil {
|
||||
@ -188,8 +189,16 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) {
|
||||
schema.Required = append(schema.Required, key)
|
||||
}
|
||||
}
|
||||
if !isValidTag(key) {
|
||||
ignoreProperties = append(ignoreProperties, key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if len(ignoreProperties) > 0 {
|
||||
schema.Properties.Removes(ignoreProperties)
|
||||
}
|
||||
|
||||
return schema, nil
|
||||
}
|
||||
|
||||
|
||||
@ -41,6 +41,11 @@ func (s *Schemas) Set(name string, ref SchemaRef) {
|
||||
s.refs.Set(name, ref)
|
||||
}
|
||||
|
||||
func (s *Schemas) Removes(names []interface{}) {
|
||||
s.init()
|
||||
s.refs.Removes(names)
|
||||
}
|
||||
|
||||
func (s *Schemas) Map() map[string]SchemaRef {
|
||||
s.init()
|
||||
m := make(map[string]SchemaRef)
|
||||
|
||||
@ -831,3 +831,47 @@ func Test_Properties_In_Sequence(t *testing.T) {
|
||||
fmt.Println(oai)
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpenApiV3_Ignore_Parameter(t *testing.T) {
|
||||
type CommonResponse struct {
|
||||
Code int `json:"code" description:"Error code"`
|
||||
Message string `json:"message" description:"Error message"`
|
||||
Data interface{} `json:"data" description:"Result data for certain request according API definition"`
|
||||
}
|
||||
type ProductSearchReq struct {
|
||||
gmeta.Meta `path:"/test" method:"get"`
|
||||
Product string `json:"-" in:"query" v:"required|max:5" description:"Unique product key"`
|
||||
Name string `json:"name" in:"query" v:"required" description:"Instance name"`
|
||||
}
|
||||
type ProductSearchRes struct {
|
||||
ID int64 `json:"-" description:"Product ID"`
|
||||
Product string `json:"product" v:"required" description:"Unique product key"`
|
||||
TemplateName string `json:"templateName" v:"required" description:"Workflow template name"`
|
||||
Version string `json:"version" v:"required" description:"Workflow template version"`
|
||||
TxID string `json:"txID" v:"required" description:"Transaction ID for creating instance"`
|
||||
Globals string `json:"globals" description:"Globals"`
|
||||
}
|
||||
|
||||
f := func(ctx context.Context, req *ProductSearchReq) (res *ProductSearchRes, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
err error
|
||||
oai = goai.New()
|
||||
)
|
||||
|
||||
oai.Config.CommonResponse = CommonResponse{}
|
||||
|
||||
err = oai.Add(goai.AddInput{
|
||||
Object: f,
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Schema asserts.
|
||||
// fmt.Println(oai.String())
|
||||
t.Assert(len(oai.Components.Schemas.Map()), 3)
|
||||
t.Assert(len(oai.Paths), 1)
|
||||
t.Assert(len(oai.Paths["/test"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 8)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user