feat: optimize ignore tag.

This commit is contained in:
mingzaily 2022-04-28 17:14:19 +08:00
parent 33c9204d58
commit ce89b440bb
4 changed files with 26 additions and 6 deletions

View File

@ -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
}

View File

@ -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) {

View File

@ -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
}

View File

@ -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)