Skip to content

Commit

Permalink
Multiline description for api info (#278)
Browse files Browse the repository at this point in the history
* Basic implementation of multiline description

* Add docs

* Update tests to include multiline descriptions
  • Loading branch information
kolaente authored and pei0804 committed Jan 4, 2019
1 parent 1048d76 commit 7e407c1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,16 @@ Field Name | Type | Description

## TIPS

### Descriptions over multiple lines

You can add descriptions spanning multiple lines in either the general api description or routes definitions like so:

```go
// @description This is the first line
// @description This is the second line
// @description And so forth.
```

### User defined structure with an array type

```go
Expand Down
2 changes: 1 addition & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (operation *Operation) ParseComment(comment string, astFile *ast.File) erro
if operation.Description == "" {
operation.Description = lineRemainder
} else {
operation.Description += "<br>" + lineRemainder
operation.Description += "\n" + lineRemainder
}
case "@summary":
operation.Summary = lineRemainder
Expand Down
2 changes: 1 addition & 1 deletion operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,6 @@ func TestParseMultiDescription(t *testing.T) {

b, _ := json.MarshalIndent(operation, "", " ")

expected := `"description": "line one\u003cbr\u003eline two x"`
expected := `"description": "line one\nline two x"`
assert.Contains(t, string(b), expected)
}
6 changes: 5 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {
case "@title":
parser.swagger.Info.Title = strings.TrimSpace(commentLine[len(attribute):])
case "@description":
parser.swagger.Info.Description = strings.TrimSpace(commentLine[len(attribute):])
if parser.swagger.Info.Description == "{{.Description}}" {
parser.swagger.Info.Description = strings.TrimSpace(commentLine[len(attribute):])
} else {
parser.swagger.Info.Description += "\n" + strings.TrimSpace(commentLine[len(attribute):])
}
case "@termsofservice":
parser.swagger.Info.TermsOfService = strings.TrimSpace(commentLine[len(attribute):])
case "@contact.name":
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestParser_ParseGeneralApiInfo(t *testing.T) {
expected := `{
"swagger": "2.0",
"info": {
"description": "This is a sample server Petstore server.",
"description": "This is a sample server Petstore server.\nIt has a lot of beatiful features.",
"title": "Swagger Example API",
"termsOfService": "http://swagger.io/terms/",
"contact": {
Expand Down
1 change: 1 addition & 0 deletions testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @description It has a lot of beatiful features.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
Expand Down

0 comments on commit 7e407c1

Please sign in to comment.