Repository: deepmap/oapi-codegen
Pull request #334 (2021-04-06 07:49:55)

Fix multi-line comment generate failed issue
Closes https://github.com/deepmap/oapi-codegen/issues/333

This issue was introduced by PR https://github.com/deepmap/oapi-codegen/pull/328.


Patch:
diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go
index ef89b4e..a476a9f 100644
--- a/pkg/codegen/schema.go
+++ b/pkg/codegen/schema.go
@@ -156,12 +156,12 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
 		}
 		return Schema{
 			GoType:      refType,
-			Description: schema.Description,
+			Description: StringToGoComment(schema.Description),
 		}, nil
 	}
 
 	outSchema := Schema{
-		Description: schema.Description,
+		Description: StringToGoComment(schema.Description),
 	}
 
 	// We can't support this in any meaningful way
@@ -576,7 +576,7 @@ func paramToGoType(param *openapi3.Parameter, path []string) (Schema, error) {
 	if len(param.Content) > 1 {
 		return Schema{
 			GoType:      "string",
-			Description: param.Description,
+			Description: StringToGoComment(param.Description),
 		}, nil
 	}
 
@@ -586,7 +586,7 @@ func paramToGoType(param *openapi3.Parameter, path []string) (Schema, error) {
 		// If we don't have json, it's a string
 		return Schema{
 			GoType:      "string",
-			Description: param.Description,
+			Description: StringToGoComment(param.Description),
 		}, nil
 	}
 
diff --git a/pkg/codegen/templates/templates.gen.go b/pkg/codegen/templates/templates.gen.go
index 8e652e6..1280d49 100644
--- a/pkg/codegen/templates/templates.gen.go
+++ b/pkg/codegen/templates/templates.gen.go
@@ -906,7 +906,7 @@ type ServerInterface interface {
 }
 `,
 	"typedef.tmpl": `{{range .Types}}
-// {{ with .Schema.Description }}{{ . }}{{ else }}{{.TypeName}} defines model for {{.JsonName}}.{{ end }}
+{{ with .Schema.Description }}{{ . }}{{ else }}// {{.TypeName}} defines model for {{.JsonName}}.{{ end }}
 type {{.TypeName}} {{if and (opts.AliasTypes) (.CanAlias)}}={{end}} {{.Schema.TypeDecl}}
 {{end}}
 `,
diff --git a/pkg/codegen/templates/typedef.tmpl b/pkg/codegen/templates/typedef.tmpl
index f4596e0..0b153b7 100644
--- a/pkg/codegen/templates/typedef.tmpl
+++ b/pkg/codegen/templates/typedef.tmpl
@@ -1,4 +1,4 @@
 {{range .Types}}
-// {{ with .Schema.Description }}{{ . }}{{ else }}{{.TypeName}} defines model for {{.JsonName}}.{{ end }}
+{{ with .Schema.Description }}{{ . }}{{ else }}// {{.TypeName}} defines model for {{.JsonName}}.{{ end }}
 type {{.TypeName}} {{if and (opts.AliasTypes) (.CanAlias)}}={{end}} {{.Schema.TypeDecl}}
 {{end}}
diff --git a/pkg/codegen/utils.go b/pkg/codegen/utils.go
index 0093208..6308875 100644
--- a/pkg/codegen/utils.go
+++ b/pkg/codegen/utils.go
@@ -553,6 +553,10 @@ func PathToTypeName(path []string) string {
 // StringToGoComment renders a possible multi-line string as a valid Go-Comment.
 // Each line is prefixed as a comment.
 func StringToGoComment(in string) string {
+	if len(in) == 0 || len(strings.TrimSpace(in)) == 0 { // ignore empty comment
+		return ""
+	}
+
 	// Normalize newlines from Windows/Mac to Linux
 	in = strings.Replace(in, "\r\n", "\n", -1)
 	in = strings.Replace(in, "\r", "\n", -1)


Test patch:
diff --git a/internal/test/schemas/schemas.gen.go b/internal/test/schemas/schemas.gen.go
index b3400ca..fbb6b9e 100644
--- a/internal/test/schemas/schemas.gen.go
+++ b/internal/test/schemas/schemas.gen.go
@@ -35,6 +35,8 @@ type N5StartsWithNumber map[string]interface{}
 // AnyType1 defines model for AnyType1.
 type AnyType1 interface{}
 
+// AnyType2 represents any type.
+//
 // This should be an interface{}
 type AnyType2 interface{}
 
@@ -606,6 +608,8 @@ type EnsureEverythingIsReferencedResponse struct {
 	JSON200      *struct {
 		AnyType1 *AnyType1 `json:"anyType1,omitempty"`
 
+		// AnyType2 represents any type.
+		//
 		// This should be an interface{}
 		AnyType2         *AnyType2         `json:"anyType2,omitempty"`
 		CustomStringType *CustomStringType `json:"customStringType,omitempty"`
@@ -855,6 +859,8 @@ func ParseEnsureEverythingIsReferencedResponse(rsp *http.Response) (*EnsureEvery
 		var dest struct {
 			AnyType1 *AnyType1 `json:"anyType1,omitempty"`
 
+			// AnyType2 represents any type.
+			//
 			// This should be an interface{}
 			AnyType2         *AnyType2         `json:"anyType2,omitempty"`
 			CustomStringType *CustomStringType `json:"customStringType,omitempty"`
@@ -1195,24 +1201,25 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL
 // Base64 encoded, gzipped, json marshaled Swagger object
 var swaggerSpec = []string{
 
-	"H4sIAAAAAAAC/7RW3W7bNhR+lQOuwG4cy3ZbrPFdVnRDBqwNmgC9SHJBk8cWG4lkyaMkguF3H0hKllxL",
-	"WbN0uYlo8vx83/ndMmFKazRq8my5ZZY7XiKhi6dLckpvzvUFpzycJXrhlCVlNFuyM/DxHiynHPaSbMJU",
-	"uA6/sgnTvES2ZJ7ChcNvlXIo2ZJchRPmRY4lD6qpts0zpTdst9u1l9GRt5fEHfkvivKPVblCd+zNVa48",
-	"JBEINsFHEXhQlAMHncQmrSGz+oqC2G7CznR9VVucs+W2Oy3GDOSmKiSsELgGpQndmgvc7oKi95UnUybO",
-	"rqKVLVsbV3JiSybiZWe/BTphf6JGp8Sn5FBHRefhx6oo+KrAC2csOlKYQnVwMtFNXgxwOdlfnmnZ6grv",
-	"9P47ReNIrgvXdvzyeUoPtF5338P6bo/iFfICReUU1Zch2Ak9FwK9PyFzhzqcV8gduj9a8v/6cnWSIgHp",
-	"JcSX0xvNmjQLJpJQF6KcyKZMVHptBhICPYHgHj2sjYN77pSpPCjvq/hTpSWYe3RAqsQpXBTIPQKXEjhQ",
-	"KxtEbzTXNayqDazVI8rkFikKJCYrl+juo2v36HyyPp/OprMUXNTcKrZkr6ez6ZxNYuVFWjLUvnJ4gvfo",
-	"asqV3pwof+JwjQ61SHHdII3kOmppjdIE+Kg8efAGKOcEXccAwXWoBeGQE0pQGihX/kZ7iwK4lqANhQfW",
-	"VRplxBWSlgcz55It2Yfo4Ie9f+f+c+ddyAlv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Wr7V6Jqd",
+	"+Brzsrj+53bQ9t5dJj61DSoiE33Fv7NyRgj7y+btXfAn1ksLsXZluz1Os6zdzsK+N1SItpJ2KHUoyH8C",
+	"AAD//ypj9OOiDQAA",
 }
 
 // GetSwagger returns the content of the embedded swagger specification file
diff --git a/internal/test/schemas/schemas.yaml b/internal/test/schemas/schemas.yaml
index a8f0b3a..bffc58e 100644
--- a/internal/test/schemas/schemas.yaml
+++ b/internal/test/schemas/schemas.yaml
@@ -118,7 +118,10 @@ components:
       type: object
     AnyType1: {}
     AnyType2:
-      description: This should be an interface{}
+      description: |
+        AnyType2 represents any type.    
+        
+        This should be an interface{}
     CustomStringType:
       type: string
       format: custom
diff --git a/pkg/codegen/utils_test.go b/pkg/codegen/utils_test.go
index 05278eb..4857ee4 100644
--- a/pkg/codegen/utils_test.go
+++ b/pkg/codegen/utils_test.go
@@ -210,13 +210,13 @@ func TestStringToGoComment(t *testing.T) {
 	}{
 		{
 			input:    "",
-			expected: "// ",
-			message:  "blank string should be preserved with comment",
+			expected: "",
+			message:  "blank string should be ignored due to human unreadable",
 		},
 		{
 			input:    " ",
-			expected: "//  ",
-			message:  "whitespace should be preserved with comment",
+			expected: "",
+			message:  "whitespace should be ignored due to human unreadable",
 		},
 		{
 			input:    "Single Line",