Skip to content

Commit

Permalink
Added support for @tag.x- attributes for tags (#1784) (#1785)
Browse files Browse the repository at this point in the history
* Added support for @tag.x- attributes for tags (#1784)

* Example for @tag.x- attributes for tags (#1784)
  • Loading branch information
golandprog authored Oct 20, 2024
1 parent 0b9e347 commit d4ade12
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ When a short string in your documentation is insufficient, or you need images, c
| description.markdown | A short description of the application. Parsed from the api.md file. This is an alternative to @description |// @description.markdown No value needed, this parses the description from api.md |
| tag.name | Name of a tag.| // @tag.name This is the name of the tag |
| tag.description.markdown | Description of the tag this is an alternative to tag.description. The description will be read from a file named like tagname.md | // @tag.description.markdown |
| tag.x-name | The extension key, must be start by x- and take only string value | // @x-example-key value |
## API Operation
Expand Down
15 changes: 15 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,21 @@ func parseGeneralAPIInfo(parser *Parser, comments []string) error {

parser.swagger.Extensions[attribute[1:]] = valueJSON
}
} else if strings.HasPrefix(attribute, "@tag.x-") {
extensionName := attribute[5:]

if len(value) == 0 {
return fmt.Errorf("annotation %s need a value", attribute)
}

if tag.Extensions == nil {
tag.Extensions = make(map[string]interface{})
}

// tag.Extensions.Add(extensionName, value) works wrong (transforms extensionName to lower case)
// needed to save case for ReDoc
// https://redocly.com/docs/api-reference-docs/specification-extensions/x-display-name/
tag.Extensions[extensionName] = value
}
}

Expand Down
6 changes: 4 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ func TestParser_ParseGeneralAPITagDocs(t *testing.T) {
"@tag.name test",
"@tag.description A test Tag",
"@tag.docs.url https://example.com",
"@tag.docs.description Best example documentation"})
"@tag.docs.description Best example documentation",
"@tag.x-displayName Test group"})
assert.NoError(t, err)

b, _ := json.MarshalIndent(parser.GetSwagger().Tags, "", " ")
Expand All @@ -587,7 +588,8 @@ func TestParser_ParseGeneralAPITagDocs(t *testing.T) {
"externalDocs": {
"description": "Best example documentation",
"url": "https://example.com"
}
},
"x-displayName": "Test group"
}
]`
assert.Equal(t, expected, string(b))
Expand Down

0 comments on commit d4ade12

Please sign in to comment.