Skip to content

Commit

Permalink
fix: error resolving host and basepath (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan authored and easonlin404 committed Jul 26, 2019
1 parent 67075e4 commit c19280e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dist
testdata/simple*/docs
example/basic/docs/*
example/celler/docs/*
cover.out


Expand Down
57 changes: 28 additions & 29 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,35 +184,38 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {
parser.swagger.Swagger = "2.0"
securityMap := map[string]*spec.SecurityScheme{}

// templated defaults
parser.swagger.Info.Version = "{{.Version}}"
parser.swagger.Info.Title = "{{.Title}}"
parser.swagger.Info.Description = "{{.Description}}"
parser.swagger.Host = "{{.Host}}"
parser.swagger.BasePath = "{{.BasePath}}"

if fileTree.Comments != nil {
nextComments:
for _, comment := range fileTree.Comments {
comments := strings.Split(comment.Text(), "\n")
// Comments belongs to operation ?
for _, commentLine := range comments {
attribute := strings.ToLower(strings.Split(commentLine, " ")[0])
switch attribute {
case "@summary", "@router", "@success", "@failure":
continue nextComments
}
}
previousAttribute := ""
// parsing classic meta data model
for _, commentLine := range comments {
attribute := strings.ToLower(strings.Split(commentLine, " ")[0])
value := strings.TrimSpace(commentLine[len(attribute):])
multilineBlock := false
if previousAttribute == attribute {
multilineBlock = true
}
switch attribute {
case "@version":
parser.swagger.Info.Version = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Info.Version = value
case "@title":
parser.swagger.Info.Title = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Info.Title = value
case "@description":
if parser.swagger.Info.Description == "{{.Description}}" {
parser.swagger.Info.Description = strings.TrimSpace(commentLine[len(attribute):])
} else if multilineBlock {
parser.swagger.Info.Description += "\n" + strings.TrimSpace(commentLine[len(attribute):])
if multilineBlock {
parser.swagger.Info.Description += "\n" + value
continue
}
parser.swagger.Info.Description = value
case "@description.markdown":
filePath, err := getMarkdownFileForTag("api", parser.markdownFileDir)
if err != nil {
Expand All @@ -226,34 +229,32 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {

parser.swagger.Info.Description = string(commentInfo)
case "@termsofservice":
parser.swagger.Info.TermsOfService = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Info.TermsOfService = value
case "@contact.name":
parser.swagger.Info.Contact.Name = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Info.Contact.Name = value
case "@contact.email":
parser.swagger.Info.Contact.Email = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Info.Contact.Email = value
case "@contact.url":
parser.swagger.Info.Contact.URL = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Info.Contact.URL = value
case "@license.name":
parser.swagger.Info.License.Name = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Info.License.Name = value
case "@license.url":
parser.swagger.Info.License.URL = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Info.License.URL = value
case "@host":
parser.swagger.Host = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Host = value
case "@basepath":
parser.swagger.BasePath = strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.BasePath = value
case "@schemes":
parser.swagger.Schemes = getSchemes(commentLine)
case "@tag.name":
commentInfo := strings.TrimSpace(commentLine[len(attribute):])
parser.swagger.Tags = append(parser.swagger.Tags, spec.Tag{
TagProps: spec.TagProps{
Name: strings.TrimSpace(commentInfo),
Name: value,
},
})
case "@tag.description":
commentInfo := strings.TrimSpace(commentLine[len(attribute):])
tag := parser.swagger.Tags[len(parser.swagger.Tags)-1]
tag.TagProps.Description = commentInfo
tag.TagProps.Description = value
replaceLastTag(parser.swagger.Tags, tag)
case "@tag.description.markdown":
tag := parser.swagger.Tags[len(parser.swagger.Tags)-1]
Expand All @@ -270,19 +271,17 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {
tag.TagProps.Description = string(commentInfo)
replaceLastTag(parser.swagger.Tags, tag)
case "@tag.docs.url":
commentInfo := strings.TrimSpace(commentLine[len(attribute):])
tag := parser.swagger.Tags[len(parser.swagger.Tags)-1]
tag.TagProps.ExternalDocs = &spec.ExternalDocumentation{
URL: commentInfo,
URL: value,
}
replaceLastTag(parser.swagger.Tags, tag)
case "@tag.docs.description":
commentInfo := strings.TrimSpace(commentLine[len(attribute):])
tag := parser.swagger.Tags[len(parser.swagger.Tags)-1]
if tag.TagProps.ExternalDocs == nil {
return errors.New("@tag.docs.description needs to come after a @tags.docs.url")
}
tag.TagProps.ExternalDocs.Description = commentInfo
tag.TagProps.ExternalDocs.Description = value
replaceLastTag(parser.swagger.Tags, tag)
}
previousAttribute = attribute
Expand Down
9 changes: 1 addition & 8 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) {
expected := `{
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"title": "{{.Title}}",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
Expand All @@ -123,11 +121,8 @@ func TestParser_ParseGeneralApiInfoTemplated(t *testing.T) {
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "{{.Version}}"
}
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {},
"securityDefinitions": {
"ApiKeyAuth": {
Expand Down Expand Up @@ -231,8 +226,6 @@ func TestParser_ParseGeneralApiInfoWithOpsInSameFile(t *testing.T) {
"license": {},
"version": "1.0"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {}
}`

Expand Down

0 comments on commit c19280e

Please sign in to comment.