From 33c9204d58e603adb3537a11c78f07fe509635ee Mon Sep 17 00:00:00 2001 From: mingzaily Date: Tue, 26 Apr 2022 23:22:43 +0800 Subject: [PATCH 1/2] fix: swagger ignore "-" param. --- protocol/goai/goai_parameter_ref.go | 3 ++ protocol/goai/goai_shema.go | 3 ++ protocol/goai/goai_z_unit_test.go | 44 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/protocol/goai/goai_parameter_ref.go b/protocol/goai/goai_parameter_ref.go index bbbc3b932..0b918561b 100644 --- a/protocol/goai/goai_parameter_ref.go +++ b/protocol/goai/goai_parameter_ref.go @@ -37,6 +37,9 @@ func (oai *OpenApiV3) newParameterRefWithStructMethod(field gstructs.Field, path parameter.Name = field.Name() } if len(tagMap) > 0 { + if v, ok := tagMap["json"]; ok && v == "-" { + return nil, nil + } if err := oai.tagMapToParameter(tagMap, parameter); err != nil { return nil, err } diff --git a/protocol/goai/goai_shema.go b/protocol/goai/goai_shema.go index c4516746e..907bc6c1d 100644 --- a/protocol/goai/goai_shema.go +++ b/protocol/goai/goai_shema.go @@ -169,6 +169,9 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) { } var fieldName = structField.Name() if jsonName := structField.TagJsonName(); jsonName != "" { + if jsonName == "-" { + continue + } fieldName = jsonName } schemaRef, err := oai.newSchemaRefWithGolangType( diff --git a/protocol/goai/goai_z_unit_test.go b/protocol/goai/goai_z_unit_test.go index bb209d6b4..e81b04715 100644 --- a/protocol/goai/goai_z_unit_test.go +++ b/protocol/goai/goai_z_unit_test.go @@ -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) + }) +} From ce89b440bb4e2b9fd0ed039182b69b77a8ee1fd1 Mon Sep 17 00:00:00 2001 From: mingzaily Date: Thu, 28 Apr 2022 17:14:19 +0800 Subject: [PATCH 2/2] feat: optimize ignore tag. --- protocol/goai/goai.go | 7 +++++++ protocol/goai/goai_parameter_ref.go | 8 +++++--- protocol/goai/goai_shema.go | 12 +++++++++--- protocol/goai/goai_shemas.go | 5 +++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/protocol/goai/goai.go b/protocol/goai/goai.go index 5359be4ae..e39c11fb4 100644 --- a/protocol/goai/goai.go +++ b/protocol/goai/goai.go @@ -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 +} diff --git a/protocol/goai/goai_parameter_ref.go b/protocol/goai/goai_parameter_ref.go index 0b918561b..b32f1e33e 100644 --- a/protocol/goai/goai_parameter_ref.go +++ b/protocol/goai/goai_parameter_ref.go @@ -37,9 +37,6 @@ func (oai *OpenApiV3) newParameterRefWithStructMethod(field gstructs.Field, path parameter.Name = field.Name() } if len(tagMap) > 0 { - if v, ok := tagMap["json"]; ok && v == "-" { - return nil, nil - } if err := oai.tagMapToParameter(tagMap, parameter); err != nil { return nil, err } @@ -77,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) { diff --git a/protocol/goai/goai_shema.go b/protocol/goai/goai_shema.go index 907bc6c1d..2c05972ad 100644 --- a/protocol/goai/goai_shema.go +++ b/protocol/goai/goai_shema.go @@ -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 { @@ -169,9 +170,6 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) { } var fieldName = structField.Name() if jsonName := structField.TagJsonName(); jsonName != "" { - if jsonName == "-" { - continue - } fieldName = jsonName } schemaRef, err := oai.newSchemaRefWithGolangType( @@ -191,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 } diff --git a/protocol/goai/goai_shemas.go b/protocol/goai/goai_shemas.go index 47fa348fa..c8f6c6458 100644 --- a/protocol/goai/goai_shemas.go +++ b/protocol/goai/goai_shemas.go @@ -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)