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 bbbc3b932..b32f1e33e 100644 --- a/protocol/goai/goai_parameter_ref.go +++ b/protocol/goai/goai_parameter_ref.go @@ -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) { diff --git a/protocol/goai/goai_shema.go b/protocol/goai/goai_shema.go index c4516746e..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 { @@ -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 } 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) 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) + }) +}