Skip to content
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

fix: don't generate license field if it's empty #768

Merged
merged 1 commit into from Aug 7, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion parser.go
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
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
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
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
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