Skip to content

Commit

Permalink
fix: remove dropped tags from general infos (#1764)
Browse files Browse the repository at this point in the history
* fix: remove unneeded tags from general infos
Signed-off-by: sdghchj <[email protected]>
  • Loading branch information
sdghchj authored Feb 20, 2024
1 parent 56fde5c commit 928264c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 33 deletions.
84 changes: 51 additions & 33 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {

func parseGeneralAPIInfo(parser *Parser, comments []string) error {
previousAttribute := ""

var tag *spec.Tag
// parsing classic meta data model
for line := 0; line < len(comments); line++ {
commentLine := comments[line]
Expand Down Expand Up @@ -572,42 +572,43 @@ func parseGeneralAPIInfo(parser *Parser, comments []string) error {
case "@schemes":
parser.swagger.Schemes = strings.Split(value, " ")
case "@tag.name":
parser.swagger.Tags = append(parser.swagger.Tags, spec.Tag{
TagProps: spec.TagProps{
Name: value,
},
})
if parser.matchTag(value) {
parser.swagger.Tags = append(parser.swagger.Tags, spec.Tag{
TagProps: spec.TagProps{
Name: value,
},
})
tag = &parser.swagger.Tags[len(parser.swagger.Tags)-1]
} else {
tag = nil
}
case "@tag.description":
tag := parser.swagger.Tags[len(parser.swagger.Tags)-1]
tag.TagProps.Description = value
replaceLastTag(parser.swagger.Tags, tag)
if tag != nil {
tag.TagProps.Description = value
}
case "@tag.description.markdown":
tag := parser.swagger.Tags[len(parser.swagger.Tags)-1]
if tag != nil {
commentInfo, err := getMarkdownForTag(tag.TagProps.Name, parser.markdownFileDir)
if err != nil {
return err
}

commentInfo, err := getMarkdownForTag(tag.TagProps.Name, parser.markdownFileDir)
if err != nil {
return err
tag.TagProps.Description = string(commentInfo)
}

tag.TagProps.Description = string(commentInfo)
replaceLastTag(parser.swagger.Tags, tag)
case "@tag.docs.url":
tag := parser.swagger.Tags[len(parser.swagger.Tags)-1]
tag.TagProps.ExternalDocs = &spec.ExternalDocumentation{
URL: value,
Description: "",
if tag != nil {
tag.TagProps.ExternalDocs = &spec.ExternalDocumentation{
URL: value,
}
}

replaceLastTag(parser.swagger.Tags, tag)
case "@tag.docs.description":
tag := parser.swagger.Tags[len(parser.swagger.Tags)-1]
if tag.TagProps.ExternalDocs == nil {
return fmt.Errorf("%s needs to come after a @tags.docs.url", attribute)
}

tag.TagProps.ExternalDocs.Description = value
replaceLastTag(parser.swagger.Tags, tag)
if tag != nil {
if tag.TagProps.ExternalDocs == nil {
return fmt.Errorf("%s needs to come after a @tags.docs.url", attribute)
}

tag.TagProps.ExternalDocs.Description = value
}
case secBasicAttr, secAPIKeyAttr, secApplicationAttr, secImplicitAttr, secPasswordAttr, secAccessCodeAttr:
scheme, err := parseSecAttributes(attribute, comments, &line)
if err != nil {
Expand Down Expand Up @@ -943,6 +944,27 @@ func getTagsFromComment(comment string) (tags []string) {

}

func (parser *Parser) matchTag(tag string) bool {
if len(parser.tags) == 0 {
return true
}

if _, has := parser.tags["!"+tag]; has {
return false
}
if _, has := parser.tags[tag]; has {
return true
}

// If all tags are negation then we should return true
for key := range parser.tags {
if key[0] != '!' {
return false
}
}
return true
}

func (parser *Parser) matchTags(comments []*ast.Comment) (match bool) {
if len(parser.tags) == 0 {
return true
Expand Down Expand Up @@ -1609,10 +1631,6 @@ func (parser *Parser) GetSchemaTypePath(schema *spec.Schema, depth int) []string
return []string{ANY}
}

func replaceLastTag(slice []spec.Tag, element spec.Tag) {
slice = append(slice[:len(slice)-1], element)
}

// defineTypeOfExample example value define the type (object and array unsupported).
func defineTypeOfExample(schemaType, arrayType, exampleValue string) (interface{}, error) {
switch schemaType {
Expand Down
35 changes: 35 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,41 @@ func TestParser_ParseGeneralAPITagDocs(t *testing.T) {
assert.Equal(t, expected, string(b))
}

func TestParser_ParseGeneralAPITagDocsWithTagFilters(t *testing.T) {
t.Parallel()

filterTags := []string{"test1", "!test2"}

comments := []string{
"@tag.name test1",
"@tag.description A test1 Tag",
"@tag.docs.url https://example1.com",
"@tag.docs.description Best example1 documentation",
"@tag.name test2",
"@tag.description A test2 Tag",
"@tag.docs.url https://example2.com",
"@tag.docs.description Best example2 documentation"}

expected := `[
{
"description": "A test1 Tag",
"name": "test1",
"externalDocs": {
"description": "Best example1 documentation",
"url": "https://example1.com"
}
}
]`

for _, tag := range filterTags {
parser := New(SetTags(tag))
err := parseGeneralAPIInfo(parser, comments)
assert.NoError(t, err)
b, _ := json.MarshalIndent(parser.GetSwagger().Tags, "", " ")
assert.Equal(t, expected, string(b))
}
}

func TestParser_ParseGeneralAPISecurity(t *testing.T) {
t.Run("ApiKey", func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 928264c

Please sign in to comment.