From 92e0626d9c92a8dea77756b58f4640770a0e6ad3 Mon Sep 17 00:00:00 2001 From: Vladimir Avchenov Date: Wed, 10 Apr 2024 17:05:24 +0300 Subject: [PATCH] Added support for @tag.x- attributes for tags (#1784) --- parser.go | 15 +++++++++++++++ parser_test.go | 6 ++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/parser.go b/parser.go index 561c4549e..5aa74e024 100644 --- a/parser.go +++ b/parser.go @@ -673,6 +673,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 } } diff --git a/parser_test.go b/parser_test.go index 7862796d1..7dc5e482f 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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, "", " ") @@ -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))