From d8893d033b4c5eb94a485abd89546f4e88a56b58 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Fri, 7 Aug 2020 10:39:25 +0800 Subject: [PATCH] fix: don't generate license field if it's empty (#768) --- parser.go | 12 +++++++++++- parser_test.go | 7 +------ testdata/composition/expected.json | 1 - testdata/conflict_name/expected.json | 1 - testdata/nested/expected.json | 1 - 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/parser.go b/parser.go index 7cf86692d..4c786e0cc 100644 --- a/parser.go +++ b/parser.go @@ -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{ @@ -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() @@ -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 diff --git a/parser_test.go b/parser_test.go index 069417620..272467f43 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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": {} @@ -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", @@ -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", @@ -2234,8 +2231,7 @@ func Fun() { ` expected := `{ "info": { - "contact": {}, - "license": {} + "contact": {} }, "paths": { "/test": { @@ -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", diff --git a/testdata/composition/expected.json b/testdata/composition/expected.json index d14bfbd91..f260e066c 100644 --- a/testdata/composition/expected.json +++ b/testdata/composition/expected.json @@ -5,7 +5,6 @@ "title": "Swagger Example API", "termsOfService": "http://swagger.io/terms/", "contact": {}, - "license": {}, "version": "1.0" }, "host": "petstore.swagger.io", diff --git a/testdata/conflict_name/expected.json b/testdata/conflict_name/expected.json index 431491219..74f046c46 100644 --- a/testdata/conflict_name/expected.json +++ b/testdata/conflict_name/expected.json @@ -4,7 +4,6 @@ "description": "test for conflict name", "title": "Swag test", "contact": {}, - "license": {}, "version": "1.0" }, "paths": { diff --git a/testdata/nested/expected.json b/testdata/nested/expected.json index 9de5ed335..6ed9d51b1 100644 --- a/testdata/nested/expected.json +++ b/testdata/nested/expected.json @@ -5,7 +5,6 @@ "title": "Swagger Example API", "termsOfService": "http://swagger.io/terms/", "contact": {}, - "license": {}, "version": "1.0" }, "host": "petstore.swagger.io",