Skip to content

feat: allow description line continuation #2048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (operation *Operation) ParseDescriptionComment(lineRemainder string) {
return
}

operation.Description += "\n" + lineRemainder
operation.Description = AppendDescription(operation.Description, lineRemainder)
}

// ParseMetadata parse metadata.
Expand Down
3 changes: 1 addition & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,7 @@ func parseGeneralAPIInfo(parser *Parser, comments []string) error {
setSwaggerInfo(parser.swagger, attr, value)
case descriptionAttr:
if previousAttribute == attribute {
parser.swagger.Info.Description += "\n" + value

parser.swagger.Info.Description = AppendDescription(parser.swagger.Info.Description, value)
continue
}

Expand Down
16 changes: 16 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4491,3 +4491,19 @@ var Func2 = Func
assert.NotNil(t, val2.Get)
assert.Equal(t, val2.Get.OperationProps.Summary, "generate indirectly pointing")
}

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

p := New()
searchDir := "testdata/description_line_continuation"
expected, err := os.ReadFile(filepath.Join(searchDir, "expected.json"))
assert.NoError(t, err)

err = p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

b, err := json.MarshalIndent(p.swagger, "", " ")
assert.NoError(t, err)
assert.Equal(t, string(expected), string(b))
}
33 changes: 33 additions & 0 deletions testdata/description_line_continuation/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package api

import (
"net/http"
)

// @Summary Endpoint A
// @Description This is a mock endpoint description \
// @Description which is long and descriptions that \
// @Description end with backslash do not add a new line.
// @Description This sentence is in a new line.
// @Description
// @Description And this have an empty line above it.
// @Description Lorem ipsum dolor sit amet \
// @Description consectetur adipiscing elit, \
// @Description sed do eiusmod tempor incididunt \
// @Description ut labore et dolore magna aliqua.
// @Success 200
// @Router /a [get]
func EndpointA(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

// @Summary Endpoint B
// @Description Something something.
// @Description
// @Description A new line, \
// @Description continue to the line.
// @Success 200
// @Router /b [get]
func EndpointB(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
34 changes: 34 additions & 0 deletions testdata/description_line_continuation/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"swagger": "2.0",
"info": {
"description": "Example long description that should not be split into multiple lines.\nThis is a new line thatescapes new line withoutadding a whitespace.\n\nAnother line that has an empty line above it.",
"title": "Swagger Example API",
"contact": {},
"version": "1.0"
},
"host": "localhost:8080",
"paths": {
"/a": {
"get": {
"description": "This is a mock endpoint description which is long and descriptions that end with backslash do not add a new line.\nThis sentence is in a new line.\n\nAnd this have an empty line above it.\nLorem ipsum dolor sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"summary": "Endpoint A",
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/b": {
"get": {
"description": "Something something.\n\nA new line, continue to the line.",
"summary": "Endpoint B",
"responses": {
"200": {
"description": "OK"
}
}
}
}
}
}
25 changes: 25 additions & 0 deletions testdata/description_line_continuation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"net/http"

"github.com/swaggo/swag/testdata/description_escape_new_line/api"
)

// @title Swagger Example API
// @version 1.0
// @description Example long description \
// @description that should not be split \
// @description into multiple lines.
// @description This is a new line that\
// @description escapes new line without\
// @description adding a whitespace.
// @description
// @description Another line that has an \
// @description empty line above it.
// @host localhost:8080
func main() {
http.HandleFunc("/a", api.EndpointA)
http.HandleFunc("/b", api.EndpointB)
http.ListenAndServe(":8080", nil)
}
14 changes: 13 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package swag

import "unicode"
import (
"strings"
"unicode"
)

// FieldsFunc split a string s by a func splitter into max n parts
func FieldsFunc(s string, f func(rune2 rune) bool, n int) []string {
Expand Down Expand Up @@ -53,3 +56,12 @@ func FieldsFunc(s string, f func(rune2 rune) bool, n int) []string {
func FieldsByAnySpace(s string, n int) []string {
return FieldsFunc(s, unicode.IsSpace, n)
}

// AppendDescription appends a new string to the existing description, treating
// a trailing backslash as a line continuation.
func AppendDescription(current, addition string) string {
if strings.HasSuffix(current, "\\") {
return current[:len(current)-1] + addition
}
return current + "\n" + addition
}
42 changes: 41 additions & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package swag

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFieldsByAnySpace(t *testing.T) {
Expand Down Expand Up @@ -36,3 +37,42 @@ func TestFieldsByAnySpace(t *testing.T) {
})
}
}

func TestAppendDescription(t *testing.T) {
type args struct {
current string
addition string
}
tests := []struct {
name string
args args
want string
}{
{"test1",
args{
"aa",
"bb",
},
"aa\nbb",
},
{"test2",
args{
"aa\\",
"bb",
},
"aabb",
},
{"test3",
args{
"aa \\",
"bb",
},
"aa bb",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, AppendDescription(tt.args.current, tt.args.addition), "AppendDescription(%v, %v)", tt.args.current, tt.args.addition)
})
}
}
Loading