From 77ffe68db8f54bf47ed561768fc25a20497dfd64 Mon Sep 17 00:00:00 2001 From: Yuki Omoto Date: Sat, 14 Sep 2024 02:31:54 +0900 Subject: [PATCH] Fix param comment escaping issue This commit fixes a param comment issue where a "\n" gets escaped so it would not be applied to the output swagger file. --- operation.go | 2 +- operation_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/operation.go b/operation.go index ac79e3102..2ce9c9e4d 100644 --- a/operation.go +++ b/operation.go @@ -281,7 +281,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F requiredText := strings.ToLower(matches[4]) required := requiredText == "true" || requiredText == requiredLabel - description := matches[5] + description := strings.Join(strings.Split(matches[5], "\\n"), "\n") param := createParameter(paramType, description, name, objectType, refType, required, enums, operation.parser.collectionFormatInQuery) diff --git a/operation_test.go b/operation_test.go index 50e4ac3b1..9ce46982f 100644 --- a/operation_test.go +++ b/operation_test.go @@ -1322,6 +1322,27 @@ func TestParseParamCommentByID(t *testing.T) { assert.Equal(t, expected, string(b)) } +func TestParseParamCommentWithMultilineDescriptions(t *testing.T) { + t.Parallel() + + comment := `@Param some_id query int true "First line\nSecond line\nThird line"` + operation := NewOperation(nil) + err := operation.ParseComment(comment, nil) + + assert.NoError(t, err) + b, _ := json.MarshalIndent(operation.Parameters, "", " ") + expected := `[ + { + "type": "integer", + "description": "First line\nSecond line\nThird line", + "name": "some_id", + "in": "query", + "required": true + } +]` + assert.Equal(t, expected, string(b)) +} + func TestParseParamCommentByQueryType(t *testing.T) { t.Parallel()