From 928264cc3a03e654b4e47d8cc7d96276c3eed6d3 Mon Sep 17 00:00:00 2001 From: sdghchj Date: Tue, 20 Feb 2024 15:14:12 +0800 Subject: [PATCH] fix: remove dropped tags from general infos (#1764) * fix: remove unneeded tags from general infos Signed-off-by: sdghchj --- parser.go | 84 ++++++++++++++++++++++++++++++-------------------- parser_test.go | 35 +++++++++++++++++++++ 2 files changed, 86 insertions(+), 33 deletions(-) diff --git a/parser.go b/parser.go index 604f8278e..561c4549e 100644 --- a/parser.go +++ b/parser.go @@ -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] @@ -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 { @@ -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 @@ -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 { diff --git a/parser_test.go b/parser_test.go index 770e64bc0..7862796d1 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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()