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

feat: ignore uri tag with gin #1315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
optionalLabel = "optional"
swaggerTypeTag = "swaggertype"
swaggerIgnoreTag = "swaggerignore"
ignoreTags = "uri"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ignoredTags should be declared as a swagParam not as constant

)

type tagBaseFieldParser struct {
Expand Down Expand Up @@ -51,8 +52,7 @@ func (ps *tagBaseFieldParser) ShouldSkip() bool {
return false
}

ignoreTag := ps.tag.Get(swaggerIgnoreTag)
if strings.EqualFold(ignoreTag, "true") {
if isIgnore(ps.tag) {
return true
}

Expand All @@ -65,6 +65,24 @@ func (ps *tagBaseFieldParser) ShouldSkip() bool {
return false
}

var ignoreTagArray = strings.Split(ignoreTags, ",")

func isIgnore(tag reflect.StructTag) bool {
ignoreTag, ok := tag.Lookup(swaggerIgnoreTag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add unit tests to cover this function.

if ok && strings.EqualFold(ignoreTag, "true") {
return true
}

for _, v := range ignoreTagArray {
_, ok = tag.Lookup(v)
if ok {
return ok
}
}

return false
}

func (ps *tagBaseFieldParser) FieldName() (string, error) {
var name string
if ps.field.Tag != nil {
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1276,8 +1276,8 @@ func (parser *Parser) parseStruct(file *ast.File, fields *ast.FieldList) (*spec.

func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[string]spec.Schema, []string, error) {
if field.Tag != nil {
skip, ok := reflect.StructTag(strings.ReplaceAll(field.Tag.Value, "`", "")).Lookup("swaggerignore")
if ok && strings.EqualFold(skip, "true") {
tag := reflect.StructTag(strings.ReplaceAll(field.Tag.Value, "`", ""))
if isIgnore(tag) {
return nil, nil, nil
}
}
Expand Down