Skip to content

Commit

Permalink
fix: don't generate license field if it's empty (swaggo#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia authored Aug 7, 2020
1 parent b712c54 commit d8893d0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
12 changes: 11 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func New(options ...func(*Parser)) *Parser {
Info: &spec.Info{
InfoProps: spec.InfoProps{
Contact: &spec.ContactInfo{},
License: &spec.License{},
License: nil,
},
},
Paths: &spec.Paths{
Expand Down Expand Up @@ -221,6 +221,14 @@ func getPkgName(searchDir string) (string, error) {
return outStr, nil
}

func initIfEmpty(license *spec.License) *spec.License {
if license == nil {
return new(spec.License)
}

return license
}

// ParseGeneralAPIInfo parses general api info for given mainAPIFile path
func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {
fileSet := token.NewFileSet()
Expand Down Expand Up @@ -272,8 +280,10 @@ func (parser *Parser) ParseGeneralAPIInfo(mainAPIFile string) error {
case "@contact.url":
parser.swagger.Info.Contact.URL = value
case "@license.name":
parser.swagger.Info.License = initIfEmpty(parser.swagger.Info.License)
parser.swagger.Info.License.Name = value
case "@license.url":
parser.swagger.Info.License = initIfEmpty(parser.swagger.Info.License)
parser.swagger.Info.License.URL = value
case "@host":
parser.swagger.Host = value
Expand Down
7 changes: 1 addition & 6 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ func TestParser_ParseGeneralApiInfoWithOpsInSameFile(t *testing.T) {
"title": "Swagger Example API",
"termsOfService": "http://swagger.io/terms/",
"contact": {},
"license": {},
"version": "1.0"
},
"paths": {}
Expand Down Expand Up @@ -1236,7 +1235,6 @@ func TestParseStructComment(t *testing.T) {
"description": "This is a sample server Petstore server.",
"title": "Swagger Example API",
"contact": {},
"license": {},
"version": "1.0"
},
"host": "localhost:4000",
Expand Down Expand Up @@ -1325,7 +1323,6 @@ func TestParseNonExportedJSONFields(t *testing.T) {
"description": "This is a sample server.",
"title": "Swagger Example API",
"contact": {},
"license": {},
"version": "1.0"
},
"host": "localhost:4000",
Expand Down Expand Up @@ -2234,8 +2231,7 @@ func Fun() {
`
expected := `{
"info": {
"contact": {},
"license": {}
"contact": {}
},
"paths": {
"/test": {
Expand Down Expand Up @@ -2330,7 +2326,6 @@ func TestParseJSONFieldString(t *testing.T) {
"description": "This is a sample server.",
"title": "Swagger Example API",
"contact": {},
"license": {},
"version": "1.0"
},
"host": "localhost:4000",
Expand Down
1 change: 0 additions & 1 deletion testdata/composition/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"title": "Swagger Example API",
"termsOfService": "http://swagger.io/terms/",
"contact": {},
"license": {},
"version": "1.0"
},
"host": "petstore.swagger.io",
Expand Down
1 change: 0 additions & 1 deletion testdata/conflict_name/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "test for conflict name",
"title": "Swag test",
"contact": {},
"license": {},
"version": "1.0"
},
"paths": {
Expand Down
1 change: 0 additions & 1 deletion testdata/nested/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"title": "Swagger Example API",
"termsOfService": "http://swagger.io/terms/",
"contact": {},
"license": {},
"version": "1.0"
},
"host": "petstore.swagger.io",
Expand Down

0 comments on commit d8893d0

Please sign in to comment.