diff --git a/pkg/generators/openapi.go b/pkg/generators/openapi.go index 745bac6b7..1ffcf9094 100644 --- a/pkg/generators/openapi.go +++ b/pkg/generators/openapi.go @@ -844,15 +844,9 @@ func (g openAPITypeWriter) generateDescription(CommentLines []string) { } } - postDoc := strings.TrimLeft(buffer.String(), "\n") - postDoc = strings.TrimRight(postDoc, "\n") - postDoc = strings.Replace(postDoc, "\\\"", "\"", -1) // replace user's \" to " - postDoc = strings.Replace(postDoc, "\"", "\\\"", -1) // Escape " - postDoc = strings.Replace(postDoc, "\n", "\\n", -1) - postDoc = strings.Replace(postDoc, "\t", "\\t", -1) - postDoc = strings.Trim(postDoc, " ") - if postDoc != "" { - g.Do("Description: \"$.$\",\n", postDoc) + postDoc := strings.TrimSpace(buffer.String()) + if len(postDoc) > 0 { + g.Do("Description: $.$,\n", fmt.Sprintf("%#v", postDoc)) } } diff --git a/pkg/generators/openapi_test.go b/pkg/generators/openapi_test.go index 3fa753d14..4ffdf6c92 100644 --- a/pkg/generators/openapi_test.go +++ b/pkg/generators/openapi_test.go @@ -150,6 +150,9 @@ func TestSimple(t *testing.T) { // an int member with a default // +default=1 OmittedInt int ` + "`" + `json:"omitted,omitempty"` + "`" + ` + // a field with an invalid escape sequence in comment + // ex) regexp:^.*\.yaml$ + InvalidEscapeSequenceInComment string }` packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) { @@ -384,8 +387,16 @@ Type: []string{"integer"}, Format: "int32", }, }, +"InvalidEscapeSequenceInComment": { +SchemaProps: spec.SchemaProps{ +Description: "a field with an invalid escape sequence in comment ex) regexp:^.*\\.yaml$", +Default: "", +Type: []string{"string"}, +Format: "", +}, +}, }, -Required: []string{"String","Int64","Int32","Int16","Int8","Uint","Uint64","Uint32","Uint16","Uint8","Byte","Bool","Float64","Float32","ByteArray","WithExtension","WithStructTagExtension","WithListType","Map","StringPointer"}, +Required: []string{"String","Int64","Int32","Int16","Int8","Uint","Uint64","Uint32","Uint16","Uint8","Byte","Bool","Float64","Float32","ByteArray","WithExtension","WithStructTagExtension","WithListType","Map","StringPointer","InvalidEscapeSequenceInComment"}, }, VendorExtensible: spec.VendorExtensible{ Extensions: spec.Extensions{